The first thing to start touching is when the command is Webfrom, the time to drag the control, and then directly write the click event, which may be preconceived thought,
Coupled with the use of events too little, resulting in contact with the command mode, CQRS, publish subscription mode when the event this piece is not understood,
So deepen your understanding, here we use the console to simulate a button
1 Public classButton2 {3 //to define a delegate that invokes an event4 Public Delegate voidButtondelegate (stringe);5 6 //button stand-alone event7 Private Eventbuttondelegate Buttionclick;8 9 //Adding and removing eventsTen Public Eventbuttondelegate click_event One { AAdd { This. Buttionclick + =value;} -Remove { This. Buttionclick-=value;} - } the - //Triggering Events - Public voidRaiseclick (stringargs) - { +Buttionclick?. Invoke (args); - } +}
The event is also a member of the class, he is not the same as the property, and the action event is via add and remove
1 class Program2 {3 Static voidMain (string[] args)4 {5Button B =NewButton ();6B.click_event + =Newbutton.buttondelegate (button_click);7B.click_event + =Newbutton.buttondelegate (BUTTON_CLICK1);8 9B.raiseclick ("Hello");Ten One console.readline (); A } - - Private Static voidButton_Click (stringargs) the { - Console.WriteLine (args); - } - + Private Static voidButton_click1 (stringargs) - { + Console.WriteLine (args); A }
Here's a brief description of how the event is defined, and the next step is to make a simple publish subscription.
. NET Events Mimic button buttons