Bootstrapper主要用来初始化Prism应用程序,其处理流程如图:
在Prism应用中,Bootstrapper的开发过程如下:
override基类的Bootstrapper
Unity基类提供了UnityBootstrapper和MefBootstrapper,可以根据实际的应用选用不同的Bootstrapper。
[csharp]
class DirectorBootstrapper : UnityBootstrapper
{
protected override System.Windows.DependencyObject CreateShell()
{
return Container.Resolve<Shell>();
}
protected override void InitializeShell()
{
Application.Current.MainWindow = (Window)Shell;
Application.Current.MainWindow.Show();
}
}
在App类中调用Bootstrapper的Run函数
[csharp]
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
DirectorBootstrapper bootstrapper = new DirectorBootstrapper();
bootstrapper.Run();
}
}
配置ModuleCatalog
下面的代码使用plug in模式,将Module配置在Shell中。
[csharp]
protected override IModuleCatalog CreateModuleCatalog()
{
ModuleCatalog catalog = new ConfigurationModuleCatalog();
return catalog;
}
[html]
< configSections>
<section name="modules" type="Microsoft.Practices.Prism.Modularity.ModulesConfigurationSection, Microsoft.Practices.Prism"/>
</configSections>
<modules>
<module assemblyFile="Modules.DeviceManager.dll"
moduleType="Modules.DeviceManager.DeviceManagerModule, Modules.DeviceManager"
moduleName="DeviceManagerModule"/>
</modules>
实现模块接口
[csharp]
public class DeviceManagerModule : IModule
{
private IUnityContainer unityContainer;
private IRegionManager regionManager;
public DeviceManagerModule(IUnityContainer container, IRegionManager regionManager)
{
this.unityContainer = container;
this.regionManager = regionManager;
}
#region IModule Members
/// <summary>
/// IModule interface method called on module loading.
/// </summary>
public void Initialize()
{
this.regionManager.RegisterViewWithRegion(RegionNames.MainRegion,
() => this.unityContainer.Resolve<DeviceView>("DeviceView"));
}
#endregion
}