Prism学习笔记(三):对Prism中模块化程序的理解。

       这几天学习Prism,看了下帮组文档和程序。在Prism中专门讲解模块化程序开发的有俩个列子,一个是用Unity解耦实现Prism的依赖注入,另一个列子是用MEF解耦是实现Prism的依赖注入。

案例(一):ModularityWithUnity.Silverlight

开启应用程序

(1)App.xaml.cs中 

private void Application_Startup(object sender, StartupEventArgs e)
{
//创建Shell实例,不需要设置RootVisual
QuickStartBootstrapper bootstrapper = new QuickStartBootstrapper();
bootstrapper.Run();
}

 (2)QuickStartBootstrapper.cs

//实例化Shell
//最基本的实现,确保Shell被整合到容器中
protected override void InitializeShell()
{
base.InitializeShell();

Application.Current.RootVisual
= (UIElement)this.Shell;
}

构建模块

这里以Module A 为例,构建Module A,主要实现IModule接口

(1)首先新建Silverlight项目ModuleA

 (2)实现IModule接口

 

View Code
namespace ModuleA
{
using System;
using Microsoft.Practices.Prism.Logging;
using Microsoft.Practices.Prism.Modularity;
using ModuleTracking;

/// <summary>
/// A module for the quickstart.
/// </summary>
public class ModuleA : IModule
{
private ILoggerFacade logger;
private IModuleTracker moduleTracker;

/// <summary>
/// Initializes a new instance of the <see cref="ModuleA"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="moduleTracker">The module tracker.</param>
public ModuleA(ILoggerFacade logger, IModuleTracker moduleTracker)
{
if (logger == null)
{
throw new ArgumentNullException("logger");
}

if (moduleTracker == null)
{
throw new ArgumentNullException("moduleTracker");
}

this.logger = logger;
this.moduleTracker = moduleTracker;
this.moduleTracker.RecordModuleConstructed(WellKnownModuleNames.ModuleA);
}

/// <summary>
/// Notifies the module that it has be initialized.
/// </summary>
public void Initialize()
{
this.logger.Log("ModuleA demonstrates logging during Initialize().", Category.Info, Priority.Medium);
this.moduleTracker.RecordModuleInitialized(WellKnownModuleNames.ModuleA);
}
}
}

 注册模块

注册模块有以下3种方式:
      (1)用代码注册模块
      (2)用Xaml文件注册模块
      (3)用配置文件注册模块(只针对WPF)

下面介绍了用代码注册模块和用xaml文件注册模块的再改案例中的实现。

一)用代码注册模块

//用代码注册模块
protected override void ConfigureModuleCatalog()
{
// Module A
Type moduleAType = typeof(ModuleA.ModuleA);
this.ModuleCatalog.AddModule(new ModuleInfo(moduleAType.Name, moduleAType.AssemblyQualifiedName, WellKnownModuleNames.ModuleD));

//Module C
Type moduleCType = typeof(ModuleC.ModuleC);
ModuleCatalog.AddModule(
new ModuleInfo()
{
ModuleName
= moduleCType.Name,
ModuleType
= moduleCType.AssemblyQualifiedName,
//指定按需特性
InitializationMode = InitializationMode.OnDemand
});
}

二)用xaml文件注册模块:

在ModulesCatalog.xaml文件中

<Modularity:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys
="clr-namespace:System;assembly=mscorlib"
xmlns:Modularity
="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">
<Modularity:ModuleInfoGroup Ref="ModuleB.xap" InitializationMode="WhenAvailable">
<Modularity:ModuleInfo ModuleName="ModuleB" ModuleType="ModuleB.ModuleB, ModuleB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</Modularity:ModuleInfoGroup>
<Modularity:ModuleInfoGroup InitializationMode="OnDemand">
<Modularity:ModuleInfo Ref="ModuleE.xap" ModuleName="ModuleE" ModuleType="ModuleE.ModuleE, ModuleE, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Modularity:ModuleInfo Ref="ModuleF.xap" ModuleName="ModuleF" ModuleType="ModuleF.ModuleF, ModuleF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
<Modularity:ModuleInfo.DependsOn>
<sys:String>ModuleE</sys:String>
</Modularity:ModuleInfo.DependsOn>
</Modularity:ModuleInfo>
</Modularity:ModuleInfoGroup>

<!-- Module info without a group -->
<Modularity:ModuleInfo Ref="ModuleD.xap" ModuleName="ModuleD" ModuleType="ModuleD.ModuleD, ModuleD, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</Modularity:ModuleCatalog>

在QuickStartBootstrapper.cs中还要加上这样一段代码 

//用Xaml文件注册模块(在UnityBootstrapper中)
//IModuleCatalog实例用于跟踪哪些模块有效,哪些模块可能需要被下载,哪些模块已经存在
protected override IModuleCatalog CreateModuleCatalog()
{
// Module B, D, E and F
return Modularity.ModuleCatalog.CreateFromXaml(new Uri("/ModularityWithUnity.Silverlight;component/ModulesCatalog.xaml", UriKind.Relative));
}

 加载模块

(1)Shell响应UI通过调用ModuleManager.LoadModule ,这里以Module C为例说明。在Shell.xaml.cs中

//Shell响应来至UI的请求,加载一个模块通过调用ModuleManager.LoadModule方法。
//ModuleManager用的是异步事件模式
//加载ModuleC
private void ModuleC_RequestModuleLoad(object sender, EventArgs e)
{
this.moduleManager.LoadModule(WellKnownModuleNames.ModuleC);
}

(2)Shell被通知已下载进度,通过订阅ModuleManager.ModuleDownloadProgressChanged事件,在Shell.xaml.cs中

//跟踪模块下载进度
void ModuleManager_ModuleDownloadProgressChanged(object sender, ModuleDownloadProgressChangedEventArgs e)
{
this.moduleTracker.RecordModuleDownloading(e.ModuleInfo.ModuleName, e.BytesReceived, e.TotalBytesToReceive);
}

(3)Shell被通知模块已经下载完成并初始化完成,通过订阅ModuleManager.LoadModuleCompleted事件

//模块加载完成时促发的事件
//e.IsErrorHandled来只是模块是否加载成功
void ModuleManager_LoadModuleCompleted(object sender, LoadModuleCompletedEventArgs e)
{
this.moduleTracker.RecordModuleLoaded(e.ModuleInfo.ModuleName);
}
posted @ 2011-06-29 14:24  焦涛  阅读(1799)  评论(0)    收藏  举报