2010-06-28 54 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(); })); 

希望這有助於