3. Events
events, as a type in C #, define the ability to notify instances of classes and classes to bundle events and executable code together. An event is a message sent by an object that signals the occurrence of a notification operation. The operation may be caused by user interaction, or it may be triggered by some other program logic. The object that raises (triggers) an event is called the event sender, and the object that captures the event and responds to it is called the event receiver.
C # events are done in a "publish-to-order" manner. The event is advertised in a class, and then the event can be scheduled in any number of classes. In event communication, the event sender class does not know which object or method will receive (process) the event it raises. What is needed is a medium (or a mechanism similar to a pointer) between the source and the receiver, which is the delegate.
The event function is provided by 3 interconnected elements: the class that provides the event data, the event delegate, and the class that raised the event. If you want the class to throw an event named EventName, you need the following elements:
(1) The class holding the event data: named Eventnameeventargs. The class must inherit the System.EventArgs class. If you do not customize the class that holds the event data, use the System.EventArgs class.
(2) Delegate of the event: named Eventnameeventhandler
(3) The class that raised the event. The class must provide:
A. Event statement: public event Eventnameeventhandler EventName;
B. How to raise an event: named OnEventName
(4) Defines a class that uses this event. All of these classes should include:
Creates an event source object. Use the defined constructor to create an object that contains the class of the event definition.
Defines an event handler that defines the method associated with the event.
Registers the event source object with the event handler. Use the delegate object and the "+ =" operator and the "-=" operator to associate one or more methods with events from the event source.
The C # event mechanism is based on a delegate implementation, first defining a delegate:
public delegate void EventHandler (Object From,myeventargs e); From indicates the object in which the event occurred
Declaring event Format: Delegate name event name for event events
public event EventHandler TextOut;
The activation of an event is generally written as:
if (textout!=null)
TextOut (This,new EventArgs ());
Subscribe to Events + = Evsrc. Textout+=new Eventsource.eventhandler (catchevent);
Unsubscribe-= evsrc. Textout-=new Eventsource.eventhandler (catchevent);
Let's look at an example below:
1 Public classMyeventargs:eventargs2{//defining events that contain data3 4 Private stringStrText;5 PublicMyEventArgs (stringStrText)6 {7 This. StrText =StrText;8 }9 Public stringGetstrtextTen { One Get A { - returnStrText; - } the } - } - - Public classEventSource +{//the class that publishes the event - +MyEventArgs Evargs =NewMyEventArgs ("Triggering Events"); A Public Delegate voidEventHandler (Object from, MyEventArgs e);//Defining Delegates at Public EventEventHandler TextOut;//Defining Events - Public voidTriggerevent ()//ways to activate events - { - if(textout!=NULL) - { -TextOut ( This, Evargs); in } - } to } + classTest -{//class for subscribing to Events the Static voidMain (string[] args) * { $EventSource EVSRC =NewEventSource ();Panax NotoginsengEvsrc. TextOut + =NewEventsource.eventhandler (catchevent);//Subscribe to Events -Evsrc. Triggerevent ();//Triggering Events theConsole.WriteLine ("--------------"); + AEvsrc. TextOut-=NewEventsource.eventhandler (catchevent);//Unsubscribe Events theEvsrc. Triggerevent ();//Nothing is done because the subscription has been canceled +Console.WriteLine ("--------------"); - $Test theapp=NewTest (); $Evsrc. TextOut + =NewEventsource.eventhandler (theapp.instancecatch); - evsrc. Triggerevent (); - } the Private Static voidCatchevent (Object from, MyEventArgs e) -{//Event HandlersWuyiConsole.WriteLine ("catchevent:{0}", e.getstrtext); the } - Private voidInstancecatch (Object from, MyEventArgs e) Wu{//Event Handlers -Console.WriteLine ("instancecatch:{0}", e.getstrtext); About } $}
You can find out some relevant elements of the event against the above introduction.
Some basic knowledge of delegates, Lamada expressions, and events is complete ....
This is my first time to write a blog, I am still groping in the university slag, almost all the content of the book, I feel a word of a word out of the impression will be more profound, understanding is more in place, the big God do not spray ...
C # Foundation Strengthening-delegates, lambda expressions, and events (bottom)