发布时间:2025-12-09 14:14:39 浏览次数:6
Bootstrapper类的责任是使用Prism类库初始化应用程序,抽象类Bootstrapper提供的函数大多都是虚方法。
UnityBootstrapper和MefBootstrapper类实现了大多数必须的功能。
在Prism应用中,创建Shell或者主窗口的责任是Bootstrapper的。因为Shell依赖的一些服务比如Region Manager需要在Shell显示之前初始化。
返回你应用程序Shell类的实例,可以根据需求选择创建Shell对象或者从Container中获取Shell。
protected override DependencyObject CreateShell(){
return this.Container.Resolve<Shell>();}创建Shell之后显示之前,需要为应用程序设置主界面。
protected override void InitializeShell(){
base.InitializeShell(); //基类的InitializeShell()没有做任何事。 App.Current.MainWindow = (Window) this.Shell; App.Current.MainWindow.Show();}抽象类Bootstrapper的CreateModuleCatalog()返回new ModuleCatalog。
protected virtual IModuleCatalog CreateModuleCatalog(){
return new ModuleCatalog();}UnityBootstrapper和MEFBootstrapper类的Run方法都调用CreateModuleCatalog()方法,为ModuleCatalog属性设置返回值。
Container在Prism应用中扮演关键角色,Prism类库和应用程序需要Container来提供服务、模块等的依赖关系。在Container的Configuration过程中,需要的核心服务被注册。
| 服务接口 | 描述 |
|---|---|
| IModuleManager | 检索、初始化应用程序模块的接口 |
| IModuleCatalog | 包含模块的元数据;Prism框架提供一些不同的模块分类 |
| IModuleInitializer | 初始化模块 |
| IRegionManager | 注册、检索Region的接口 |
| IEventAggregator | 一组松耦合的事件 |
| ILoggerFacade | 框架为日志服务提供了包装机制,日志服务在bootstrapper.Run()方法中注册。使用CreateLogger方法的返回值,所以如果需要定制日志服务,需要覆写CreateLogger()方法 |
| IServiceLocator | 允许Prism 类库访问Container。如果需要定制或者扩展类库将会很有用 |
| Stock Trader RI 中的服务 | 描述 |
|---|---|
| IMarketFeedService | 提供实时的市场数据,PositionSummaryViewModel使用此服务 |
| IMarketHistoryService | 提供市场历史数据 |
| IAccountPositionService | 提供基金列表 |
| IOrdersService | 提交订单服务 |
| INewsFeedService | 提供选择基金的新闻 |
| IWatchListService | 控制添加新的监控数据到监控列表 |
UnityBootstrapper的CreateContainer方法返回一个UnityContainer的实例,通常这种行为不需要被改变,如果要改变,覆盖这个方法即可。
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),然后手动注册服务。
protect override void ConfigureContainer(){
base.ConfigureContainer(); this.RegisterTypeIfMissing(typeof(IModuleTracker), typeof(ModuleTracker), true); this.Container.RegisterInstance<CallbackLogger>(this.callbackLogger);//CallbackLogger为单例}在CreateContainer中做了以下几件事:
这个函数默认注册一组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中,核心的服务都是单例。
MefBootstrapper另外两个可以重载的函数来创建和配置AggregateCatalog:
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);}转载于:https://www.cnblogs.com/qianzi067/p/5804837.html