10

我的 shell 窗口中有一组选项卡,其中一个主要区域是 contentcontrol。I also have four modules that I want to load on demand when a certain tab is selected. So when tab1 is selected I want to load moduleA, when tab2 is selected I want to load ModuleB, etc. The first module loads on when the application starts. 问题是当我更改选项卡时没有任何反应。没有错误很难。我正在使用此版本的 prism Composite Application Guidance for WPF 和 Silverlight - 2009 年 10 月。

我尝试了这种方法:

壳:

 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>();
    }
}

我在这里做错了什么?我应该如何进行?

4

2 回答 2

4

我成功地使用了模块的按需加载。在我的场景中,我在用户登录后加载它们。

作为对项目的健全性检查,确保 ModuleB.dll 与 shell/应用程序位于同一目录中。(例如,如果您处于调试模式,请确保将其复制到调试目录)。

我的模块名称和模块 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(); }));

希望这可以帮助

于 2011-08-15T02:33:40.710 回答
0

我不太确定,但在添加模块时尝试将 InitializationMode 设置为 OnDemand,如下所示:

var catalog = new ModuleCatalog();
catalog.AddModule(typeof(ModuleA)).AddModule(typeof(ModuleB), InitializationMode.OnDemand);
于 2010-07-07T10:56:20.507 回答