Load different modules in the same regions

Topics: Prism v4 - WPF 4
Feb 17, 2014 at 6:11 AM
Hi,

I have created an application with one menu consisting of two menu items. My ShellView.xaml contains two regions: a LeftRegion and a MainRegion. Each menu loads different modules upon click on it. Each module consists of a MasterView (ListControl) and an EditView (ContentControl).

My goal is to load MasterView in the LeftRegion and EditView in the MainRegion when the user clicks on the respective menu items.

How can I approach this case?

Thanks
Feb 17, 2014 at 2:32 PM
Hi abampakos,

One possible way of doing this would be to register both Master Views in LeftRegion and both Edit Views in the MainRegion from each Module's Initialize() method. However, you would need to register them with different names so they could be identified in the Region's View Collection. When you click on a menu item, the corresponding module would get initialized, performing registration of their Views into the corresponding Region for future Navigation. This would not be an issue for different modules as you may import the RegionManager through constructor and retreive the related Region from it.

Finally, you would handle the menu click event by calling RequestNavigate() method for the two Regions.

I hope this helped you,
Regards.

Gabriel Ostrowsky.
https://blogs.southworks.net/gostrowsky
Feb 18, 2014 at 6:46 PM
Edited Feb 18, 2014 at 7:09 PM
Hi,

Thank you for your answer.
I would like to clarify that the menu commands exist in the ShellViewModel. How can I use RegionManager in it?
Below is the code from my ShellViewModel with the RegionManager imported. Is this correct?
        [Import]
        IModuleManager moduleManager { get; set; }

        [Import]
        IRegionManager regionManager { get; set; }

        public ShellViewModel()
        {
            // Initialize this ViewModel's commands.
            ExitCommand = new DelegateCommand<object>(AppExit, CanAppExit);
            MembersCommand = new DelegateCommand<object>(AppMembers, CanAppMembers);
            BoardCommand = new DelegateCommand<object>(AppBoard, CanAppBoard);
        }

        #region ExitCommand
        public DelegateCommand<object> ExitCommand { get; private set; }

        private void AppExit(object commandArg)
        {
            App.Current.Shutdown();
        }

        private bool CanAppExit(object commandArg)
        {
            return true;
        }
        #endregion

        #region MembersCommand
        public DelegateCommand<object> MembersCommand { get; private set; }

        private void AppMembers(object commandArg)
        {
            moduleManager.LoadModule("MemberModule.ModuleInit");
            regionManager.RequestNavigate(RegionNames.LeftRegion, new Uri("MemberMasterView", UriKind.Relative));
        }

        private bool CanAppMembers(object commandArg)
        {
            return true;
        }
        #endregion

        #region BoardCommand
        public DelegateCommand<object> BoardCommand { get; private set; }

        private void AppBoard(object commandArg)
        {
            moduleManager.LoadModule("BoardModule.ModuleInit");
            regionManager.RequestNavigate(RegionNames.LeftRegion, new Uri("BoardMasterView", UriKind.Relative));
        }

        private bool CanAppBoard(object commandArg)
        {
            return true;
        }
        #endregion
Feb 19, 2014 at 6:02 PM
Hi,

Your implementation is correct in which you import the RegionManager from the container. In addition, you would also need to import the RegionManager on each Module class so you may be able to register the Views into each Region when initializing the module.

When I mentioned that the RegionManager may be imported through constructor, I was referring to the following implementation although it basically behaves exactly as yours:
[ImportingConstructor]
public ModuleA(IRegionManager regionManager, IModuleManager moduleManager)
{
    RegionManager = regionManager;
    ModuleManager = moduleManager;
}
...
Then, you would successfully navigate through Master Views like you described, but Edit Views would need their corresponding RequestNavigate() on the MainRegion too.

For more information you can refer to the following MSDN Prism Guide chapters:

Regards,

Gabriel Ostrowsky.
https://blogs.southworks.net/gostrowsky
Marked as answer by abampakos on 2/20/2014 at 1:37 AM