委托与事件

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

1.什么是委托.

MSDN上解释..

1).委托是一种定义方法签名的类型。 当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联。 与委托的签名(由返回类型和参数组成)匹配的任何可访问类或结构中的任何方法都可以分配给该委托. 方法可以是静态方法,也可以是实例方法。 这样就可以通过变成方式来更改方法调用,还可以向现有类中插入新代码。只要知道委托的签名,就可以分配您自己的方法。

2). 您可以通过委托实例调用方法。事件处理程序就是通过委托调用的方法。

3). 委托用于将方法做为参数传递给其他方法。

4). 您可以创建一个自定义方法,当发生特定事件时某个类(例如 Windows控件) 就可以调用您的方法。

委托的特点:

1).委托类似于C++函数指针.但他们是类型安全的。

2).委托允许将方法作为参数进行传递。

3).委托可用于定义回调方法。

4).委托可以链接在一起; 例如,可以对一个事件调用多个方法。

5). 方法不必与委托签名签名安全匹配.

总结了下,主要分为两类:

1). 委托可以做为方法的类型。方法可以赋值给委托的实例,且可以用“+=”赋值多个,也可以用"-="移除.

2). 可以做为回调函数。

因为我们主要讲委托和事件,所以主要讲类1

1.委托

//Create a delegatepublicdelegatevoidDel(stringmessage);//Create a method for a delegate.publicstaticvoiddelegateMethod1(stringmessage)

{

System.Console.WriteLine("I am delegateMethod1:"+message);

}staticvoidMain(string[] args)

{//Instantiate the delegate.Del handler =delegateMethod1;//call the delegate.handler("Hello World");

如上图, 创建一个委托和一个签名相同的方法,将方法赋值给委托的实例,传参调用委托和调用方法一样.

//Create a method for a delegate.publicstaticvoiddelegateMethod2(stringmessage)

{

System.Console.WriteLine("I am delegateMethod2:"+message);

}staticvoidMain(string[] args)

{//Instantiate the delegate.Del handler =delegateMethod1;//call the delegate.handler("Hello World");

Console.WriteLine();//add second delegatehandler +=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 delegatehandler +=delegateMethod2;//call the delegate.handler("Bye World");

Console.WriteLine();//move methodhandler -=delegateMethod1;

handler("Hello world");

Console.ReadLine();

移除一个方法后的执行结果

上述可以通过加减赋值的叫多播委托,也是委托的一个作用

2. 事件

MSDN上事件解释: 类或对象可以通过事件向其他类或对象通知发生的相关事情。 发送(或引发)事件的类称为“发行者”,接收(或处理)事件的类称为“订户”。

通过解释知道事件的主要作用是通知其他对象或类发生了事情

winform程序和web程序主要通过订阅控件引发事件

事件的定义:

//Create a eventpubliceventDel eventDel;

看看事件的定义是不是和委托有点像,很像直接实例化的委托,只不过前面多了一个event关键字.

事件的作用是通知其他类发生了事情,那么怎么样通知呢?

1). 事件的通知

下面看个例程:

classMainNotify

{//定义一个委托publicdelegatevoidNotifyDelegate();//定义一个事件publiceventNotifyDelegate notifyevent;//方法publicvoidNotify()

{if(notifyevent !=null)

{

notifyevent();

}

}

}classPRogram

{//Receive Notify MethodpublicstaticvoidNotifyPocess()

{

System.Console.WriteLine("Receive Notify!");

}

staticvoidMain(string[] args)

{//实例化类型MainNotify mainnotify =newMainNotify();//事件绑定方法mainnotify.notifyevent +=NotifyPocess;//执行方法mainnotify.Notify();

}

}

1.在MainNotify类中定义了一个事件,定义一个方法,方法执行时触发事件

2.实例化这个类,将事件用"+="绑定这个接收方法,被绑定的方法叫订阅这个事件,如果取消订阅,事件触发时就不会执行这个方法.

执行结果

看看事件的使用和委托是不是类似,事件说白了就是实例化的委托.

2). 事件也可以多播

//Receive Notify MethodpublicstaticvoidNotifyPocess()

{

System.Console.WriteLine("Receive Notify!");

}//Receive notify MethodpublicstaticvoidnotifyProcess1()

{

System.Console.WriteLine("Receive Notify second!");

}//实例化类型MainNotify mainnotify =newMainNotify();//事件绑定方法mainnotify.notifyevent +=NotifyPocess;

mainnotify.notifyevent+=notifyProcess1;//执行方法mainnotify.Notify();

执行结果

如果方法不是静态方法,需要用委托实例化方法

//Receive Notify MethodpublicvoidNotifyPocess()

{

System.Console.WriteLine("Receive Notify!");

}//Receive notify MethodpublicvoidNotifyProcess1()

{

System.Console.WriteLine("Receive Notify second!");

}//实例化类型MainNotify mainnotify =newMainNotify();

//事件绑定方法mainnotify.notifyevent +=newMainNotify.NotifyDelegate(NotifyPocess);

mainnotify.notifyevent+=newMainNotify.NotifyDelegate(NotifyProcess1);//执行方法mainnotify.Notify();

winform控件事件程序

在窗体上添加一个按钮,双击进入方法,添加事件处理程序,点击按钮,事件处理程序触发.

下面演示怎么通过按钮事件触发程序

1.添加方法

privatevoidbutton1_Click(objectsender, EventArgs e)

{

System.Console.WriteLine("Receive Button Event!");

}

2.按钮自带委托和事件

//摘要://表示将处理不包含事件数据的事件的方法。////参数://sender://事件源。////e://不包含任何事件数据的 System.EventArgs。[Serializable]

[ComVisible(true)]publicdelegatevoidEventHandler(objectsender, EventArgs e);publiceventEventHandler Click;

3. 方法注册事件

this.button1.Click +=newSystem.EventHandler(this.button1_Click);

4.点击按钮事件触发通知方法

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航