Registering view

Topics: Prism v2 - WPF 3.5
May 1, 2009 at 7:42 PM
Edited May 2, 2009 at 8:47 PM
Hi all,

I encounter a strange problem registering a view in MainRegion . This is the case :

In Module B Initalize method , I subsribe to event for registering ModuleView in the MainRegion . Then in Module A when something happen I publish an event to show the view. Here is the code :


s =>
if (s.Topic == EventTopics.ShowDashBoard)
      DashBoradView dashBoradView View = new DashBoradView ();
RegionNames.MainRegion, () => dashBoradView );
RegionNames.MainRegion].Activate(dashBoradView );

The subscribtion doesn't work this way. I tried to write down a simple code instead in the subscribtion say Debug.Write(s.Topic) only for test to see what the problem then the subscribtion works. When I changed the follwong code :

dashBoradView View = new DashBoradView ();
RegionManager.RegisterViewWithRegion(RegionNames.MainRegion, () => dashBoradView );

s =>
if (s.Topic == EventTopics.ShowDashBoard)
           RegionManager.Regions[RegionNames.MainRegion].Activate(dashBoradView );

Then evey thing is fine.
Is it a problem to put the registration to the view inside the event aggregator action.




May 3, 2009 at 2:56 PM
I'm not entirely clear when your Publishing of the event occurs but if I were to guess I'd say that the most likely reason this isn't working as you expect is because the Publish is called after the Subscribe. In this case the event aggregator will loose the event. Can you step through your code and see if Publish is being called before the view is registered and subscribes to the event.

May 3, 2009 at 7:08 PM
Thank you for the reply,

1. The event is published by button clicking. So , it's sure the subscription comes after the publish.
2. The same code is working like the second example I supplied , only the first one example doesn't work.

The important thing it's working for me now although in the second way, but I was so surprised about the problem.

May 4, 2009 at 7:48 PM



I have been able to reproduce your situation and found the cause for it. When you use the first approach, you are instantiating your view inside the event handler. Thus, you are not creating a view unless the event is fired.

However, in the second approach you create the view whether the event is handled or not. By doing that you are preventing the subscriber in the module initializer from being Garbage Collected, which is the reason the event handler is executed.


The Composite Application Library was designed with this scenario in mind. What you can do to use the first approach is keep a reference of the subscriber. You can read more about this here. This could be memory leak prone, so you should unsubscribe when you don’t need the handler to be executed any more. The Event Aggregation Quickstart shows how to unsubscribe and subscribe to events.


The following blog post also talks about a  similar situation:

·         Prism Event Aggregator Subscription Blues


Please let me know if this helps.


Damian Schenkelman