Event in C #

zhaozj2021-02-17  51

table of Contents:

Introduction of commission and events

Disclaimer events (definition events)

Registration issue

Realize event

trigger event

Add an "Add / Delete" method for events

Introduction of commission and events

The event is a specialized commission, and the entrusted is the foundation of the event, so introducing the commission before introducing the event.

Principal: Use the entrustment to apply (not a method) to the entrustment object, and then pass the delegate object to the code of the call method, which is not necessary to know which method is called when the code is compiled. A method of invoking unpaid calls can be modified by using a delegate program.

Event: The event is the message sent by the object, and the send signal notifies the customer to operate. This action may be caused by the mouse click, or it may be triggered by some other program logic. The sender of the event does not need to know the object or method to receive the event that it triggers. The sender only needs to know the intermediary existing between it and the recipient.

two. Disclaimer events (definition events)

Events in the GUI graphical interface (click Buttons or Select Menu, etc.) are events that have been defined, which is required to register events. We can also define a statement that the event can be applied anywhere in the program. Events and entrusted are unprecedented, and C # uses commission to declare events.

If the event is declared in the class, you must first declare the entry type of the event, or use a delegated type that has been declared in advance.

Below is an example of a statement:

Using system;

Public delegate void menuary () // A commission in advance can also be delegated in the internal statement of the class

Public class menuItem // Declare a class of a menu item

{

Public Event Menuhadler MenuSelection; // Declare an event and specify its entrustment type

String text; // Declare a string

Public MenuiteM (String Text) // Menu Project Class Constructor

{

This.Text = text; // Initialize the TEXT string

}

Public void fird ()

{

Menuseelection; / / Define a method of triggering events (later will be used)

}

Public String text // Define Properties

{

get

{

Return TEXT;

}

set

{

TEXT = VALUE;

}

}

}

The program first defines a delegate called MenuHandler and then defines a class called MenuItem, and the MenuiteM class contains a MenuSelection event (menu is selected), and the delegate type of the event is MenuHandler. The statement of the event is completed, and the registration event is started.

three. Registration issue

Here first, Publisher / Subscriber mode is told, because the event is inherited from this mode. The sender of a message is called Publisher, and the receiver of the message is called Subscriber, subscriber (subscriber) is registered at the Publisher (Publisher), and Publisher changes, Notify Subscriber to do proper operation, very similar to Observer in design mode.

The program we believe that the registration is the subscriber. The event is equivalent to the publisher, the following programs indicate how to connect the subscriber to the publisher: use system;

Public class delegateAndevents // Define the class of a registration event

{

Public static void int main ()

{

SiteManager SM = New SiteManager (); // SiteManager is a site management class

MenuItem addMenu = New MenuItem ("add") // Defines a new event class

Menuitem Delmenu = New Menuitem ("delete") / / Define another new event class

// Register in the event by delegate, SM class's Addsite method

AddMenu.MenuSelction = New MenuHandler (Sm .Addsite)

// Sign up to the event by delegate, SM class deletesite method

Delmenu.MenuSelction = New MenuHandler (SM. Deletes)

}

}

From the line of registration event, the event is like a field (attribute) of the event class, but it is very restricted to its access, which can only be followed: write new delegates on this field ( =) Or remove the commission (- =) from the field (may be a compound field),

Remove registration event: Delmenu.MenuSelction- = new menuHandler (SM. Deletes)

four. Realize event

Here, it is necessary to talk about the SiteManager class in the previous example, which contains the method of the event needs to be implemented, and the method of the SiteManager class must meet the type of commissioned type and return type, which is added to the event. The method can be delegated in this way.

Using system;

Public Class SiteManger // Site Management Class

{

Public siteManger () // Constructor initialization variable

{

// Implelement Something

}

Public void addsite () // method is in line with the type of commission

{

// impleElement executes the operation code of adding site

}

Public void deleteSite () // method should comply with the type of commission

{

// Implelement Executes the operation code of the delete site

}

}

The method of the SiteManger class should comply with the principal type so that his method can be used as an event.

V. Trigger events

When an event is called, it can be said when it is said. The event is triggered from the defined class

Example

Using system;

Public class menu // Defines a menu class

{

Public Menu (Strng Title) // Class constructor

{

//Initialize variables

}

Public void run () // The process of triggering events

{

// Declare a menu project object (previously defined in front of MenuiteM)

Menuitem mymenuitem = new menuitem}

MyMenuItem.fire // Call the Fire method to trigger the event, the event executes the SiteManger class

}

I have completed the implementation of the entire event so far. six. Add an "Add / Delete" method for events

If you need to register an event by a large number of methods, you can add add, remove methods for events. Register by accessing an "Add / Delete Method"

Example:

Using Sytem;

Pulic Delete Ovid MenuHandler (Object Sender, Eventargs E); // Example 1 Deformation

Public Class MenuItem

{

INT NUMBEROF EVENTS;

String tex;

Private menuHandler MH = NULL; / / I have a commission

Public Event MenuHandler Menuseelecton

{

Add // Add "Add" method

{

MH = Value;

Nuberofevents ;

}

Remove

{

MH- = Value;

Numberofevents -;

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-30367.html

New Post(0)