Several RegionManagers

Topics: Prism v1, Prism v2 - Silverlight 2, Prism v2 - Silverlight 3, Prism v2 - WPF 3.5
Mar 17, 2010 at 10:06 AM

Hello! I'd like to know is following is possible:

My app can create several windows of same type. So, in order to child views to be injected correctly we need to have individual instance of RegionManager per window. Because as I suppose, if we would have one regionmanager, it cannot distinct regions with the same name of different windows. Additionally this problem forces us somehow to inject in child views particular region manager instance, that control particular window, i.e. we cannot have reginmanager registered globally.

Any thoughts appreciated! Thanks in advance!

Developer
Mar 19, 2010 at 7:37 PM
Edited Mar 19, 2010 at 7:40 PM

Hi,

The scenario that you are describing is possible, you could use Scoped Regions in order to achieve it. You can find information about Scoped Regions in this document.

You could register the additional RegionManagers in the Container. A possible approach to have multiple RegionManager instances registered in the Container at the same time could be achieved like this:

IRegionManager additionalRegionManager = this.regionManager.Regions["RegionInASeparateWindow"].Add(new ViewA(), null, true); 
this.container.RegisterInstance<IRegionManager>("AdditionalRegionManager", additionalRegionManager);

The above code creates a scoped RegionManager instance for the view that has been added to the “RegionInASeparateWindow” region, and then registers that instance as a named instance of IRegionManager (you can read more about how that is done here). Thus, you can resolve the main RegionManager by calling

this.container.Resolve<IRegionManager>();

and the additional RegionManagers that you create for extra windows by calling

this.container.Resolve<IRegionManager>(“ScopedRegionManagerName”);

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Mar 24, 2010 at 11:05 AM

Hi

Thank you for your info I am using the exact same scenario, but I have a problem with tering down scoped regionmanager.

I resolve all regionmanagers registered and try to tear them down. ResolveAll method gets all of them but it doesn't get them disposed. What could be the reason?

Code:

 

 For Each regionmanager As IRegionManager In _container.ResolveAll(Of IRegionManager)()
            If regionmanager Is _regionmanager Then
            Else
                For Each region As IRegion In regionmanager.Regions
                    For Each view As Object In New List(Of Object)(region.Views)
                        region.Remove(view)
                    Next view
                Next
                _container.Teardown(regionmanager)
            End If
        Next

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

            If regionmanager Is _regionmanager Then

            Else

                For Each region As IRegion In regionmanager.Regions

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

                        region.Remove(view)

                    Next view

                Next

                _container.Teardown(regionmanager)

 End If

 Next

 

Jun 24, 2010 at 9:33 AM

Please can anyone explain me how to dispose registered scoped regionmanagers that are registered with ContainerControlledLifetimeManagers?