View Discovery in A Dynamically Created WPF Window

Topics: Prism v4 - WPF 4
Feb 28, 2012 at 7:56 PM

I am working on a WPF project, using Prism, that requires the following

1. When first launched the application creates a hidden WPF window and displays an icon on the task bar

2. When right-clicking on the icon a menu shows

3. When selecting an item from the menu a second WPF window shows

The question is how to fill different regions in the second/visible WPF window to display the views using view discovery?

Mar 1, 2012 at 4:18 PM


I believe you might find the following blog post useful, where a possible approach for handling multiple-shell application in Prism is proposed:

Note that it should be similar to achieve this in Prism v4.

Also you can find the proposed sample, upgraded to Prism v4 in the following work item:

I hope you find this handy,

Agustin Adami

Mar 1, 2012 at 6:50 PM

Thanks a lot, aadami.

However I don't want to have two shell windows, unless one of them doesn't need to be created at application startup, and can be closed and recreated again.

What I need is that the application has a small memory footprint when the main UI (the second/visible WPF window) is not showing. I believe this is a pretty common use case.

Mar 2, 2012 at 3:45 PM


Based on my understanding, following the approach mentioned above you should be able to show the window whenever you need to. For example you could show the second window when selecting an item from the menu. Remember that the setting of the RegionManager will have to be done manually for any window you show this way.

If you plan to close it and recreate it again, be aware that if you are using the same instance of the RegionManager you should keep the RegionManager's Regions collection updated to avoid getting an error when opening the window the second time (as the region will already be registered). For example by removing the region when the window is closed, like this:

void Shell2_Closed(object sender, EventArgs e)

To avoid this an option could be to set the RegionManager attached property of the new window with a new region manager, that you could define in a shared service or just create a new one each time you set the attached property.

Take into account that the implementation details for the aforementioned functionality will mainly depend on your personal preferences and the requirements of your scenario.

On the other hand, I believe you could also check the approach used in the StockTrader Reference Implementation on how to show a popup window. As to achieve this kind of scenarios you could also benefit from the DialogActivation behaviors implementations provided in this application.

I hope you find this useful,

Agustin Adami

Mar 5, 2012 at 6:55 PM
Edited Mar 5, 2012 at 6:56 PM

Again thank you aadami.

In view discovery views are registered with regions at the time modules are initialized (IModule.Initialize()). How do I register the views with the regions in a dynamically created window/shell?

I also looked at the DialogActivationBehavior you mentioned. It seems to me that a new popup window using this behavior doesn't support regions in the new popup window. Am I missing something?

I am wondering if it is possible to do the following:

(1) Create the first WPF window in App.OnStartup - no Prism initialization, bootstrapping, etc. just a regular WPF application window.

(2) When user clicks a button on the first WPF window a thread is created which, in turn, runs bootstrapping and creates the second WPF window with regions. The second WPF window is the real Prism Shell window.

Mar 7, 2012 at 7:18 PM


Based on my understanding, you should be able to register or inject views in a dynamically created shell in the same way you would do with a typical single-shell Prism application. Also, as far as I know, both dynamically created window and popup windows should be able to support regions without problems.

As mentioned previously by Agustin, the only additional step to use regions in those windows is to set a RegionManager in them manually.

In the following link you can find a sample application, which uses a dynamically created view as a shell:

In the application you will find a HiddenShell window (which would be the hidden shell you mentioned above) with a button to shown the MainShell (this could be instead done by a menu option in a system tray icon). Using the WindowService shared service, the MainShell window is displayed and a view is added (which was registered by the SecondaryModule). This view has another region in which you can inject views.

Also, check the Bootstrapper included with the sample application, as it overrides the ConfigureDefaultRegionBehaviors method in order to avoid a known issue in Prism.

As a side note, take into account that as far as I know there is not official Prism guidance for scenarios such as running applications without an initial shell or with a icon in the system tray.

I hope you find this useful,

Damian Cherubini

Mar 9, 2012 at 2:09 AM
Edited Mar 9, 2012 at 2:19 AM

I tried SecondShellThroughService and it works great! Thank you DCherubini and aadami for your help and quick response!

As I mentioned before I want to have small memory footprint when the main window is not showing because the application may run in the background, and notify users of any events.