Scoped Region Manager

Topics: Prism v4 - WPF 4
Jun 4, 2013 at 5:49 PM
Hi All,

I have an Infragistics TabGroupPane and I am using scoped regions (using Prism 4.1 with MEF) to display the same TabItemView (which has multiple regions in it) in different tabitems.

-My TabItemView, with nested regions, use ClearChildViewsRegionBehavior.ClearChildViews set to true.
-TabGroupPane is defined as a usercontrol which resides in a region named "TAB_REGION".
-Whenever I add a TabItem to the TabGroupPane, I create a new scoped regionmanger so that regions within the TabItemView are not registered to the main region manager using the following code:

scopedRegionManager = regionManager.Regions["TAB_REGION"].Add(tabItemView, null, true);
-I am not registering this scopedRegionManager with the MEF container, neither it is referenced by any part of the application.
-After adding 5 tabItems, I have 6 views in my "TAB_REGION".

-When I remove a TabItem, I also remove the TabItemView from "TAB_REGION". I have to do a UpdateRegion() and I can see that the TabItemView is removed from "TAB_REGION".

But I do have following questions/concerns:
  1. Where are the scopedregionmanagers stored? Is there a collection of regionmanagers similar to one we have for Regions?
  2. How can I access a scoped regionmanager if I want to in any part of the application?
  3. How and when the scoped regionmanager is destroyed/disposed? And how can I ensure that the scoped regionmanager is disposed?
My main aim is to ensure that any scopedRegionManager created (whether or not assigned to a variable) is duly disposed.

Jun 4, 2013 at 7:53 PM

First of all, when you inject a view with a scoped RegionManager (using the Add method as you mentioned above,) the newly created RegionManager is stored inside an attached property put in the injected view. For example, if you inject the tabItemView with an scoped RegionManager, said RegionManager will be put in a "RegionManager" attached property in the tabItemView. Also, in Prism, all regions have a reference to their corresponding RegionManager (if any,) so when the scoped regions in the tabItemView are created they will have a reference to the scoped RegionManager too.

There is no specific method to access the scoped RegionManagers created in your application. If you have a Region that is registered in that RegionManager you can obtain it from its RegionManager property. If you have a reference to the view, you can obtain it from its attached property using the GetRegionManager method:
IRegionManager rm = RegionManager.GetRegionManager(tabItemView);
Also, if you wish to access the scoped RegionManager in the view models that are injected in the scoped regions, I believe you could find the RegionManagerAwareBehavior included in the following blog post useful:
Finally, the last question is related to how the memory is managed in a .NET application. Basically, when there is no strong reference that keep the RegionManager alive, it will be marked to be garbage-collected. However, that RegionManager will only be destroyed when when the garbage-collector is run.

I hope this helps,

Damian Cherubini
Jun 4, 2013 at 8:47 PM
Thanks a bunch Damian.

This is really helpful!