Disposing scopedregionmanager

Topics: Prism v2 - WPF 4
Jun 24, 2010 at 9:26 AM


I am having some dificulties regarding my application design.

I have a module that is resposible for creating scopedregionmanagers based on the outside xaml in which i define regions. When I inject those outside xaml-s i create scoped regionmanagers and then i register them in the container:

 detailregionmanager = regionmanager1.Regions("MainRegion").Add(model.View, "ScopeRegion", True)

_container.RegisterInstance(Of IRegionManager)("ScopeRegionManager", detailregionmanager)


Then in other modules which inject their views in scope regions I resolve scopedregionmanager:

 Dim regionmanager As IRegionManager

regionmanager = _container.Resolve(Of IRegionManager)("ScopeRegionManager“)

 Is this OK?

Problem is when I want to dispose existing scopedregionmanagers and everything in the MainRegion, I dont know how to do it. I want to dispose all injected views, regionmanagers, and show Starting view in the MainRegion where user choses another ''application''.

I try this:


 For Each regionmanagerr As IRegionManager In _container.ResolveAll(Of IRegionManager)()

           If regionmanagerr Is _regionmanager Then


               For Each region As IRegion In regionmanagerr.Regions

                   For Each view As Object In New List(Of Object)(region.Views)


                   Next view



           End If


 Views(0) is the starting view where user choses applications, and the selected application loads into Views(1) scopedregion so I only want to dispose Views(1).

Every ''application'' loads its specific xaml-s that define regions and according to them specific views are injected into that regions. When another application is loaded (or the same application second time) i get the error in the line:

 Object reference not set to an instance of an object.

 detailregionmanager = regionmanager1.Regions("MainRegion").Add(model.View, "ScopeRegion", True)

 Probabaly becose that scopedregionmanager is already in the container. Please what am I doing wrong? Should I use childcontainer and register all instances in it and at the end dispose that childcontainer?


Jun 28, 2010 at 9:49 PM
Edited Jun 28, 2010 at 9:50 PM


I’ve created a sample which shows some of the concepts from you question. You can download the sample from here (it has been developed in c#).

Based on our understanding the you are registering the IRegionManager (scoped region) into the container for adding some views from another module, but this is generally not the recommended approach. I identified two challenges to tackle your scenario:

  1. How to register views through modules using the same scoped region
  2. How to remove all views to get the main region cleaned (optionally it could contain a default view).

 1. How to register a view into a scoped region created in another module

In Prism with “view discovery” (more details here), a module can register views inside a particular region by indicating its name. When this region is displayed at run time, any views that have been registered for that region will be automatically created and displayed within it.

Therefore, assuming that this region could be a scoped one, if you use IRegionViewRegistry for doing “view discovery” you could avoid registering your scoped region in the container. The following code shows how it looks (for more info you can see HelloWorldModule2 constructor in the shared sample):

public HelloWorldModule2(IRegionViewRegistry registry)
   registry.RegisterViewWithRegion("ScopedRegion", typeof(Views.HelloWorldView2));

2. How to dispose the views

Regarding how to dispose the views, the solution in the sample is similar to your approach, but you will only need to remove the mainView from the MainRegion.

As your views are registered into a scoped region, when you remove the mainView, all other view will be also removed. If need to notify other modules when the main view is removed, you can use the Event Aggregator as shown in the sample.

The code is something like this (for more info you can see the HelloWorldModule constructor in the shared sample):

public HelloWorldModule(IRegionManager manager, IEventAggregator eventAggregator)
    this.mainRegion = manager.Regions["MainRegion"];
    eventAggregator.GetEvent<RemoveAllViewsClickedEvent>().Subscribe((o) =>
        }, true

Please let me know if this helps.

Fernando Antivero

Jun 29, 2010 at 2:10 PM

Thank you for your reply but what when I register two regions with the same name? How will the view from another module know where to go?

For the disposing I will try your approach with eventaggregator.

Oct 8, 2010 at 6:24 PM
Edited Oct 8, 2010 at 6:24 PM


If you have two regions with the same name (in separate RegionManagers, using Scoped Regions) and you register a view in the RegionViewRegistry (that is, using the View Discovery approach), the view will get added to both of them.

If you need to decide which region the view will be added to, without using View Injection, you should name the regions differently.

Hope this helps.

Fernando Antivero