.Net Framework中的标准委托,已经定义在命名空间System中,

namespace System
{
public delegate void EventHandler(object sender, EventArgs e);
}

.Net Framwork类库中的所有事件均基于EventHandler委托。

其中EventArgs参数是可以自定义,必须继承EventArgs类:

public class CustomEventArgs:EventArgs

发布事件有三种方式:

1. 使用.net framework标准委托

public event EventHandler RaiseCustomEvent;

2. 自定义EventArgs参数

public event CustomEventHandler RaiseCustomEvent;

3. 自定义EventArgs参数,泛型的表达方式

public event EventHandler<CustomEventArgs> RaiseCustomEvent;

如何:发布符合.net framework准则的事件

首先分两个类:

1. 发行者类(Publisher),发送Sends(引发Raise)事件的类,这个类做两件事

  • 发布事件
  • 编写事件的触发程序

窗体(Form)和控件(control)的类中都发布了事件(Load,Click等),事件的触发程序同样能找到,如下:

protected virtual void OnClick(EventArgs e);

2. 订阅者类(Subscriber),接收Receive(处理Handle)事件的类,这个类做两件事

  • 注册事件
  • 编写事件的处理程序

当你双击Form1中的一个按钮Button1,会直接进入

        private void button1_Click(object sender, EventArgs e)
{
}

其实就是在写事件的处理程序;在Form1.Designer.cs中会自动注册按钮双击事件

this.button1.Click += new System.EventHandler(this.button1_Click);

下面来看一个例子,是一个比较标准的方式添加自定义的事件:

namespace DotNetEvents
{
class Program
{
static void Main(string[] args)
{
Publisher pub = new Publisher();
Subscriber sub1 = new Subscriber("sub1", pub);
Subscriber sub2 = new Subscriber("sub2", pub); //Call the method that raises the event
pub.DoSomething(); Console.WriteLine("Press Enter to close this window.");
Console.ReadKey();
}
} //Define a class to hold custom event info
public class CustomEventArgs:EventArgs
{
private string message;
public CustomEventArgs (string s)
{
message = s;
}
public string Message
{
get { return message;}
set { message = value; }
}
} //Class that publishes an event
class Publisher
{
//Declare the event using EventHandler<T>
public event EventHandler<CustomEventArgs> RaiseCustomEvent; public void DoSomething()
{
//Write some code that does something useful here
//then raise the event.You can also raise an event
//before you execute a block of code.
OnRaiseCustomEvent(new CustomEventArgs("Did something"));
}
//Wrap event invocations inside a protected virtual method
//to allow derived classes to oveeride the event invocation behavior
protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
{
//Make a temporary copy of the event to avoid possibility of
//a race condition if the last subscriber unsubscribes
//immediately after the null check and before the event is raise
if(RaiseCustomEvent !=null )
{
e.Message = String.Format(" at {0}", DateTime.Now.ToString());
RaiseCustomEvent(this, e);
}
}
} //Class that subscribes to an event
class Subscriber
{
private string id;
public Subscriber (string ID,Publisher pub)
{
id = ID;
//Subscribe to the event suing C# 2.0 syntax
pub.RaiseCustomEvent += HandleCustomEvent;
} //Define what action to take when the event is raised
void HandleCustomEvent(object sender,CustomEventArgs e)
{
Console.WriteLine(id + " receive this message: {0}", e.Message);
}
}
}

最新文章

  1. 在利用xampp开发时候为apache设置多个项目目录
  2. Android—LayoutInflater学习笔记
  3. docker中如何制作自己的基础镜像
  4. CPP,MATLAB实现牛顿插值
  5. Sqli-labs less 20
  6. ALM/QC OTA Field in Database(查询ALM数据库的字段)
  7. oracle 的变量的定义和赋值
  8. ACCSESS数据库导入到SQL SEVERES2005
  9. uva 10003 Cutting Sticks (区间dp)
  10. Windows Services的1053错误的解决办法之一:修改注册表允许的响应时间
  11. EF OrderBy(string propertyname), OrderByDescending(string propertyname) 按属性排序,扩展方法
  12. java中部分知识点的验证实现
  13. 开源社交系统ThinkSNS v4.6.1更新日志及功能详解!
  14. Netty4 学习笔记之四: Netty HTTP服务的实现
  15. 利用create-react-app结合react-redux、react-router4构建单页应用
  16. 【Unity Shaders】Using Textures for Effects——通过修改UV坐标来滚动textures
  17. ES 19 - Elasticsearch的检索语法(_search API的使用)
  18. The superclass &quot;javax.servlet.http.HttpServlet&quot; was not found on the Java Build Path 解决办法
  19. Android设备一对多录屏直播--(UDP组播连接,Tcp传输)
  20. CEF 跨域访问iframe

热门文章

  1. shell note
  2. 【JavaScript】 JSON
  3. C++11的default和delete关键字
  4. Hadoop配置安装手册
  5. jQuery总结(2016-7-4)
  6. 理解 Paxos
  7. 强联通 HDU 2767 3836
  8. 51nod 1040最大公约数和(欧拉函数)
  9. Java算法-冒泡排序
  10. Android 自定义Activity基类与TitleBar