Prism: How to load Default View in Shell Region with Unity Bootstrapper

Steve.NayLinAung picture Steve.NayLinAung · Jan 2, 2016 · Viewed 7.4k times · Source

When I look Prism Navigation QuickStart demo, it uses Mef Bootstrapper and use IPartImportsSatisfiedNotification interface in Shell.xaml.cs file to load default view into the Shell Region as following.

[Export]
public partial class Shell : UserControl, IPartImportsSatisfiedNotification
{
    ...

    public void OnImportsSatisfied()
    {
        this.ModuleManager.LoadModuleCompleted +=
            (s, e) =>
            {
                if (e.ModuleInfo.ModuleName == EmailModuleName)
                {
                    this.RegionManager.RequestNavigate(
                        RegionNames.MainContentRegion,
                        InboxViewUri);
                }
            };
    }
}

In my project, I use Unity Bootstrapper and try to reference this demo for loading default view. As expected, it totally does not work.

Please, share me suggestions and some recommendations on "How to inject Default View into Shell Region with Unity Bootstrapper".

Answer

R. Richards picture R. Richards · Jan 3, 2016

Assuming you have a CreateModuleCatalog override in your bootstrapper, you use it to add a module to the catalog.

catalog.AddModule(typeof(YourModule));

In the YourModule Initiaize override, register the view you want displayed like below.

Using view discovery:

RegionManager.RegisterViewWithRegion("YourRegion", typeof(YourView));

OR

Using view injection (if you want a little more control, or need a scoped region, etc.):

IRegion region = _region_manager.Regions["YourRegion"];
var view = _container.Resolve<YourView>();
region.Add(view, typeof(YourView).FullName);
region.Activate(view);

This injection method needs you have to a reference to the region manager, and you have registered the view with the Unity container, and you have a reference to the container

As long as the YourRegion region is in your Shell xaml, and is visible at runtime, the YourView will show in it.

The Hello World quick start shows this too, and uses the Unity container.

https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/HelloWorld