View Discovery - Can you have > 1 region with the same name?

Topics: Prism v2 - WPF 3.5
Dec 9, 2009 at 11:22 AM

In my application, I sometimes need to show the same view in multiple places on screen at once. I'm using View Discovery (RegisterViewWithRegion) for some of those views. Up until now it has been working OK, but now I'm starting to see exceptions from the RegionManager about adding >1 region with the same name.

It looks like the only reason it has been working is that the RegionManagerRegistrationBehaviour was usually failing to find the RegionManager, so the view discovery worked but the actual region was never added to the RegionManager.

Is this supposed to work? I get the feeling it isn't. I can see how having two regions with the same name would be problematic for View Injection.

If not: It seems like the best way around this would be to divorce the View Discovery from the existing RegionManager entirely. I.e. remove the view discovery from the RegionManager, and have a ViewManager class that worked like the RegionManager, but only did View Discovery (not View Injection).

Does that sound like a good idea? I've not looked to see how much work it would be, yet.


Dec 9, 2009 at 2:19 PM

Hi James,

About adding > 1 region with the same name

A RegionManager cannot have more than one region with the same name. However, scoped regions allow you to have multiple regions with the same name. You can read more about that here.

About showing the same view in multiple places on screen at once

If you want to show the same view instance, you should make sure that the delegate in charge of returning the view always returns the same instance. If you want to show the same type of view, then there is no problem with having it in more than one Region. Also take into account that a region cannot hold more than one view with the same name.

Please let me know if this helps.

Damian Schenkelman

Dec 9, 2009 at 4:00 PM

Thanks, Damien.

I thought it looked like we were limited to 1 region with a particular name at once.

What I've done to workaround this is create a new attached property called "ContentName" with the following property change handler:

        private static void OnContentNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            string contentName = (string)e.NewValue;
            RegionManager.SetRegionName(d, String.Format("{0}|{1}", contentName, Guid.NewGuid()));

I.e. It sets the RegionName to the ContentName + "|" + a GUID.

I've then hooked in my own AutoPopulateRegionBehavior that only takes the part of the region name before the first "|" when it looks up the view.

That gets around the unique region name constraint, whilst still letting the View Discovery stuff work pretty much as before.