2010-06-28 55 views
8

我有一套选项卡在我的shell窗口和一个主要区域whicxh是一个内容控件。当选择某个选项卡时,我还有四个模块需要按需加载。所以,当选择tab1时,我想加载moduleA,当选择tab2时,我想加载ModuleB等。第一个模块在应用程序启动时加载。问题是,当我改变标签没有任何反应。没有错误艰难。我使用这个版本的棱镜复合应用程序指南WPF和Silverlight的 - 2009年10月PRISM和WPF如何添加一个模块按需

我尝试这样的做法:

壳牌:

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

} 

我希望能够按需加载moduleB(我曾经使用这个注释行,这就是为什么我把它留在这里):

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

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

+0

我有几乎相同的场景。来自我的问题+1 – autonomatt 2010-06-30 17:12:48

回答

0

我不太肯定,但尝试设置InitializationMode添加模块时,按需,像这样:

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

我正在成功地使用了模块的按需加载。在我的情况下,我在用户登录后加载它们。

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

我有模块名称和模块DLL命名相同,我不知道这是否是一个要求,但它的约定,我坚持。

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

希望这有助于