IRegionManager.Regions.Count ZERO

Topics: Prism v4 - WPF 4
Mar 27, 2012 at 3:57 PM

I have a bunch of views which are displayed via

    [ViewExport(RegionName = RegionName.NavBar)]
    public partial class MyNavBar : UserControl
    { ///

etc. and they work fine

However subsequently after the views are displayed and on some user action, i check on the IRegionManager declared as follows  ( via MEF )

     [IMPORT] IRegionManager rmgr;



returns ZERO

which throws my logic totally off  - checking if the view already exists and if not create and display it

any clues ?

FYI - i am not using any locally scoped region managers, it's pretty vanilla usage

Mar 27, 2012 at 8:54 PM
Edited Mar 27, 2012 at 9:00 PM


So far, we couldn't reproduce the behavior you are mentioning. We tried using ViewExport attribute with MEF and importing the RegionManager as you mentioned but we obtain the regions count from the RegionManager as expected.

Perhaps, you could provide us with a little more information about your scenario or a repro sample application so we can help you further with this.


Agustin Adami

Mar 28, 2012 at 7:54 PM
Edited Mar 28, 2012 at 7:55 PM

Hi Agustin

tried the following to no avail

[Import] IRegionManager rmgr;
[Import] CompositionContainer container;

void changeView<T>(int arg)
IRegionManager rm1 = container.GetExportedValue<IRegionManager>();
IRegionManager rm2 = ServiceLocator.Current.GetInstance<IRegionManager>();
var cnt1 = rm1.Regions.Count();
var cnt2 = rm2.Regions.Count();
var cnt3 = rmgr.Regions.Count();

// cnt1, cnt2, cnt3 are all ZERO ????

        T vw
if (!rmgr.TryGetViewByType(typeof(T), out vw))
= ServiceLocator.Current.GetInstance<T>(); // this also fails with Activation error Key = ''
.AddToRegion("MyMainRegion", vw);


the code above is called after the app is up and running, and the views are displayed on the screen, regionmanager is registered and available in the class,

the real Q is why the regions.count = ZERO even though i can see the views on the screen ???

Mar 28, 2012 at 9:41 PM


I believe that this could be related to a known issue in Prism where, if a region is defined inside a DataTemplate, the region and all of its child regions will not be registered in the RegionManager. For example, if a shell has its "MainRegion" inside a DataTemplate, this region and any other region inside it will not be registered in the RegionManager. In this scenario the RegionManager would be empty while the ViewExport and its AutoPopulateExportedViewsBehavior would work, as they don't require a RegionManager.

You can find more information about this issue as well as several workarounds for it in the following work item:

Also, this could be related to another known issue where if the AutoPopulateExportedViewsBehavior or the AutoPopulateRegionBehavior is registered before the RegionManagerRegistrationBehavior, the regions are never registered in the RegionManager. In order to avoid this, you could invoke the base implementation of the ConfigureDefaultRegionBehaviors method before registered the AutoPopulateExportedViewsBehavior in your bootstrapper. You can find more information in the following work item:

If none of those are your scenario, it would be helpful if you could provide us with a repro-sample application portraying this problem, so that we can help you further with this. You can upload your sample-application to SkyDrive or any other file hosting service of your preference.

I hope you find this useful,

Damian Cherubini

Mar 29, 2012 at 6:23 PM

Hi Damian


Some more tests and it turns out to be related to the Adapters somehow and the way the regions are nested

FYI - I'm using DevX controls

The AppShell define a region called MainShellRegion

There's a shell module which defines regions called MainRegion ( devx DocumentGroup control ), NavBarRegion( LayoutPanel control) and other modules which render into these views and are unaware of MainShellRegion

The problem where IREgionManager.Regions.Count occurs with this setup


Now if i change the AppShell to directly include MainRegion, NavBarRegion, RightRegion etc.

then the code above works fine and IREgionManager.Regions.Count returns TWO as it should


so it appears to be related to the DocumentGroupAdapter and/or LayoutPanelAdapter from here

I hope this is a known issue else I'll create a repro app and post it on their support channel