Question

I am using Prism's Event Aggregator and I publish an event from my composite control. But if a developer uses two instances of the control on the same form, how can a subscriber differentiate the events? What is the best practice?

Thank you.

Was it helpful?

Solution

Typically I just pass the caller or a callerId in the EventMessage, and the subscriber ignores the message if the caller isn't what it expects

// Subscribe
eventAggregator.GetEvent<SomeEvent>().Subscribe(SomeMethod);
public void ShowNews(SomeEventMessage e)
{
    if (e.CallerId != this.Id)
        return;

    Do Work();
}

// Broadcast
eventAggregator.GetEvent<SomeEvent>().Publish(
    new SomeEventMessage { CallerId = this.Id });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top