第533篇--Prism Module学习_2
接着上文Prism Module讲,本文主要讲述Prism Modules在Boot strapper中的配制方式,可以用ConfigureModuleCatalog 或是CreateModuleCatalog方法来配制全文中的Module Catalog.
在第一个方法中,我们用代码的形式,把B ,C Module加载到了工程,在后一个模式中,我们把Module E配制在XAML中.
代码1:
//用代码注册模块 protected override void ConfigureModuleCatalog() { // Module B Type moduleBType = typeof(ModuleB.ModuleB); this.ModuleCatalog.AddModule(new ModuleInfo(moduleBType.Name, moduleBType.AssemblyQualifiedName)); //Module C Type moduleCType = typeof(ModuleC.ModuleC); ModuleCatalog.AddModule(new ModuleInfo() { ModuleName = moduleCType.Name, ModuleType = moduleCType.AssemblyQualifiedName, //指定按需特性 InitializationMode = InitializationMode.OnDemand }); } //用Xaml文件注册模块(在UnityBootstrapper中) //IModuleCatalog实例用于跟踪哪些模块有效,哪些模块可能需要被下载,哪些模块已经存在 protected override IModuleCatalog CreateModuleCatalog() { // Module B, D, E and F this.ModuleCatalog = new ModuleCatalog(); ModuleCatalog catalog = Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("PrismModuleWPFStart;component/ModuleConfig.xaml", UriKind.Relative)); foreach (var item in catalog.Modules) { ModuleCatalog.AddModule(item); } return ModuleCatalog; }
在Shell.xaml.cs文件中,我们可以跟踪加载的情况,
public partial class Shell : Window { //[Dependency] public IModuleManager ModuleManager { get; set; } public Shell(IModuleManager ModuleManager) { InitializeComponent(); this.ModuleManager = ModuleManager; this.ModuleManager.ModuleDownloadProgressChanged += ModuleManager_ModuleDownloadProgressChanged; this.ModuleManager.LoadModuleCompleted += ModuleManager_LoadModuleCompleted; } private void Button_Click(object sender, RoutedEventArgs e) { ModuleManager.LoadModule("ModuleC"); } //跟踪模块下载进度 void ModuleManager_ModuleDownloadProgressChanged(object sender, ModuleDownloadProgressChangedEventArgs e) { // This line will not be invoked //this.moduleTracker.RecordModuleDownloading(e.ModuleInfo.ModuleName, e.BytesReceived, e.TotalBytesToReceive); } //模块加载完成时促发的事件 //e.IsErrorHandled来只是模块是否加载成功 void ModuleManager_LoadModuleCompleted(object sender, LoadModuleCompletedEventArgs e) { // This line will be invoked //this.moduleTracker.RecordModuleLoaded(e.ModuleInfo.ModuleName); } }