Scoped EventAggregator

Topics: Prism v4 - WPF 4
Aug 8, 2013 at 2:42 PM
Is it possible to have an EventAggregator scoped to a Region ?
A bit like the scoped RegionManager.

Aug 8, 2013 at 5:22 PM

As far as I know, I´m afraid that feature is not supported in Prism. Nevertheless, what you can do instead is to define a Child Container, create a new EventAggregator, and then register it in that Child Container, as this is equivalent to have a scoped EventAggregator. However, this is not a straightforward solution as the Child Container should be seen as an extension of the main container and this may generate undesired effects depending on how your solution is designed.

Therefore, it would be helpful if you could explain us why do you need this scoped EventAggregator and how your application is designed so we can further analyze your scenario to give you a better approach.


Federico Martinez
Aug 20, 2013 at 6:32 AM
Thanks for the answer.
I'm using the scoped Region implementation of Agustín Adami.
regionManager.RequestNavigate(RegionNames.MainRegion, new Uri("MainWorkspaceView" + query.ToString(), UriKind.Relative),
            (NavigationResult nr) =>
                 if (nr.Error != null)
                     throw nr.Error;
                        var myRegionManager = nr.ExtractRegionManager();
                        myRegionManager.RequestNavigate("Header", new Uri("MainWorkspaceHeaderView", UriKind.Relative));
                        myRegionManager.RequestNavigate("Menu", new Uri("SearchMenuView", UriKind.Relative));
I'd like to have 4 different EventAggregators :
  1. The usual EventAggregator (global message)
  2. An EA that would send a message to the scoped region (MainWorkspaceView) + all sub regions (Header, Menu)
  3. An EA that would send a message to a specific region within the scoped region.
  4. An EA that would send a message to a specific view in a region.
That makes a lot of different EA.
Aug 20, 2013 at 2:23 PM

Based on your scenario, I believe that what you want is to raise an equal event on a scoped EventAggregator so it doesn't affect other Views globally. If that is the case, it would be better for you to change your approach of implementing several EventAggregators (since it is much more complicated to implement) for a simpler one: subscription filtering. This filtering lets you decide if the handler of the subscriber gets executed based on the payload from the event. The following is an example where the Filter parameter (the last parameter of the subscription), every time the event is published, determines if the payload of the event matches a criteria to decide if the subscriber callback get executed or not:
FundAddedEvent fundAddedEvent = this.eventAggregator.GetEvent<FundAddedEvent>();

fundAddedEvent.Subscribe(FundAddedEventHandler, ThreadOption.UIThread, false, fundOrder => fundOrder.CustomerId == this.customerId);
This way you won't need more EventAggregators than the one provided by your container and, when you raise an event (global message), you will be able to decide which of the subscribers of that event will handle it and which not.

For more information on how to implement subscription filtering, you can refer to the following section of the Prism documentation:

Hope this helps,

Federico Martinez