Search code examples
wpfeventsprismeventaggregator

How can I differentiate the caller of the Prism's events?


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.


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 });