简体   繁体   中英

How to add PRISM modules to Toolbar using MEF and MVVM

I am using PRISM 4.0 with MEF as my container. I have defined 2 regions, ToolBar and MainRegion in my Shell. The toolbar region is automatically populated with my ToolBarModule using a custom RegionBehaviour - AutoPopulateExportedViewsBehaviour. My MainRegion can contain 1 or more View modules which will be docked via a third party dock layout manager.

I'm having trouble creating the toolbar buttons to represent the available views in my application. My idea was to use a ToolBarService or an Event pattern so that each View module could register itself with the ToolBar in a decoupled manner.

However it seems my View Module contructor is not called until I call RegionManager.RegisterViewWithRegion...

How can I control the initialisation of my modules so they can register with the ToolBar. Thus allowing them to add a button but not actually show the view itself. The View will only be shown when the button the view just registered is clicked.

Thanks

How can I control the initialisation of my modules so they can register with the ToolBar but not be shown initally?

I'm not sure what you mean by that.

I understand that you want modules to register it's own navigation part when they loaded. I have similar scenario where I have menu bar on top and button bar below. Each module when loaded - inserts it's own buttons/menus using Initialize code:

public void Initialize()
        {
            this.RegionManager.RegisterViewWithRegion(RegionNames.Menu, typeof(NavigationView));
            this.RegionManager.RegisterViewWithRegion(RegionNames.Toolbar, typeof(ToolbarNavigationView));
        }

Those regions have actual buttons/items which when pressed call something else. For examle, here is NavigationViewModel

namespace IDATT.Module.SystemManager.ViewModels
{
    using System;
    using System.ComponentModel.Composition;

    using Microsoft.Practices.Prism.Regions;

    [Export]
    public class NavigationViewModel
    {
        [Import]
        public ISecurityService SecurityService { get; set; }

        [Import]
        public IRegionManager RegionManager { get; set; }

        public void Mail()
        {
            this.RegionManager.RequestNavigate(RegionNames.Tabs, new Uri(typeof(MailView).Name, UriKind.Relative));
        }

        public void MaintainUser()
        {
            this.RegionManager.RequestNavigate(RegionNames.Tabs, new Uri(typeof(MaintainUserView).Name, UriKind.Relative));
        }

        public void MaintainGroup()
        {
            this.RegionManager.RequestNavigate(RegionNames.Tabs, new Uri(typeof(MaintainGroupView).Name, UriKind.Relative));
        }

        public void MaintainMailTemplate()
        {
            this.RegionManager.RequestNavigate(RegionNames.Tabs, new Uri(typeof(MaintainMailTemplateView).Name, UriKind.Relative));
        }

        public void SetUpOptions()
        {
            this.RegionManager.RequestNavigate(RegionNames.Tabs, new Uri(typeof(SetUpSystemManagerOptionsView).Name, UriKind.Relative));
        }

        public void Logout()
        {
            this.SecurityService.Logout();
        }
    }
}

It looks like I can "force" the constructor of my view module to be called by using a custom RegionBehaviour. Inside of this I can cast my view module to a specific base view or interface type and call the a function. This will then register my view with the toolbar but not necessarily show the view in the "main" region of my application.

Thanks for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM