How to return data from a signed method using EventAggregator and Microsoft Prism libraries

I am working on a WPF project using MVVM and Microsoft Prism libraries. Therefore, when I need to communicate through classes, I use a class Microsoft.Practices.Prism.MefExtensions.Events.MefEventAggregatorand I post events and sign methods, as shown below:

Publish:

myEventAggregator.GetEvent<MyEvent>().Publish(myParams)

Subscribe:

myEventAggregator.GetEvent<MyEvent>().Subscribe(MySubscribedMethod)

But my question is: Is there a way to return some data from the "Subscription" after the publication of the event

+5
source share
1 answer

As far as I know, if all event subscribers use the parameter ThreadOption.PublisherThread(which is also the default), the event is executed synchronously and the subscribers can modify the object EventArgsso that you could have a publisher

myEventAggregator.GetEvent<MyEvent>().Publish(myParams)
if (myParams.MyProperty)
{
   // Do something
}

:

// Either of these is fine.
myEventAggregator.GetEvent<MyEvent>().Subscribe(MySubscribedMethod)
myEventAggregator.GetEvent<MyEvent>().Subscribe(MySubscribedMethod, ThreadOption.PublisherThread)

private void MySubscribedMethod(MyEventArgs e)
{
    // Modify event args
    e.MyProperty = true;
}

, , ( CompositePresentationEvent<T>), Subscribe ThreadOption.PublisherThread, :

public class SynchronousEvent<TPayload> : CompositePresentationEvent<TPayload>
{
    public override SubscriptionToken Subscribe(Action<TPayload> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate<TPayload> filter)
    {
        // Don't allow subscribers to use any option other than the PublisherThread option.
        if (threadOption != ThreadOption.PublisherThread)
        {
            throw new InvalidOperationException();
        }

        // Perform the subscription.
        return base.Subscribe(action, threadOption, keepSubscriberReferenceAlive, filter);
    }
}

, MyEvent CompositePresentationEvent, SynchronousEvent, , EventArgs.

+12

All Articles