Prism EventAggregator Callback to the publisher

Topics: Prism v4 - WPF 4
Mar 5, 2013 at 6:19 PM
Is it possible to have a call back to the publisher using PrismEventAggregator. Here is the example scenario:
•MainPage
•LeftControl
•RightControl

The LeftControl publishes an Event and RightControl subcribes to act upon the event.

Upon completion of the action in the right control, I would like to execute another function to refresh the left control. For this, can I pass an Action in the EventAggregator event as a callback? How do I do it?
Mar 6, 2013 at 4:00 PM
Hi,

Based on my understanding of your question, I believe there are two possible approaches for your scenario:

1) Create a new CompositePresentationEvent and subscribe your LeftControl to it to handle the refresh. Then, after the RightControl finishes handling the first event, it could publish a second one with the EventAggregator as a callback.

2) Create a new class with both the parameter you are passing with your CompositePresentationEvent and a callback event:
public class MyClass
{
    public ParameterType parameter;
    public event MyCallbackEvent callback;
}
Then, in your LeftControl, you could create an instance of this class, where you would set the parameter to be passed and subscribe to the callback event. After publishing the CompositePresentationEvent with this instance as the payload, the subscribed RightControl would receive the instance in its handler. So, after handling the published event, the RightControl could simply raise the callback event passed as a method of executing a callback in the LeftControl :
// In LeftControl
MyClass c = new MyClass();
c.parameter = // The original parameter
c.callback += // The method of the LeftControl that will be invoked as the callback
this.eventAggregator.GetEvent<MyCompositeEvent>().Publish(c);

// In RightControl
public void EventHandler(MyClass c)
{
    // Do work...
    c.callback.Invoke();
}
Hope this helps,

Federico Martinez
http://blogs.southworks.net/fmartinez