Understanding 1: Concept of event Delegation
. NET Framework event delegation follows specific signatures and naming conventions. This Convention relies on visual design tools to provide a consistent model for client code. To understand this convention, let's take a look at a common accessible event Delegate system. eventhandler In the. NET Framework:
Public Delegate void eventhandler (Object sender, eventargs E );
The following are some specific event Delegate signature conventions:
◎ The event Delegate return type isVoid
◎ An event Delegate has two parameters. The first parameter is of the object type, indicating the event sender. Second Parameter
The data describing the event is an instance of the class derived from system. eventargs.
You should name the event data class and event Delegate according to the naming conventions of the. NET Framework. The event data class is added by the event name.
The suffix eventargs, for example, monthchangeeventargs.
Event delegation is composed of the event name and the suffix eventhandler, for example, monthchangeeventhandler. Event Delegate
They are named by event handler because they are bound to the method used to process events.
Understanding 2: Wiring events
The process of associating event handlers with events (adding delegates to the invocation list) is called event wiring ).
The process of deleting an event handler from an event is called event unwring ).
In C #, the syntax for the event cabling and disconnection event handler is as follows:
Button. Click + = new eventhandler (this. button_clicked );
Button. Click-= new eventhandler (this. button_clicked );
Here, a button is an instance of the button control and is created in a class with the button_clicked method to process the Click Event of the button.
Understanding 3: Implementation of event Delegation
To implement events in a class, you need an event data class, event Delegate, a delegate member with the invocation list in the class, and a method for releasing Event Notifications.
The specific implementation process is as follows:
1) if the class does not have any associated event data, use the eventargs class for the event data. Alternatively, you can use other pre-existing event data classes. If there is no appropriate event data class, define an event to include event data. This class must be derived from system. eventargs. According to the rule, its name should be obtained by adding eventargs to the event name. For example, adcreatedeventargs, monthchangedeventargs.
The following Code declares an event data class:
Public class lowchargeeventargs: eventargs {...}
2) If the event is not associated with data, use eventargs in step 1 and system. eventhandler as the event Delegate or use other pre-existing delegates that match the event. If there is no proper event Delegate, define an event Delegate. The second parameter of the delegate has the type of the event data class from the first step. According to the rules, the event Delegate name is appended with eventhandler. For example, adcreateeventhandler and monthchangedeventhandler. The following code defines event delegation:
Public Delegate void lowchargeeventhandler (Object sender, lowchargeeventargs E );
3) define event members using the event keyword in the class. SetEvent nameTo the event member.The member type is the type of the event Delegate in step 2..
Example:
Public event lowchargeeventhandler lowcharge;
The event member contains a list of delegates that subscribe to events. When this member is called, it allocates events by calling the delegate.
4) define a protected virtual method in the class and call the event Delegate after checking whether there is an event listener. The parameter is the event data × eventargs defined in step 1. The method name is prefixed with on before the event name. For example:
Protected virtual void onlowcharge (lowchargeeventargs e ){
If (lowcharge! = NULL ){
Lowcharge (this, e );
}
}
The on <eventname> method is used to notify event subscribers.