How to define Regions like in CAB

Topics: Prism v4 - WPF 4
Dec 22, 2011 at 7:15 PM
Edited Dec 22, 2011 at 9:58 PM

where the shell contained just 1 region, say MainView

A module (ModuleA) is loaded, say ShellView, which defines other regions say Center, Left, Top etc

Other modules, say ModuleB, are only aware of these regions Center, Left, Top etc and oblivious of region MainView

Does this still work, any gotchas, special considerations to keep in mind ?

Dec 22, 2011 at 9:57 PM
Edited Dec 22, 2011 at 9:58 PM

I was able to create the scenario above in a test case

Wondering if there are any gotchas ?

perhaps the order in which modules are loaded may change on a different machine which would cause ModuleB to fail to init as regions Center, Left, Top defined in ModuleA are not available as yet ?

Dec 23, 2011 at 4:34 PM


Based on my understanding, you might find the view discovery approach useful for you scenario. In this approach, when a module initializes, it registers the view in the corresponding regions (using for example, the RegisterViewWithRegion extension method of the RegionManager) so that when when the region is available, an instance of the registered view is automatically injected in the region. Like this, you shouldn't need to worry about the order in which the modules are initialized, as it's not required for the region to be available at the moment of the registration of the views.

You can find more information about the view discovery approach in the following chapter of the Prism documentation:

On the other hand, if needed, you could also have explicit control of when each module is loaded. For example, if your modules are initialized on demand, you can define in which order the modules are loaded according to your requirements. Also, if none of the above approaches fit your scenario, you could define dependencies between different modules, (for example, making the Module B dependent of the Module A) so that the dependency module is initialized first.

You might find more information regarding both possibilities in the following chapter of the Prism documentation:

I hope you find this useful,

Damian Cherubini

Jan 4, 2012 at 8:52 PM

Hello Damian


I,m using the view discovery approach but strangely getting the error

System.InvalidOperationException was unhandled by user code
  Message=View already exists in region.
       at Microsoft.Practices.Prism.Regions.Region.InnerAdd(Object view, String viewName, IRegionManager scopedRegionManager) in c:\release\WorkingDir\PrismLibraryBuild\PrismLibrary\Desktop\Prism\Regions\Region.cs:line 360
       at Microsoft.Practices.Prism.Regions.Region.Add(Object view, String viewName, Boolean createRegionManagerScope) in c:\release\WorkingDir\PrismLibraryBuild\PrismLibrary\Desktop\Prism\Regions\Region.cs:line 275
       at Microsoft.Practices.Prism.Regions.Region.Add(Object view) in c:\release\WorkingDir\PrismLibraryBuild\PrismLibrary\Desktop\Prism\Regions\Region.cs:line 246
       at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.AddViewIntoRegion(Object viewToAdd) in c:\release\WorkingDir\PrismLibraryBuild\PrismLibrary\Desktop\Prism\Regions\Behaviors\AutoPopulateRegionBehavior.cs:line 84
       at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.OnViewRegistered(Object sender, ViewRegisteredEventArgs e) in c:\release\WorkingDir\PrismLibraryBuild\PrismLibrary\Desktop\Prism\Regions\Behaviors\AutoPopulateRegionBehavior.cs:line 109


at this line in moduleInitialize

 rmgr.RegisterViewWithRegion(RegionNames.MainRegion, typeof(ExShell));


all the exceptions for this so far are on reactivation etc

any idea what could be wrong ?




Jan 4, 2012 at 9:53 PM
Edited Jan 4, 2012 at 10:08 PM

Update - not sure what the problem was but i added the AutoPopulateExportedViewsBehavior from StockTraderRI and it went away

However, given the scenario above of

ModuleA adding a CustomView with regions RA, RB, RC

and ModuleB using view discovery to load View1 into RA and View2 into RB

view1 and view2 don't show up, stepping thru the code in AutoPopulateExportedViewsBehavior, it appears that RA, RB, RC are never processed/added to the regionmanager !!

any clues on what/where to check ?


also on a side note, in AutoPopulateExportedViewsBehavior.AddRegisteredViews()


is executed twice from this code block

                       if (!this.Region.Views.Contains(view))

not sure how that's possible but that may have something to do with the issue above!

Jan 5, 2012 at 2:00 PM
Edited Jan 5, 2012 at 2:59 PM

still have the issue of the call for adding the view being made twice though !!

Jan 5, 2012 at 3:53 PM


Based on my understanding of your scenario it seems that you are registering the view in the region twice: one with the RegisterViewWithRegion method and the other with the AutoPopulateExportedViewsBehavior. Although both approaches associate a view with a region to be injected automatically when the corresponding region is available, those are are not related to each other and each one use its own "registration" implementation. Usually, when following the view discovery approach, you would need to use only one of the aforementioned functionalities.

For example, when registering a view using the AutoPopulateExportedViewsBehavior you need to export the view using the following custom export attribute: [ViewExport(RegionName = RegionName)]. Instead, when using the RegisterViewWithRegion method, this custom attribute is not required.

Also, if you need more support regarding this problem, it would be helpful if you could provide us with a repro-sample application portraying it, so that we can help you find the causes behind the exception.

I hope you find this useful,

Damian Cherubini

Jan 5, 2012 at 4:17 PM
Edited Jan 9, 2012 at 12:39 PM

RegisterViewWithRegion is commented out so it,s only done once

will create a repro app and revert

Jan 6, 2012 at 8:15 PM

You are right, this works with the ms .net controls

however i am using devx controls where it fails in the test app so looks like an issue with the adapter - fyi


Jan 9, 2012 at 12:38 PM

Got feedback from devx and it's happening because of this bug in the framework



posting this here in case others land here confounded by same/similar issue