How to find the number of views that are open for a perticular module.

Topics: Prism v4 - WPF 4
Jul 17, 2012 at 11:47 PM


I have a WPF/PRISM application which uses Non-Linear navigation.  To enable that functionality, I need to be able to find a way to determine the number of open views for each module within the same module. 

Assume, I have an order module, if I open order details for order #1 then I should have a count of 1 next to the menu icon of that module, if I open order details for order #2 I should have a count of 2, so forth and so on.  The user can close the module which means I have to decrement the count.

What is the best way to do that???


Jul 18, 2012 at 9:34 PM


How to implement the functionality will mainly depend on your personal preferences and the requirements of your scenario.

In my opinion, a possible approach could be to implement a shared service (lets call it "ViewCountService") which could simply hold a Dictionary of string - int and subscribe to events through the EventAggregator.

When a view is created you can inject the EventAggregator and then:

  • When a view is loaded, publish an event (for example ViewOpened event) with a string payload used to identify the module. (You can know when a view is loaded through its Loaded event.)
  • When a view is unloaded, publish an event (for example ViewClosed event) with the aforementioned string. (You can know when a view is unloaded through its Unloaded event.)

The ViewCountService would subscribe to both events (through the EventAggregator) and increase / decrease the count associated with the string "identifier," which in this case would identify the module. Then, when you need to know the amount of opened views, you could simply consume this service.

You can find more information about shared services and the EventAggregator in the following chapter of the Prism documentation:

I hope you find this useful,

Damian Cherubini