How to populate ScopedRegions at startup?

Topics: Prism v4 - Silverlight 4
Oct 15, 2011 at 9:40 AM

Hi there,

I have a NavigationView that is found and injected into Shell automatically like this:

[ViewExport(RegionName = Constants.NavRegion)]
public partial class NavigationView : UserControl


The NavigationView has another Region that also needs to be populated:

<ContentControl regions:RegionManager.RegionName="BlackPanelRegion" />

In my NavigationController class I do the following to populate the BlackPanel Region inside the NavigationView. 


var blackPanelViewModel = BlackPanelViewModelFactory.CreateExport().Value;
var blackPanelView = new BlackPanelView(blackPanelViewModel) { Name = Constants.BlackPanelRegion };
var scopedRegionManager = _regionManager.Regions[Constants.NavRegion].RegionManager;
scopedRegionManager.Regions[Constants.BlackPanelRegion].Add(blackPanelView, Constants.BlackPanelRegion);

This works fine. But I find it limiting that I have to do it myself.  How can I populate the BlackPanelView inside the NavigationView automatically, without being forced to inject it myself?

Many Thanks,

Oct 17, 2011 at 3:45 PM


You could try using the view discovery approach, which allows you to avoid adding views to regions programmatically. This approach sets up a relationship in the RegionViewRegistry between a region's name and the type of a view. This way when a region is created, a behavior looks for all the ViewTypes associated with the region and automatically instantiates and loads the corresponding views. Therefore you could try registering your view like this:

this.regionManager.RegisterViewWithRegion(Constants.BlackPanelRegion, typeof(blackPanelView)); 
You might find more information about view discovery approach in the Prism documentation:

I hope you find this helpful.

Agustin Adami