.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);
}
}
}

.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. CSS选择器优先级 CSS权值

    计算指定选择器的优先级:重新认识CSS的权重 标签的权值为 0,0,0,1 类的权值为 0,0,1,0 属性选择的权值为 0,0,1,1  ID的权值为 0,1,0,0 important的权值为最高 ...

  2. The median of multi ascending array

    Given 17 arrays,every array is ascending.The task is to get the median of all these numbers. 0 1 2 3 ...

  3. iOS -- MJrefresh

    - (void)refresh { MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self r ...

  4. 1010每次备份我的MySQL数据库

    转自http://bbs.csdn.net/topics/320136534 在windows平台下面 /*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/* ...

  5. mysql case when then end学习

    表 vtiger_acctive,字段 id,name. 1. 查询中使用 # 查询如果name的值为 hello1 时输出 6666,当值为 hello2 时,输出 333333 select ca ...

  6. zabbix 监控MySQL

    现在我来说一下我的监控环境 zabbix-3.0.3 MySQL-5.6.23 1.首先我们要登录MySQL,创建一个监控MySQL的用户 GRANT USAGE,PROCESS,SUPER,REPL ...

  7. 【UOJ #29】【IOI 2014】holiday

    http://uoj.ac/problem/29 cdq四次处理出一直向左, 一直向右, 向左后回到起点, 向右后回到起点的dp数组,最后统计答案. 举例:\(fi\)表示一直向右走i天能参观的最多景 ...

  8. ThinkPHP的RBAC

    基于角色的访问控制(Role-Based Access Control) 在RBAC中,权限与角色相关联,用户通过成为适当角色的成员而得到这些角色的权限. ThinkPHP通过5张表实现权限控制 th ...

  9. 【转】ViewPager学习笔记(一)——懒加载

    在项目中ViewPager和Fragment接口框架已经是处处可见,但是在使用中,我们肯定不希望用户在当前页面时就在前后页面的数据,加入数据量很大,而用户又不愿意左右滑动浏览,那么这时候ViewPag ...

  10. Spark 常用参数及调优

    spark streaming 调优的几个角度: 高效地利用集群资源减少批数据的处理时间 设置正确的批容量(size),使数据的处理速度能够赶上数据的接收速度 内存调优 Spark SQL 可以通过调 ...