
·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 委托与事件
1.什么是委托.
MSDN上解释..
1). 委托是一种定义方法签名的类型。 当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联。 与委托的签名(由返回类型和参数组成)匹配的任何可访问类或结构中的任何方法都可以分配给该委托. 方法可以是静态方法,也可以是实例方法。 这样就可以通过变成方式来更改方法调用,还可以向现有类中插入新代码。只要知道委托的签名,就可以分配您自己的方法。
2). 您可以通过委托实例调用方法。事件处理程序就是通过委托调用的方法。
3). 委托用于将方法做为参数传递给其他方法。
4). 您可以创建一个自定义方法,当发生特定事件时某个类(例如 Windows控件) 就可以调用您的方法。
委托的特点:
1). 委托类似于C++函数指针.但他们是类型安全的。
2). 委托允许将方法作为参数进行传递。
3). 委托可用于定义回调方法。
4). 委托可以链接在一起; 例如,可以对一个事件调用多个方法。
5). 方法不必与委托签名签名安全匹配.
总结了下,主要分为两类:
1). 委托可以做为方法的类型。方法可以赋值给委托的实例,且可以用“+=”赋值多个,也可以用"-="移除.
2). 可以做为回调函数。
因为我们主要讲委托和事件,所以主要讲类1
1.委托
//Create a delegate
public delegate void Del(string message);
//Create a method for a delegate.
public static void delegateMethod1(string message)
{
System.Console.WriteLine("I am delegateMethod1:"+message);
}
static void Main(string[] args)
{
//Instantiate the delegate.
Del handler = delegateMethod1;
//call the delegate.
handler("Hello World");
如上图, 创建一个委托和一个签名相同的方法,将方法赋值给委托的实例,传参调用委托和调用方法一样.

//Create a method for a delegate.
public static void delegateMethod2(string message)
{
System.Console.WriteLine("I am delegateMethod2:"+message);
}
static void Main(string[] args)
{
//Instantiate the delegate.
Del handler = delegateMethod1;
//call the delegate.
handler("Hello World");
Console.WriteLine();
//add second delegate
handler += delegateMethod2;
//call the delegate.
handler("Bye World");
Console.ReadLine();
}
将另一个方法用“+=”符号,链接到委托实例上,当调用该委托实例时就依次调用这两个方法.

同理可以用“-=”符号,移除委托中的方法.
//Instantiate the delegate.
Del handler = delegateMethod1;
//call the delegate.
handler("Hello World");
Console.WriteLine();
//add second delegate
handler += delegateMethod2;
//call the delegate.
handler("Bye World");
Console.WriteLine();
//move method
handler -= delegateMethod1;
handler("Hello world");
Console.ReadLine();
移除一个方法后的执行结果

上述可以通过加减赋值的叫多播委托,也是委托的一个作用
2. 事件
MSDN上事件解释: 类或对象可以通过事件向其他类或对象通知发生的相关事情。 发送(或引发)事件的类称为“发行者”,接收(或处理)事件的类称为“订户”。
通过解释知道事件的主要作用是通知其他对象或类发生了事情
winform程序和web程序主要通过订阅控件引发事件
事件的定义:
//Create a event public event Del eventDel;
看看事件的定义是不是和委托有点像,很像直接实例化的委托,只不过前面多了一个event关键字.
事件的作用是通知其他类发生了事情,那么怎么样通知呢?
1). 事件的通知
下面看个例程:
class MainNotify
{
//定义一个委托
public delegate void NotifyDelegate();
//定义一个事件
public event NotifyDelegate notifyevent;
//方法
public void Notify()
{
if (notifyevent != null)
{
notifyevent();
}
}
}
class PRogram
{
//Receive Notify Method
public static void NotifyPocess()
{
System.Console.WriteLine("Receive Notify!");
}
static void Main(string[] args)
{
//实例化类型
MainNotify mainnotify = new MainNotify();
//事件绑定方法
mainnotify.notifyevent += NotifyPocess;
//执行方法
mainnotify.Notify();
}
}
1.在MainNotify类中定义了一个事件,定义一个方法,方法执行时触发事件
2.实例化这个类,将事件用"+="绑定这个接收方法,被绑定的方法叫订阅这个事件,如果取消订阅,事件触发时就不会执行这个方法.
执行结果

看看事件的使用和委托是不是类似,事件说白了就是实例化的委托.
2). 事件也可以多播
//Receive Notify Method
public static void NotifyPocess()
{
System.Console.WriteLine("Receive Notify!");
}
//Receive notify Method
public static void notifyProcess1()
{
System.Console.WriteLine("Receive Notify second!");
}
//实例化类型
MainNotify mainnotify = new MainNotify();
//事件绑定方法
mainnotify.notifyevent += NotifyPocess;
mainnotify.notifyevent += notifyProcess1;
//执行方法
mainnotify.Notify();
执行结果

如果方法不是静态方法,需要用委托实例化方法
//Receive Notify Method
public void NotifyPocess()
{
System.Console.WriteLine("Receive Notify!");
}
//Receive notify Method
public void NotifyProcess1()
{
System.Console.WriteLine("Receive Notify second!");
}
//实例化类型
MainNotify mainnotify = new MainNotify();
//事件绑定方法
mainnotify.notifyevent += new MainNotify.NotifyDelegate(NotifyPocess);
mainnotify.notifyevent += new MainNotify.NotifyDelegate(NotifyProcess1);
//执行方法
mainnotify.Notify();
winform控件事件程序
在窗体上添加一个按钮,双击进入方法,添加事件处理程序,点击按钮,事件处理程序触发.
下面演示怎么通过按钮事件触发程序
1.添加方法
private void button1_Click(object sender, EventArgs e)
{
System.Console.WriteLine("Receive Button Event!");
}
2.按钮自带委托和事件
// 摘要:
// 表示将处理不包含事件数据的事件的方法。
//
// 参数:
// sender:
// 事件源。
//
// e:
// 不包含任何事件数据的 System.EventArgs。
[Serializable]
[ComVisible(true)]
public delegate void EventHandler(object sender, EventArgs e);
public event EventHandler Click;
3. 方法注册事件
this.button1.Click += new System.EventHandler(this.button1_Click);
4.点击按钮事件触发通知方法
