Best way to manage views in a region

May 18, 2009 at 8:51 PM

I am writing a prism based application and I need to be able to display different types of views in a common region [eg. MainRegion] based on menu selection. What is the best way to manage multiple views in a region?

- Remove all existing views and add an instance of the newly selected view, Or

- Keep all the views, and just activate the one you want to show, Or

- use a LifeTimeManager to handle when a view is garbage Collected.

If anyone can share an insight into this issue, I would appreciate it a lot.




May 18, 2009 at 11:30 PM

Hi Ravi,


Regarding showing view based on menus, there are some discussions that you might find interesting:

·         ContentControl in MainRegion

·         Menu in a module

·         Module Navigation With MenuItem Click?   

·         Region and ContextMenu


Regarding the best way to manage multiple views in a region, it will depend on your specific scenario. The most general policy is trying to reuse already created views (to avoid creation overheads). So if you are deactivating a view that won't be used in the future, then you should remove it from the region (to free up resources). If the view will be activated sometime in the future, then it might be worth keeping it in the region.

Activation and deactivation when a view is added is managed by the region itself: depending of what type of container the region is adapting, when a view is added it automatically activates/deactivates the needed views that the region contains.


You might read more on this in the following articles from the documentation:

·         How to: Add a Region

·         UI Composition


Hope it helps!


Matias Bonaventura

May 19, 2009 at 6:32 PM


Thanks for pointing in the right direction. I found out I can re-use the views in certain situations and would need to remove the views after they are inactive in certain scenarios.

Just wondering about one more thing though - I am using MVVM pattern for the views and am wondering when you remove a view does it also send the corresponding view model to be garbage collected as well or we need to handle that explicitly.

Thanks again. You inputs were very helpful

- Ravi

May 20, 2009 at 8:02 PM

Hi Ravi,


As long as the View and Presenters are not referenced by any other object, they will be garbage collected automatically. Regions only keep a reference to the views they contain, so removing the view from the region would be enough from that side.

You should be careful not to keep references to the view and presenter from other objects. This is generally not a problem, but for example, a common way of keeping unwanted references alive (as they are a bit hidden) is with delegates and events that keep the subscriber reference alive.


Hope it helps!


Matias Bonaventura