View-switching with RequestNavigate()

Topics: Prism v4 - WPF 4
Mar 1, 2011 at 12:12 AM

I am getting started with Prism 4, and I am working on a demo app to get through the basic learning issues. My demo app has two modules, 'Module A' and 'Module B'. Each module has three views:

  • navigation button to load ther module's other views. This view object is loaded into a "NavigationRegion" (an ItemsControl) in the Shell.
  • A RibbonTab that is activated when the navigation button is clicked. This view object is loaded into the 'RibbonRegion' in the Shell. The Ribbon uses a Region Adapter that allows it to act as a region, in the same manner as an ItemsControl.
  • A main view for the module, which loads into a 'WorkspaceRegion' (a ContentControl) in the Shell. It is simply a text block with the module's name. It is also activated when the navigation button is clicked.

The navigation buttons are bound to ICommand objects via MVVM command properties. The ICommands activate and deactivate views when a navigation button is clicked:

    public class ShowModuleBViewCommand : ICommand
        public void Execute(object parameter)
            // Initialize
            var regionManager = m_ViewModel.RegionManager;
            // Show Ribbon Tab
            var moduleBRibbonTab = new Uri("ModuleBRibbonTab", UriKind.Relative);
            regionManager.RequestNavigate("RibbonRegion", moduleBRibbonTab);
            // Show View
            var moduleBView = new Uri("ModuleBView", UriKind.Relative);
            regionManager.RequestNavigate("WorkspaceRegion", moduleBView);

When the app starts up, modules are discovered without problem, and their views are loaded. The Shell correctly displays both navigation buttons in its NavigationRegion. Module A's main view displays correctly in the WorkspaceRegion, and when I click the ModuleB navigation button, the WorkspaceRegion correctly switches over to Module B's main view.

Here is my problem: The RibbonRegion behaves like an ItemsControl (because I may want to load more than one RibbonTab for a module at some point). But that means that the RibbonTabs for both modules are always visible, just like the navigation buttons. However, the behavior that I want is for the app to switch between the RibbonTabs for the two modules when the navigation buttons are clicked. In other words, when the application starts, only the RibbonTab for Module A should be visible, and when I click the navigation button for Module B, the RibbonTab for Module A should be hidden (or unloaded), and the RibbonTab for Module B should be shown (or loaded).

What's the best way to accomplish this result, using Prism 4? Thanks for your help.

Mar 1, 2011 at 6:16 AM
Edited Mar 1, 2011 at 6:17 AM

I found my answer. Implement the IRegionMemberLifetime interface on the view object or its view model. It has a single boolean property, KeepAlive, which determines whether the view should be retained in a region when the view is deactivated. Setting the property to false unloads the view on deactivation. See Developer's Guide to Microsoft Prism (ver 4), p. 121.

BTW, I implemented the interface of the view objects, rather than their view models, since the interface affects only the view--it does not flow through to the back-end of the app.

Hope that helps someone else down the road!

Mar 5, 2011 at 9:38 PM

Helped me a lot !!