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

  1. namespace System
  2. {
  3. public delegate void EventHandler(object sender, EventArgs e);
  4. }

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

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

  1. public class CustomEventArgs:EventArgs

发布事件有三种方式:

1. 使用.net framework标准委托

  1. public event EventHandler RaiseCustomEvent;

2. 自定义EventArgs参数

  1. public event CustomEventHandler RaiseCustomEvent;

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

  1. public event EventHandler<CustomEventArgs> RaiseCustomEvent;

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

首先分两个类:

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

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

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

  1. protected virtual void OnClick(EventArgs e);

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

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

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

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. }

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

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

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

  1. namespace DotNetEvents
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. Publisher pub = new Publisher();
  8. Subscriber sub1 = new Subscriber("sub1", pub);
  9. Subscriber sub2 = new Subscriber("sub2", pub);
  10.  
  11. //Call the method that raises the event
  12. pub.DoSomething();
  13.  
  14. Console.WriteLine("Press Enter to close this window.");
  15. Console.ReadKey();
  16. }
  17. }
  18.  
  19. //Define a class to hold custom event info
  20. public class CustomEventArgs:EventArgs
  21. {
  22. private string message;
  23. public CustomEventArgs (string s)
  24. {
  25. message = s;
  26. }
  27. public string Message
  28. {
  29. get { return message;}
  30. set { message = value; }
  31. }
  32. }
  33.  
  34. //Class that publishes an event
  35. class Publisher
  36. {
  37. //Declare the event using EventHandler<T>
  38. public event EventHandler<CustomEventArgs> RaiseCustomEvent;
  39.  
  40. public void DoSomething()
  41. {
  42. //Write some code that does something useful here
  43. //then raise the event.You can also raise an event
  44. //before you execute a block of code.
  45. OnRaiseCustomEvent(new CustomEventArgs("Did something"));
  46. }
  47. //Wrap event invocations inside a protected virtual method
  48. //to allow derived classes to oveeride the event invocation behavior
  49. protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
  50. {
  51. //Make a temporary copy of the event to avoid possibility of
  52. //a race condition if the last subscriber unsubscribes
  53. //immediately after the null check and before the event is raise
  54. if(RaiseCustomEvent !=null )
  55. {
  56. e.Message = String.Format(" at {0}", DateTime.Now.ToString());
  57. RaiseCustomEvent(this, e);
  58. }
  59. }
  60. }
  61.  
  62. //Class that subscribes to an event
  63. class Subscriber
  64. {
  65. private string id;
  66. public Subscriber (string ID,Publisher pub)
  67. {
  68. id = ID;
  69. //Subscribe to the event suing C# 2.0 syntax
  70. pub.RaiseCustomEvent += HandleCustomEvent;
  71. }
  72.  
  73. //Define what action to take when the event is raised
  74. void HandleCustomEvent(object sender,CustomEventArgs e)
  75. {
  76. Console.WriteLine(id + " receive this message: {0}", e.Message);
  77. }
  78. }
  79. }

.Net Framework中的标准委托和事件_1的更多相关文章

  1. C语言函数指针与 c#委托和事件对比

    C语言: 函数指针可以节省部分代码量,写类似具有多态的函数,比如要比较最大值,如果不用函数指针就只能写比较某一类型比如int类型的max函数,这个max无法比较string的大小.函数指针的意义就不多 ...

  2. C#学习之委托和事件

    C#学习中,关于委托和事件的一些见解: 一.C语言中的函数指针 想要理解什么是委托,就要先理解函数指针的概念.所谓函数指针,就是指向函数的指针(等于没说-.-).比如我定义了两个函数square和cu ...

  3. c#中委托和事件(续)(转)

    本文将讨论委托和事件一些更为细节的问题,包括一些大家常问到的问题,以及事件访问器.异常处理.超时处理和异步方法调用等内容. 为什么要使用事件而不是委托变量? 在 C#中的委托和事件 中,我提出了两个为 ...

  4. 转载:C#中委托、事件与Observer设计模式

    原文地址 http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx 感谢博主分享! 范例说明 假设 ...

  5. C#中委托和事件

    目 录 将方法作为方法的参数 将方法绑定到委托 更好的封装性 限制类型能力 范例说明 Observer 设计模式简介 实现范例的Observer 设计模式 .NET 框架中的委托与事件 为什么委托定义 ...

  6. c#中委托和事件(转)

    C# 中的委托和事件 引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真 ...

  7. C#中委托、事件和回调函数的理解

    在C#中我们经常会碰到事件,尤其是在WPF或者WinForm中,窗体加载.或者点击一个按钮,都会触发事件.实际上,事件是对委托的封装.如果不进行封装,让委托暴露给调用者,调用者就可以把委托变量重新引用 ...

  8. 关于c#中委托与事件的一些理解

    文章目的:作者(初学者)在学习c#的过程中,对事件.委托及其中的“object sender,EventArgs e”一直感觉理解不透,因此在网上找了一些资料,学习并整理出了该篇笔记,希望能将自己的心 ...

  9. 详解C#泛型(二) 获取C#中方法的执行时间及其代码注入 详解C#泛型(一) 详解C#委托和事件(二) 详解C#特性和反射(四) 记一次.net core调用SOAP接口遇到的问题 C# WebRequest.Create 锚点“#”字符问题 根据内容来产生一个二维码

    详解C#泛型(二)   一.自定义泛型方法(Generic Method),将类型参数用作参数列表或返回值的类型: void MyFunc<T>() //声明具有一个类型参数的泛型方法 { ...

随机推荐

  1. RabbitMQ 工作队列

    创建一个工作队列用来在工作者(consumer)间分发耗时任务. 工作队列的主要任务是:避免立刻执行资源密集型任务,然后必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送给队列.工作进 ...

  2. oracle去重等基础问题

    --去重查询方法一:根据id select * from sxe where id in(select min(id) from sxe group by username) order by id ...

  3. 给li设置float浮动属性之后,无法撑开外层ul的问题。(原址:http://www.cnblogs.com/cielzhao/p/5781462.html)

    最近在项目中有好几次遇到这个问题,感觉是浮动引起的,虽然用<div style="clear:both"></div>解决了,但自己不是特别明白,又在网上查 ...

  4. Linux系统日志

    日 志 文 件 说    明 /var/log/message 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一 /var/log/secure 与安全相关的日志信息 /va ...

  5. Echarts-axislabel文字过长导致显示不全或重叠

    先看两张图 按目前情况,官方并为对axislabel的高度或者宽度做调整.所以解决方案只能从其他方案下手 解决方案有几种 第一种为上图解决方案 设置grid属性定义图的大小来释放空间,使得axisla ...

  6. mysql-窗口多表连接视图view

    create VIEW view_comment as (' as type FROM wei_comment_comment w) UNION ALL (' as type from review_ ...

  7. Html+js 控制input输入框提示

    <input type="text" class="fl plsearch_txt" id="key" value="请输入 ...

  8. 主线程MainThread与渲染线程RenderThread

    在Android 5.0之前,Android应用程序的主线程同时也是一个Open GL线程.但是从Android 5.0之后,Android应用程序的Open GL线程就独立出来了,称为Render ...

  9. 关于Yii2中CSS,JS文件的引入心得

    js和css的引入 use yii\helpers\Html; 1.全局引入,所有的view生效 /assets/AppAsset.php public $css = [ 'css/site.css' ...

  10. 64.GitHub 排名前100的android项目简介

    GitHub Android Libraries Top 100 简介 排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不 ...