3.Event & Delegate

书目:MCTS 70-536: TS: .NET Framework 2.0-Application Development Foundation 
章节:Chapter 1: Framework Fundamentals -- Lesson 3: Constructing Classes -- Event
原文内容:
Most projects are nonlinear. In Windows Forms applications, you might have to wait
for a user to click a button or press a key, and then respond to that event. In server
applications, you might have to wait for an incoming network request. These capabilities
are provided by events in the .NET Framework, as described in the following
sections.

What Is an Event?
An event is a message sent by an object to signal the occurrence of an action. The
action could be caused by user interaction, such as a mouse click, or it could be triggered
by some other program logic. The object that raises the event is called the event
sender. The object that captures the event and responds to it is called the event receiver.

In event communication, the event sender class does not know which object or
method will receive (handle) the events it raises. What is needed is an intermediary
(or pointer-like mechanism) between the source and the receiver. The .NET Framework
defines a special type (Delegate) that provides the functionality of a function
pointer.

What Is a Delegate?
A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate
class has a signature, and it can hold references only to methods that match its
signature. A delegate is thus equivalent to a type-safe function pointer or a callback.
While delegates have other uses, the discussion here focuses on the event-handling
functionality of delegates. A delegate declaration is sufficient to define a delegate class.
The declaration supplies the signature of the delegate, and the common language
runtime provides the implementation. The following example shows an event delegate
declaration:
None.gif //  C#
None.gif
public   delegate   void  AlarmEventHandler( object  sender, EventArgs e);

The standard signature of an event handler delegate defines a method that does not
return a value, whose first parameter is of type Object and refers to the instance that
raises the event, and whose second parameter is derived from type EventArgs and
holds the event data. If the event does not generate event data, the second parameter
is simply an instance of EventArgs. Otherwise, the second parameter is a custom
type derived from EventArgs and supplies any fields or properties needed to hold the
event data.

EventHandler is a predefined delegate that specifically represents an event handler
method for an event that does not generate data. If your event does generate data, you
must supply your own custom event data type and either create a delegate where the
type of the second parameter is your custom type, or you must use the generic
EventHandler delegate class and substitute your custom type for the generic type
parameter.

To associate the event with the method that will handle the event, add an instance of
the delegate to the event. The event handler is called whenever the event occurs,
unless you remove the delegate.

How to Respond to an Event

You must do two things to respond to an event:
      1.Create a method to respond to the event. The method must match the Delegate
signature. Typically, this means it must return void and accept two parameters:
an Object and an EventArgs (or a derived class). The following code demonstrates
this:
None.gif //  C#
None.gif
private   void  button1_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
// Method code
ExpandedBlockEnd.gif
}

      2.Add the event handler to indicate which method should receive events, as the
following code demonstrates:
None.gif //  C#
None.gif
this .button1.Click  +=   new  System.EventHandler( this .button1_Click);
When the event occurs, the method you specified will run.

How to Raise an Event

      1.Create a delegate:
None.gif //  C#
None.gif
public   delegate   void  MyEventHandler( object  sender, EventArgs e);

      2.Create an event member:
None.gif //  C#
None.gif
public   event  MyEventHandler MyEvent;

      3.Invoke the delegate within a method when you need to raise the event, as the following
code demonstrates:
None.gif //  C#
None.gif
MyEventHandler handler  =  MyEvent;
None.gifEventArgs e 
=   new  EventArgs();
None.gif
if  (handler  !=   null )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
// Invokes the delegates.
InBlock.gif
handler(this, e);
ExpandedBlockEnd.gif}

None.gif
//  Note that C# checks to determine whether handler is null.
None.gif
//  This is not necessary in Visual Basic

Additionally, you can derive a custom class from EventArgs if you need to pass information
to the event handler.

参考文章:
http://www.akadia.com/services/dotnet_delegates_and_events.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值