规范的.net 事件原理

<接着上一篇事件委托讲解>

.net framework 事件设计准则
1 声明delegate时,使用void 类型当作返回值,EventName事件的事件委托是

EventNameEventHandler,事件接受两个传入参数,一律命名为sender和e。
2 定义一个提供事件数据的类。对类以EventNameEventArgs进行命名,从system.EventArgs派生该

类,然后添加所有事件特定的成员
public delegate void EventNameEventHandler(object sender,EventNameEventArgs e);
EventNameEventArgs用于封装事件的多个参数,这个类可以自己写但必须继承EventArgs
class EventNameEventArgs:EventArgs
{
 ...
}

3 在引发事件的类中提供一个手保护的方法,以OnEventName进行命名。在该方法中引发事件
protected virtual void OnEventName(EventArgs e)
{
 if(EventName!=null)
 {
  EventName(this,e);
 }
}

 

 1 using  System;
 2 using  System.Collections.Generic;
 3
 4 namespace  event3
 5 ExpandedBlockStart.gifContractedBlock.gif {
 6    class PubEventArgs:EventArgs//提供事件所需要的数据
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 8        private readonly string m_magazineName;
 9        private readonly DateTime m_pubDate;
10        public PubEventArgs(string magazineName,DateTime pubDate)
11ExpandedSubBlockStart.gifContractedSubBlock.gif        {
12            this.m_magazineName=magazineName;
13            this.m_pubDate=pubDate;
14        }

15        public  string magazineName
16ExpandedSubBlockStart.gifContractedSubBlock.gif        {
17ExpandedSubBlockStart.gifContractedSubBlock.gif            get{return m_magazineName;}
18        }

19        public DateTime pubDate
20ExpandedSubBlockStart.gifContractedSubBlock.gif        {
21ExpandedSubBlockStart.gifContractedSubBlock.gif            get{return m_pubDate;}
22        }

23    }

24    
25    class Publisher //出版社
26ExpandedSubBlockStart.gifContractedSubBlock.gif    {
27        public delegate void PubComputerEventHandler(object sender,PubEventArgs e); //事件所需的代理 委托是种类
28        public delegate void PubLifeEventHandler(object sender,PubEventArgs e);
29        public event PubComputerEventHandler PubComputer=null;//事件的声明
30        public event PubLifeEventHandler PubLife=null;
31        
32        protected virtual void OnPubComputer(PubEventArgs e)
33ExpandedSubBlockStart.gifContractedSubBlock.gif        {
34            PubComputerEventHandler handler=PubComputer; //临时委托变量
35            if(handler!=null)
36ExpandedSubBlockStart.gifContractedSubBlock.gif            {
37                handler(this,e);
38            }

39        }

40        
41        protected virtual void OnPubLife(PubEventArgs e)
42ExpandedSubBlockStart.gifContractedSubBlock.gif        {
43            PubLifeEventHandler handler=PubLife; //临时委托变量
44            if(handler!=null)
45ExpandedSubBlockStart.gifContractedSubBlock.gif            {
46                handler(this,e);
47            }

48        }

49        
50        public void issuComputer(string magazineName,DateTime pubDate) //触发订阅电脑杂志事件的方法
51ExpandedSubBlockStart.gifContractedSubBlock.gif        {
52            Console.WriteLine("发行"+magazineName);
53            OnPubComputer(new PubEventArgs(magazineName,pubDate));
54            Console.ReadLine();
55        }

56        
57        public void issuLife(string magazineName,DateTime pubDate) 
58ExpandedSubBlockStart.gifContractedSubBlock.gif        {
59            Console.WriteLine("发行"+magazineName);
60            OnPubLife(new PubEventArgs(magazineName,pubDate));
61            Console.ReadLine();
62        }

63    }

64    
65    class Subscriber //订阅者
66ExpandedSubBlockStart.gifContractedSubBlock.gif    {
67        private string name;
68        public Subscriber(string name)
69ExpandedSubBlockStart.gifContractedSubBlock.gif        {
70            this.name=name;
71        }

72        public void Receive(object sender,PubEventArgs e)//在事件订阅者中定义事件处理程序
73ExpandedSubBlockStart.gifContractedSubBlock.gif        {
74            Console.WriteLine(e.pubDate+" "+name+"已经收到了"+e.magazineName);
75        }

76    }

77    
78    class Story //故事发生在订阅者和出版社之间
79ExpandedSubBlockStart.gifContractedSubBlock.gif    {
80        static void Main()
81ExpandedSubBlockStart.gifContractedSubBlock.gif        {
82            Publisher Pub=new Publisher();
83            Subscriber zs=new Subscriber("张三");
84            Pub.PubLife+=new Publisher.PubLifeEventHandler(zs.Receive);//张三订阅生活杂志
85            Subscriber ls=new Subscriber("李四");//李四也订阅了电脑、生活杂志
86            Pub.PubComputer+=new Publisher.PubComputerEventHandler(ls.Receive);
87            Pub.PubLife+=new Publisher.PubLifeEventHandler(ls.Receive);
88            Pub.issuComputer("《电脑》杂志",Convert.ToDateTime("2008-11-28")); //收到事件后做出的处理
89            Pub.issuLife("《生活》杂志",Convert.ToDateTime("2008-11-28"));
90            //一年后李四由于经济危机取消了《生活》杂志的订阅
91            Console.WriteLine("一年后李四由于经济危机取消了《生活》杂志的订阅");
92            Pub.PubLife-=new Publisher.PubLifeEventHandler(ls.Receive);
93            Pub.issuComputer("《电脑》杂志",Convert.ToDateTime("2009-11-28")); 
94            Pub.issuLife("《生活》杂志",Convert.ToDateTime("2009-11-28"));
95        }

96    }

97}

转载于:https://www.cnblogs.com/hemood/archive/2008/11/27/1342639.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值