Prism初研究之初始化应用

Prism初研究之初始化应用
Bootstrapper
DI
Shell
关键抉择
核心步骤
创建Bootstrapper
实现CreateShell方法
实现InitializeShell方法
创建并配置Module Catalog
创建并配置Container
核心服务(与应用无关)
与应用相关的服务(Stock Trader RI)
在UnityBootstrapper中创建并配置Container
CreateContainer
ConfigureContainer
示例:QuickStartBootstrapper(Modularity QuickStart)
在MefBootstrapper中创建并配置Container
CreateContainer
ConfigureContainer
示例:QuickStartBootstrapper() Bootstrapper

Bootstrapper类的责任是使用Prism类库初始化应用程序,抽象类Bootstrapper提供的函数大多都是虚方法。

DI

UnityBootstrapper和MefBootstrapper类实现了大多数必须的功能。

Shell

在Prism应用中,创建Shell或者主窗口的责任是Bootstrapper的。因为Shell依赖的一些服务比如Region Manager需要在Shell显示之前初始化。

关键抉择 是否使用Unity、MEF或者其它的dependency injection container,这决定Bootstrapper类创建;应用使用那些服务,这些服务需要注册到container中;决定创建日志服务是否需要,或者是否需要其它日志服务;决定哪些模块是希望应用发现的,通过显示代码声明、文件夹扫描、配置文件还是XAML方式配置; 核心步骤 创建Bootstrapper 实现CreateShell方法

返回你应用程序Shell类的实例,可以根据需求选择创建Shell对象或者从Container中获取Shell。

protected override DependencyObject CreateShell(){ return this.Container.Resolve<Shell>();} 实现InitializeShell方法

创建Shell之后显示之前,需要为应用程序设置主界面。

protected override void InitializeShell(){ base.InitializeShell(); //基类的InitializeShell()没有做任何事。 App.Current.MainWindow = (Window) this.Shell; App.Current.MainWindow.Show();} 创建并配置Module Catalog

抽象类Bootstrapper的CreateModuleCatalog()返回new ModuleCatalog。

protected virtual IModuleCatalog CreateModuleCatalog(){ return new ModuleCatalog();}

UnityBootstrapper和MEFBootstrapper类的Run方法都调用CreateModuleCatalog()方法,为ModuleCatalog属性设置返回值。

创建并配置Container

Container在Prism应用中扮演关键角色,Prism类库和应用程序需要Container来提供服务、模块等的依赖关系。在Container的Configuration过程中,需要的核心服务被注册。

核心服务(与应用无关) 服务接口描述IModuleManager检索、初始化应用程序模块的接口IModuleCatalog包含模块的元数据;Prism框架提供一些不同的模块分类IModuleInitializer初始化模块IRegionManager注册、检索Region的接口IEventAggregator一组松耦合的事件ILoggerFacade框架为日志服务提供了包装机制,日志服务在bootstrapper.Run()方法中注册。使用CreateLogger方法的返回值,所以如果需要定制日志服务,需要覆写CreateLogger()方法IServiceLocator允许Prism 类库访问Container。如果需要定制或者扩展类库将会很有用 与应用相关的服务(Stock Trader RI) Stock Trader RI 中的服务描述IMarketFeedService提供实时的市场数据,PositionSummaryViewModel使用此服务IMarketHistoryService提供市场历史数据IAccountPositionService提供基金列表IOrdersService提交订单服务INewsFeedService提供选择基金的新闻IWatchListService控制添加新的监控数据到监控列表 在UnityBootstrapper中创建并配置Container CreateContainer

UnityBootstrapper的CreateContainer方法返回一个UnityContainer的实例,通常这种行为不需要被改变,如果要改变,覆盖这个方法即可。

ConfigureContainer

ConfigureContainer方法注册了一系列Prism核心服务(默认)

// UnityBootstrapper.csprotected virtual void ConfigureContainer(){ … if (useDefaultConfiguration) { RegisterTypeIfMissing(typeof(IServiceLocator), typeof(UnityServiceLocatorAdapter), true); RegisterTypeIfMissing(typeof(IModuleInitializer), typeof(ModuleInitializer), true); RegisterTypeIfMissing(typeof(IModuleManager), typeof(ModuleManager), true); RegisterTypeIfMissing(typeof(RegionAdapterMappings), typeof(RegionAdapterMappings), true); RegisterTypeIfMissing(typeof(IRegionManager), typeof(RegionManager), true); RegisterTypeIfMissing(typeof(IEventAggregator), typeof(EventAggregator), true); RegisterTypeIfMissing(typeof(IRegionViewRegistry), typeof(RegionViewRegistry), true); RegisterTypeIfMissing(typeof(IRegionBehaviorFactory), typeof(RegionBehaviorFactory), true); RegisterTypeIfMissing(typeof(IRegionNavigationJournalEntry), typeof(RegionNavigationJournalEntry), false); RegisterTypeIfMissing(typeof(IRegionNavigationJournal), typeof(RegionNavigationJournal), false); RegisterTypeIfMissing(typeof(IRegionNavigationService), typeof(RegionNavigationService), false); RegisterTypeIfMissing(typeof(IRegionNavigationContentLoader), typeof(UnityRegionNavigationContentLoader), true); }}

RegisterTypeIfMissing()方法确保服务不会被注册两次。
如果不想默认注册服务,调用Bootstrapper.Run(false),然后手动注册服务。

示例:QuickStartBootstrapper(Modularity QuickStart) protect override void ConfigureContainer(){ base.ConfigureContainer(); this.RegisterTypeIfMissing(typeof(IModuleTracker), typeof(ModuleTracker), true); this.Container.RegisterInstance<CallbackLogger>(this.callbackLogger);//CallbackLogger为单例} 在MefBootstrapper中创建并配置Container CreateContainer

在CreateContainer中做了以下几件事:

创建一个AssemblyCatalog和一个CatalogExportProvider(CompositionContainer构造函数中);返回一个CompositionContainer新实例。 ConfigureContainer

这个函数默认注册一组Prism核心服务:

protected virtual void ConfigureContainer(){ this.RegisterBootstrapperProvidedTypes();}protected virtual void RegisterBootstrapperProvidedTypes(){ this.Container.ComposeExportedValue<ILoggerFacade>(this.Logger); this.Container.ComposeExportedValue<IModuleCatalog>(this.ModuleCatalog); this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocatorAdapter(this.Container)); this.Container.ComposeExportedValue<AggregateCatalog>(this.AggregateCatalog);}

注意:在MefBootstrapper中,核心的服务都是单例。

示例:QuickStartBootstrapper()

MefBootstrapper另外两个可以重载的函数来创建和配置AggregateCatalog:

CreateAggregateCatalog;ConfigureAggregateCatalog; protected override void ConfigureAggregateCatalog(){ base.ConfigureAggregateCatalog(); // Add this assembly to export ModuleTracker this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(QuickStartBootstrapper).Assembly)); // Module A is referenced in in the project and directly in code. this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleA.ModuleA).Assembly)); this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleC.ModuleC).Assembly)); // Module B and Module D are copied to a directory as part of a post-build step. // These modules are not referenced in the project and are discovered byinspecting a directory. // Both projects have a post-build step to copy themselves into that directory. DirectoryCatalog catalog = new DirectoryCatalog(“DirectoryModules”); this.AggregateCatalog.Catalogs.Add(catalog);}

来自为知笔记(Wiz)

转载于:https://www.cnblogs.com/qianzi067/p/5804837.html