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:
// C#
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:
// C#
private void button1_Click(object sender, EventArgs e)
{
// Method code
}

      2.Add the event handler to indicate which method should receive events, as the
following code demonstrates:
// C#
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:
// C#
public delegate void MyEventHandler(object sender, EventArgs e);

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

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

// Note that C# checks to determine whether handler is null.
// 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
posted @ 2007-02-15 11:38  Jailu  阅读(563)  评论(0编辑  收藏  举报