首页 > 代码库 > 弱事件

弱事件

----------------------------------------------------主程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            PhoneInfo p = new PhoneInfo("小米4");
            Consumer c = new Consumer("张迪");
            WeakPhoneEventManage.AddListener(p, c);//添加订阅事件
            p.StartPhone();//输出
            p.StartPhone();//输出
            WeakPhoneEventManage.RemoveListener(p, c);//删除订阅事件
            p.StartPhone();//不输出
        }
    }
}

----------------------------------------------------PhoneInfo.cs(发布程序)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication10
{
    public class PhoneInfo : EventArgs//继承EventArgs
    {
        public event EventHandler<PhoneInfo> PhoneEvent;//声明事件
        public string Name { get; set; }//手机名称
        public PhoneInfo(string name)//初始化手机名称
        {
            this.Name = name;
        }
        public void StartPhone()
        {
            if (PhoneEvent != null)//判断事件是否为空
                PhoneEvent(this, this);
        }
    }
}

----------------------------------------------------Consumer.cs(监听器)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace ConsoleApplication10
{
    public class Consumer : IWeakEventListener//监听器必须继承IWeakEventListener
    {
        public string Name { get; private set; }
        public Consumer(string name)
        {
            this.Name = name;
        }
        public void EventListener(object sender, PhoneInfo e)//订阅方法
        {
            Console.WriteLine("{0}先生,您好!{1}已经上架了,去关注吧!",this.Name,e.Name);
            Console.ReadKey();
        }

        public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)//实现接口
        {
            EventListener(sender, e as PhoneInfo);
            return true;
        }
    }
}

----------------------------------------------------WeakPhoneEventManage.cs(发布程序与监听器之间的连接)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace ConsoleApplication10
{
    public class WeakPhoneEventManage:WeakEventManager
    {
        //静态属性,用于实例WeakPhoneEventManage(创建一个实例)
        public static WeakPhoneEventManage CurrentEvent { get {
            WeakPhoneEventManage w= GetCurrentManager(typeof(WeakPhoneEventManage)) as WeakPhoneEventManage;
            if (w == null)
            {
                w = new WeakPhoneEventManage();
                SetCurrentManager(typeof(WeakPhoneEventManage), w);
            }
            return w;
        } }
        //添加订阅事件
        public static void AddListener(object source,IWeakEventListener listener)
        {
            CurrentEvent.ProtectedAddListener(source, listener);
        }
        //删除订阅事件
        public static void RemoveListener(object source, IWeakEventListener listener)
        {
            CurrentEvent.ProtectedRemoveListener(source, listener);
        }
        //重写StartListening方法
        protected override void StartListening(object source)
        {
            (source as PhoneInfo).PhoneEvent += EventListener;//订阅事件
        }
        //重写StopListening方法
        protected override void StopListening(object source)
        {
            (source as PhoneInfo).PhoneEvent -= EventListener;//取消订阅
        }
        protected void EventListener(object o,PhoneInfo p)//方法签名
        {
            DeliverEvent(o, p);//将事件传递给事件侦听器
        }
 

    }
}

 

 

本文出自 “程序猿的家” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1540214