.NET Framework事件设计准则
1 、事件的命名准则应使用PascalCasing命名方式
2 、声明delegate时,使用void类型当作返回值,EventName事件的事件委托是EventNameEventHandler,事件接受两个传入参数,一律命为sender与e。
3 、定义一个提供事件数据的类。对类以EventNameEventArgs进行命名,从System.EventArgs派生该类,然后添加所有事件特定的成员。
puclic delegate void EventNameEventHandler( object sender,EventNameEventArgs e);
4 、在引发事件的类中提供一个受保护的方法。以OnEventName进行命名。在该方法中引发该事件
protected virtual void OnEventName(EventArgs e)
... {
if(EventName!=null)
...{
EventName(this,e);
}
}
1 、事件的命名准则应使用PascalCasing命名方式
2 、声明delegate时,使用void类型当作返回值,EventName事件的事件委托是EventNameEventHandler,事件接受两个传入参数,一律命为sender与e。
3 、定义一个提供事件数据的类。对类以EventNameEventArgs进行命名,从System.EventArgs派生该类,然后添加所有事件特定的成员。
puclic delegate void EventNameEventHandler( object sender,EventNameEventArgs e);
4 、在引发事件的类中提供一个受保护的方法。以OnEventName进行命名。在该方法中引发该事件
protected virtual void OnEventName(EventArgs e)
... {
if(EventName!=null)
...{
EventName(this,e);
}
}
采用 EventHandler 模式发布事件
1、在发行者类和订户类均可看见的范围中声明类,并添加保留自定义事件数据所需的成员。
public
class
CustomEventArgs : EventArgs
... {
public CustomEventArgs(string s)
...{
msg = s;
}
private string msg;
public string Message
...{
get ...{ return msg; }
}
}
... {
public CustomEventArgs(string s)
...{
msg = s;
}
private string msg;
public string Message
...{
get ...{ return msg; }
}
}
2、在发布类中声明一个委托。为它指定以 EventHandler 结尾的名称。第二个参数指定自定义 EventArgs 类型。
public
delegate
void
CustomEventHandler(
object
sender, CustomEventArgs a);
3、如果使用的是 EventHandler 的非泛型版本,并且您有一个由 EventArgs 派生的自定义类,请在发布类中声明您的事件,并且将您的委托用作类型:
class
Publisher
... {
public event CustomEventHandler RaiseCustomEvent;
}
... {
public event CustomEventHandler RaiseCustomEvent;
}
using
System;
class pubEventArgs : EventArgs
... {
private readonly string m_magazineName;
private readonly DateTime m_pubDate;
public pubEventArgs(string magazineName, DateTime pubDate)
...{
m_magazineName = magazineName;
m_pubDate = pubDate;
}
public string magazineName
...{
get ...{ return m_magazineName; }
}
public DateTime pubDate
...{
get ...{ return m_pubDate; }
}
}
class Publisher // 出版社
... {
public delegate void PubComputerEventHandler(object sender, pubEventArgs e);//声明发布电脑杂志事件所需的代理
public delegate void PubLifeEventHandler(object sender, pubEventArgs e);//声明发布生活杂志事件所需的代理
public event PubComputerEventHandler PubComputer;//事件的声明
public event PubLifeEventHandler PubLife;//事件的声明
protected virtual void OnPubComputer(pubEventArgs e)
...{
PubComputerEventHandler handler = PubComputer;
if (handler != null)
...{
handler(this, e);
}
}
protected virtual void OnPubLive(pubEventArgs e)
...{
PubLifeEventHandler handler = PubLife;
if (handler != null)
...{
handler(this, e);
}
}
public void issueComputer(string magazineName, DateTime pubDate)//触发事件的方法
...{
Console.WriteLine("发行" + magazineName);
OnPubComputer(new pubEventArgs(magazineName, pubDate));
}
public void issueLife(string magazineName, DateTime pubDate)
...{
Console.WriteLine("发行" + magazineName);
OnPubLive(new pubEventArgs(magazineName, pubDate));
}
}
class Subscriber
... {
private string name;
public Subscriber(string name)
...{
this.name = name;
}
public void Receive(object sender, pubEventArgs e)
...{
Console.WriteLine(e.pubDate + " " + name + "已经收到 " + e.magazineName);
}
}
class Story
... {
static void Main()
...{
Publisher Pub = new Publisher();
Subscriber zs = new Subscriber("张三");
Pub.PubComputer += new Publisher.PubComputerEventHandler(zs.Receive);
Subscriber ls = new Subscriber("李四");
Pub.PubComputer += new Publisher.PubComputerEventHandler(ls.Receive);
Pub.PubLife += new Publisher.PubLifeEventHandler(ls.Receive);
Pub.issueComputer("电脑杂志", Convert.ToDateTime("2006-1-1"));
Pub.issueLife("生活杂志", Convert.ToDateTime("2006-1-1"));
Console.WriteLine();
Console.WriteLine("一年后");
Pub.PubComputer -= new Publisher.PubComputerEventHandler(ls.Receive);
Pub.issueComputer("电脑杂志", Convert.ToDateTime("2007-1-1"));
Pub.issueLife("生活杂志", Convert.ToDateTime("2007-1-1"));
}
}
class pubEventArgs : EventArgs
... {
private readonly string m_magazineName;
private readonly DateTime m_pubDate;
public pubEventArgs(string magazineName, DateTime pubDate)
...{
m_magazineName = magazineName;
m_pubDate = pubDate;
}
public string magazineName
...{
get ...{ return m_magazineName; }
}
public DateTime pubDate
...{
get ...{ return m_pubDate; }
}
}
class Publisher // 出版社
... {
public delegate void PubComputerEventHandler(object sender, pubEventArgs e);//声明发布电脑杂志事件所需的代理
public delegate void PubLifeEventHandler(object sender, pubEventArgs e);//声明发布生活杂志事件所需的代理
public event PubComputerEventHandler PubComputer;//事件的声明
public event PubLifeEventHandler PubLife;//事件的声明
protected virtual void OnPubComputer(pubEventArgs e)
...{
PubComputerEventHandler handler = PubComputer;
if (handler != null)
...{
handler(this, e);
}
}
protected virtual void OnPubLive(pubEventArgs e)
...{
PubLifeEventHandler handler = PubLife;
if (handler != null)
...{
handler(this, e);
}
}
public void issueComputer(string magazineName, DateTime pubDate)//触发事件的方法
...{
Console.WriteLine("发行" + magazineName);
OnPubComputer(new pubEventArgs(magazineName, pubDate));
}
public void issueLife(string magazineName, DateTime pubDate)
...{
Console.WriteLine("发行" + magazineName);
OnPubLive(new pubEventArgs(magazineName, pubDate));
}
}
class Subscriber
... {
private string name;
public Subscriber(string name)
...{
this.name = name;
}
public void Receive(object sender, pubEventArgs e)
...{
Console.WriteLine(e.pubDate + " " + name + "已经收到 " + e.magazineName);
}
}
class Story
... {
static void Main()
...{
Publisher Pub = new Publisher();
Subscriber zs = new Subscriber("张三");
Pub.PubComputer += new Publisher.PubComputerEventHandler(zs.Receive);
Subscriber ls = new Subscriber("李四");
Pub.PubComputer += new Publisher.PubComputerEventHandler(ls.Receive);
Pub.PubLife += new Publisher.PubLifeEventHandler(ls.Receive);
Pub.issueComputer("电脑杂志", Convert.ToDateTime("2006-1-1"));
Pub.issueLife("生活杂志", Convert.ToDateTime("2006-1-1"));
Console.WriteLine();
Console.WriteLine("一年后");
Pub.PubComputer -= new Publisher.PubComputerEventHandler(ls.Receive);
Pub.issueComputer("电脑杂志", Convert.ToDateTime("2007-1-1"));
Pub.issueLife("生活杂志", Convert.ToDateTime("2007-1-1"));
}
}