I am working on a WPF project, using MVVM and Microsoft Prism libraries. So, when I need to communicate through classes I use the class Microsoft.Practices.Prism.MefExtensions.Events.MefEventAggregator and I publish events and subscribe methods as below:
To publish:
myEventAggregator.GetEvent<MyEvent>().Publish(myParams)
To Subscribe:
myEventAggregator.GetEvent<MyEvent>().Subscribe(MySubscribedMethod)
But my question is: Is there a way to return some data from the “Subscribed method” after publishing an event??
As far as I know, if all the event subscribers are using the
ThreadOption.PublisherThreadoption (which is also the default), the event is performed synchronously and the subscribers can modify theEventArgsobject, so you could have in the publisherThe subscriber code would look like this:
If you know that the event should always be called synchronously, you can create your own base class for events (instead of
CompositePresentationEvent<T>) which overrides theSubscribemethod, and only allow subscribers to use theThreadOption.PublisherThreadoption. It would look something like this:then instead of deriving
MyEventfromCompositePresentationEvent, you derive it fromSynchronousEvent, which will guarantee you that the event will be called synchronously and that you will get the modifiedEventArgs.