关闭

自定义事件和参数。详细版。。。

491人阅读 评论(0) 收藏 举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace QinQinGo.MyControl
{
    /// <summary>
    /// AdvertPicControl.xaml 的交互逻辑 广告图片控件 播放完毕后自我销毁
    /// </summary>
    public partial class AdvertPicControl : UserControl
    {
        #region 加载List数据
        /// <summary>
        /// 当前图片地址播放列表
        /// </summary>
        private static List<string> currentList;

        public static DependencyProperty advertPicList = DependencyProperty.Register("advertPicList", typeof(List<string>), typeof(AdvertPicControl)
            , new PropertyMetadata(new PropertyChangedCallback(loadAdvertPic)));

        public List<string> AdvertPicList
        {
            get { return (List<string>)GetValue(advertPicList); }
            set { SetValue(advertPicList, value); }
        }

        /// <summary>
        /// 图片播放器地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void loadAdvertPic(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            AdvertPicControl advertPicControl = (AdvertPicControl)sender;
            if (e.Property == advertPicList)
            {
                advertPicControl.AdvertPicList = (List<string>)e.NewValue;
                currentList = advertPicControl.AdvertPicList;
            }
        }
        #endregion

        public static readonly RoutedEvent AdvertPicPlayStateChangedEvent;

        public class AdvertPicPlayEventArgs : RoutedEventArgs
        {
            public int playState
            {
                get;
                set;
            }

            public int playLength
            {
                get;
                set;
            }

            public int playIndex
            {
                get;
                set;
            }
        }

        static AdvertPicControl()
        {
            AdvertPicPlayStateChangedEvent = EventManager.RegisterRoutedEvent("AdvertPicPlayStateChanged",
                RoutingStrategy.Bubble, typeof(AdvertPicPlayStateChangedHandler), typeof(AdvertPicControl));
        }
        public delegate void AdvertPicPlayStateChangedHandler(object sender, AdvertPicPlayEventArgs e);
        public event AdvertPicPlayStateChangedHandler AdvertPicPlayStateChanged
        {
            add { AddHandler(AdvertPicControl.AdvertPicPlayStateChangedEvent, value); }
            remove { RemoveHandler(AdvertPicControl.AdvertPicPlayStateChangedEvent, value); }
        }

        public AdvertPicControl()
        {
            InitializeComponent();
        }

        DispatcherTimer switchPicTimer = new DispatcherTimer();
        int i = 0;
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            switchPicTimer.Interval = new TimeSpan(0, 0, 1);
            switchPicTimer.IsEnabled = true;
            switchPicTimer.Tick += SwitchPic;
            switchPicTimer.Start();
        }

        private void SwitchPic(object sender, EventArgs e)
        {
            Console.WriteLine("开始切换~~~");
            if (i < currentList.Count)
            {
                i++;
                Console.WriteLine("第" + i + "张");
            }
            else
            {
                AdvertPicPlayEventArgs args = new AdvertPicPlayEventArgs();
                args.RoutedEvent = AdvertPicPlayStateChangedEvent;
                args.playState = 1;
                RaiseEvent(args);
                switchPicTimer.Stop();
            }
        }
    }
}


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QinQinGo.ControlWindow;
using QinQinGo.BLL;
using QinQinGo.MyControl;
using QinQinGo.BLL.Shop.Biz.Impl;
using QinQinGo.BLL.Shop.Biz;
using QinQinGo.BLL.Advert;
using System.Windows.Media.Animation;
using QinQinGo.Common;
namespace QinQinGo
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SystemInit init = new SystemInit();
            init.InitSystem();
            List<string> advertPicList = AdvertInfoBiz.advertPicList;
            foreach (var item in advertPicList)
            {
                Console.WriteLine(item);
            }
            advertPicControl.AdvertPicList = advertPicList;
            advertPicControl.AdvertPicPlayStateChanged += testHandler;
        }

        private void testHandler(object sender, QinQinGo.MyControl.AdvertPicControl.AdvertPicPlayEventArgs args)
        {
            Console.WriteLine("testtttttttttt");
            Console.WriteLine("播放状态:" + args.playState);
        }

    }
}


 

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:69609次
    • 积分:1235
    • 等级:
    • 排名:千里之外
    • 原创:38篇
    • 转载:3篇
    • 译文:4篇
    • 评论:50条