PRISM和WPF如何按需添加模块

10

我的shell窗口有一组选项卡和一个主要的区域,是一个内容控件。我有四个模块需要在选择某个选项卡时按需加载。因此,当选择选项卡1时,我想要加载模块A,当选择选项卡2时,我想要加载模块B,以此类推。第一个模块在应用程序启动时加载。问题在于,当我切换选项卡时什么都不会发生,但也没有错误提示。我正在使用WPF和Silverlight - 2009年10月版的prism Composite Application Guidance。

我尝试了以下方法:

Shell:

 public partial class Shell : RibbonWindow, IShellView
    {
        private readonly IRegionManager regionManager;
        private readonly IModuleManager moduleManager;

    public Shell(IModuleManager moduleManager)
    {
        this.moduleManager = moduleManager;
        InitializeComponent();

    }

    public void ShowView()
    {
        this.Show();
    }



    private void onTabSelection(object sender, RoutedEventArgs e)
        {
                 this.moduleManager.LoadModule("ModuleB");
        }
}

引导程序:

  public partial class MyBootstrapper : UnityBootstrapper
    {
        protected override IModuleCatalog GetModuleCatalog()
        {
            var catalog = new ModuleCatalog();
            catalog.AddModule(typeof(ModuleA)).AddModule(typeof(ModuleB));

        return catalog;
    }

    protected override void ConfigureContainer()
    {
        Container.RegisterType<IShellView, Shell>();

        base.ConfigureContainer();
    }



    protected override DependencyObject CreateShell()
    {
        ShellPresenter presenter = Container.Resolve<ShellPresenter>();
        IShellView view = presenter.View;

        view.ShowView();

        return view as DependencyObject;
    }

}

我希望能够按需加载模块B(我曾经使用过此处已注释的行,这就是为什么我将其保留在这里的原因):

[Module(ModuleName = "ModuleB", OnDemand = true)]
public class ModuleB : IModule
{  

    private readonly IUnityContainer _container;
    private readonly IRegionManager _regionManager;

    public ModuleB(IUnityContainer container, IRegionManager regionManager)
    {
        _container = container;
        _regionManager = regionManager;
    }
    public void Initialize() {

        _regionManager.Regions["MainRegion"].Add(new ModuleBView());
        this.RegisterViewsAndServices();

      //  this._regionManager.RegisterViewWithRegion(RegionNames.MainRegion, () => _container.Resolve<MissionProfileView>());
    }
    protected void RegisterViewsAndServices()
    {
        _container.RegisterType<Modules.ModuleBView>();
    }
}

我在这里做错了什么?我应该如何继续?


1
我有几乎相同的情况。给你点赞。 - autonomatt
2个回答

4
我成功地使用了按需加载模块。在我的方案中,我在用户登录后加载它们。
作为您项目的健全性检查,请确保您的ModuleB.dll与您的shell/application位于同一目录中(例如,如果您处于调试模式,请确保它被复制到调试目录中)。
我将模块名称和模块dll命名相同,我不确定这是否是必需的,但这是我坚持的惯例。
我的引导程序CreateModuleCatalog非常简单。
protected override IModuleCatalog CreateModuleCatalog()
{
    ModuleCatalog catalog = new ConfigurationModuleCatalog();
    return catalog;

}

这些模块在我的 app.config 文件中列出。

<modules>
    <module assemblyFile="PatronModule.dll" moduleType="PatronModule.PatronModuleDefinition, PatronModule" moduleName="PatronModule" startupLoaded="false" />
</modules>

然后当我加载模块时,我使用这段代码。
    IModuleManager moduleManager = _container.Resolve<IModuleManager>();
    ModulesConfigurationSection modules = ConfigurationManager.GetSection("modules") as ModulesConfigurationSection;
    foreach (ModuleConfigurationElement module in modules.Modules)
        moduleManager.LoadModule(module.ModuleName);

模块的加载必须发生在GUI线程上,因此如果需要,您需要使用调度程序来进行加载(这是我用于此的行)。

Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(() => { LoadModulesOnGuiThread(); }));

希望这能帮到你


0

我不是很确定,但在添加模块时尝试将InitializationMode设置为OnDemand,像这样:

var catalog = new ModuleCatalog();
catalog.AddModule(typeof(ModuleA)).AddModule(typeof(ModuleB), InitializationMode.OnDemand);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接