Eventaggregator event subscription and release in Prism

Source: Internet
Author: User

Eventaggregator is a tool for managing events easily.

1. Define a class that inherits from Compositepresentationevent<t>. This class is used to define different events, their parameters, and how they are handled

 Public class getinputmessages:compositepresentationevent<string>{}

2. Create an instance of Eventagregator and ensure that the instance is unique to manage events

     Public classEventaggregatorrepository { Publiceventaggregatorrepository () {Eventaggregator=NewEventaggregator (); }         PublicIeventaggregator Eventaggregator;  Public StaticEventaggregatorrepository eventrepository =NULL; //Singleton, keeping memory unique instances         Public Staticeventaggregatorrepository getinstance () {if(Eventrepository = =NULL) {eventrepository=Neweventaggregatorrepository (); }            returneventrepository; }    }
Create Eventaggregator

3. Subscribe to Events (define how events are handled)

Eventaggregatorrepository.getinstance () .eventaggregator.getevent<getinputmessages> (). Subscribe (Receivemessage,threadoption.uithread,true); // Event Handling Methods  Public void ReceiveMessage (string  messagedata) {     this. txtresult.text = messagedata;} 
Subscribe Events

4. Publish events (invoke event handling)

Eventaggregatorrepository.getinstance () .eventaggregator.getevent<getinputmessages> (). Publish (MessageData);
Publish Events

The implementation mechanism and the delegate (Delegate) are very similar, and the individual believes that such eventaggregator exist only to make the logic clearer. His advantage is that if the processing method is consistent, you can trigger such an event on multiple pages

Eventaggregator event subscription and release in Prism

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.