Example explanation of events and weak events

Source: Internet
Author: User

1 usingSystem;2 usingSystem.Windows;//Implementing a weak event requires referencing the WindowsBase assembly3 4 namespaceexample explanation of events and weak events5 {6     class Program7     {8         Static voidMain (string[] args)9         {Ten             vardealer =NewCardealer ();//This class contains an event handler, and a method for invoking it One  A             varMichael =NewConsumer ("Michael");//This class contains the name of a person and a method for responding to events, with the event argument type Carinfoeventargs -  -Weakeventmanager<cardealer, Carinfoeventargs> AddHandler (dealer,"Newcarinfo", Michael. Newcarishere); the             //Adding a connection registration to a static generic class of a weak event by using a static method -  -Dealer. Newcar ("Ferrari");//executing the event handler -  +             varSebastian =NewConsumer ("Sebas days"); -Weakeventmanager<cardealer, Carinfoeventargs> AddHandler (dealer,"Newcarinfo", Sebastian. Newcarishere); +  ADealer. Newcar ("Mei Soders"); at  -Weakeventmanager<cardealer, Carinfoeventargs> RemoveHandler (dealer,"Newcarinfo", Michael. Newcarishere); -             //The static method of a static generic class for a weak event removes both connection registrations -  -Dealer. Newcar ("Red Bull Competition"); -  in Console.readkey (); -         } to     } +  -     //The general idea of the event system the     //Activating Event Response Methods (2nd model, methods of receiving reminders and data) by Event reminder Delegate (event handler) (1th model, managing parameter definitions and event delegates for method registrations) *     //there should be a uniform parameter agreement between the two to pass the data, this parameter is the event parameter (3rd model, the class containing the data) $     //their entirety is invoked by an instance of the generic event handler eventhandler<teventargs>, using EventHandler (object sender, Teventargs e)Panax Notoginseng     //Sender is a publisher that passes information about the publisher, and E is the event argument that contains its custom data (usually attributes) -  the     //the idea of a weak event system (based on the event system) +     //define a mediator to receive reminders (from the event handler) and send reminders (to event response methods) to manage the registration status of both connections A     //teventargs> static method to manage registration status through static generic class weakeventmanager<teventsource of weak events the     //use static methods AddHandler and RemoveHandler to add or cancel registrations +     //Method Example: Weakeventmanager<cardealer, Carinfoeventargs>. AddHandler (dealer, "Newcarinfo", Sebastian. Newcarishere); -     //Dealer is the class instance of the event handler, and "Newcarinfo" is the identifier (event delegate name) of the event handler, Sebastian. Newcarishere is an event response method for a class instance $     //benefit: Resolves a memory leak issue.  $  -  -      Public classConsumer the     //Model 2 contains the class of the event response method, or the listener -     {Wuyi         Private stringname; the  -          PublicConsumer (stringname) Wu         { -              This. Name =name; About         } $  -  -          Public voidNewcarishere (Objectsender, Carinfoeventargs e) -         //This is an event response method, and the second parameter determines the event argument type to match the event handler for the same parameter A         { +Console.WriteLine ("{0}: New arrivals for car {1}! Event performer: {2}", name, E.car, sender. ToString ()); the         } -     } $  the      Public classCarinfoeventargs:eventargs the     //Model 3 This is an event parameter class that is used in both event handlers and event response methods to pass data in a uniform type the     //It stores the data in the Car property. the     { -          PublicCarinfoeventargs (stringcar) in         //the constructor of the event argument receives a string type parameter and stores it as a property of Car the         { the              This. Car =car; About         } the  the          Public stringCar {Get;Private Set; } the     } +  -      Public classCardealer the     //Model 1 contains the class of the event handler, or is called the publisherBayi     { the          Public EventEventhandler<carinfoeventargs>Newcarinfo; the         //This is an event reminder delegate, called the event handler, which needs to define the event argument type -  -          Public voidNewcar (stringcar) the         { theConsole.WriteLine ("Auto Supplier: New Car {0}", car); the  the raisenewcarinfo (car); - Console.WriteLine (); the         } the  the         protected Virtual voidRaisenewcarinfo (stringcar)94         //This is a common method that requires receiving a string type parameter the         //and passes the string argument to the event argument, and then executes the event through the event handler with the same event type the         { theEventhandler<carinfoeventargs> Newcarinfo =Newcarinfo;98             if(Newcarinfo! =NULL) About             { -Newcarinfo ( This,NewCarinfoeventargs (car));101             }102         }103     }104}

Example explanation of events and weak events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.