Scenario

You are working on a WPF composite application built with Prism bootstrapped with UnityBoostrapper, and you try to launch a second isolated/standalone WPF Prism composite app also bootstrapped with UnityBoostrapper, so the two windows are open at the same time.

At this stage the second window is behaving properly i.e. views get navigated to the appropriate regions. So far so good!

Ok where is the matter then? As soon as you try to go back to the main shell window and try to navigate a new view to one of its regions then Bang! What you will see in the region is System.Object’, which is the sort of undesired behaviour you get when trying to navigate an unregistered view with UnityContainer to a region.

Note: Using Prism 6.1.1 (Latest Stable).

TwoShellBootstrappersProblem

Cause

By instantiating a second UnityBootstrapper we end up having two instances of UnityContainer & RegionManager in memory, but, one instance of the public static ServiceLocator class (from the Microsoft.Practices.ServiceLocation assembly).

Every time you call bootstrapper.Run() method the Prism framework configures various services and one of them is the ServiceLocatorProvider singleton. I.e. the ServiceLocator class uses the newly instantiated UnityContainer.

How is ServiceLocator used then? Every time you request navigation through the RegionManager, the request is delegated to RegionNavigationContentLoader which uses ServiceLocator to get an instance of a View. At this time of execution, the service locator doesn’t know about the first unity container instance that has main shell’s views and regions registered. As a result, when navigating any view to a region in the first shell you would get ‘System.Object’.

Work Around

First thing springs to mind is to not instantiate a new UnityBoostrapper, and pass a reference of the main unity container and use wherever appropriate to launch second shell window. However this is not enough, still the region manager needs to know about the second window. Hence a reference of the region manager is passed. Also the region manager needs to be cleared from any information related to the second window regions once closed so it can have a clean start next time.

Note: Multiple Windows

For multiple shells being opened at the same time (non modal scenario) you must give each shell instance a unique region name.

PrismMultipleShellWindows

 

Demo Source Code

 

 

 

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *