Region in a non-singleton view (non-root view)

Topics: Prism v4 - WPF 4
Jul 4, 2011 at 7:47 AM


I have a view which may be created on request during application lifetime. This view has 2 regions defined inside it, each region contains a subview. If I make this view a singleton (3rd parameter to true), it works alright. However if I make it non-singleton, it will give errors. 

Region is not supposed to be created in non-root view level?

        protected override void ConfigureContainer(){
            RegisterTypeIfMissing(typeof(IView), typeof(View), false);
            RegisterTypeIfMissing(typeof(ISubViewA), typeof(SubViewA), false);
            RegisterTypeIfMissing(typeof(ISubViewB), typeof(SubViewB), false);

inner most exception msg: {"Region with the given name is already registered: RegionA"}
inner most Stacktrace:
at Microsoft.Practices.Prism.Regions.RegionManager.RegionCollection.Add(IRegion region)   
at Microsoft.Practices.Prism.Regions.Behaviors.RegionManagerRegistrationBehavior.TryRegisterRegion()   
at Microsoft.Practices.Prism.Regions.Behaviors.RegionManagerRegistrationBehavior.StartMonitoringRegionManager()   
at Microsoft.Practices.Prism.Regions.Behaviors.RegionManagerRegistrationBehavior.OnAttach()   
at Microsoft.Practices.Prism.Regions.RegionBehavior.Attach()   
at Microsoft.Practices.Prism.Regions.RegionBehaviorCollection.Add(String key, IRegionBehavior regionBehavior)   
at Microsoft.Practices.Prism.Regions.RegionAdapterBase`1.AttachDefaultBehaviors(IRegion region, T regionTarget)   
at Microsoft.Practices.Prism.Regions.RegionAdapterBase`1.Initialize(T regionTarget, String regionName)   
at Microsoft.Practices.Prism.Regions.RegionAdapterBase`1.Microsoft.Practices.Prism.Regions.IRegionAdapter.Initialize(Object regionTarget, String regionName)   
at Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior.CreateRegion(DependencyObject targetElement, String regionName)
Jul 4, 2011 at 3:42 PM


The RegionManager does not allow to add regions with the same name. Therefore, you should identify your regions with unique names.

If you'd like to show a view twice, you could use Scoped Regions. By using scoped regions, when you add a view to a region, you create a scoped region manager for the regions that lie inside that view. Therefore, if you create multiple instances of a view that holds a region inside, multiple instances of that region (with the same name) are generated, but inside separate region managers.

You could read more about Scoped Regions in Prism MSDN documentation here.

I hope you find this information useful.


Miguel Bronzovic