我觉得这篇文章比较详细的介绍了HelloWorld的这个Sample示例程序,参考地址(http://www.cnblogs.com/hegezhou_hot/archive/2012/12/21/2828162.html)
我在学习过程中的笔记如下:
1.程序入口
WPF程序的入口在vs自动给我们生成的App.xaml和App.xaml.cs文件中体现,目前见到的有两种方式:
①App.xaml中设置启动项:
StartupUri="********.xaml"
②App.xaml.cs中设置启动项(通过override OnStartup函数):
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//TODO your code
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
这个方法在运行app.Run()的时候触发。
2.Bootstrapper
这是一个继承自UnityBootstrapper的类,代码如下:
class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return this.Container.Resolve<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)this.Shell;
App.Current.MainWindow.Show();//Show主窗口,但content内没有内容,只有当调用Module中的Initialize()方法后才将HelloWorldView显示出来。
}
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
moduleCatalog.AddModule(typeof(HelloWorldModule.HelloWorldModule));//注册模块
}
}
注意这句moduleCatalog.AddModule(typeof(HelloWorldModule.HelloWorldModule));它的作用是将HelloWorldModule.HelloWorldModule模块注册到容器中,同时调用Module中的Initialize()方法.
3.bootstrapper.Run(),这是Bootstrapper的入口方法,亦是整个应用的入口方法。在Run()方法里主要调用了应用程序初始化的逻辑,完整顺序如下:
① 创建Logger实例 (Bootstrapper实现,virtual 方法)
this.Logger = this.CreateLogger();
② 创建ModuleCatalog实例(Bootstrapper实现,virtual 方法)
this.ModuleCatalog = this.CreateModuleCatalog();
③ 配置ModuleCatalog实例(Bootstrapper实现,virtual 方法)
this.ConfigureModuleCatalog();
④ 创建DI Container实例 (UnityBootstrapper实现,返回一个UnityContainer的实例,virtual方法)
this.Container = this.CreateContainer();
⑤ 配置DI Container (UnityBootstrapper实现,virtual方法)
this.ConfigureContainer();
⑥ 配置ServiceLocator(UnityBootstrapper重写Bootstrapper abstract方法)
protected override void ConfigureServiceLocator()
{
ServiceLocator.SetLocatorProvider(() => this.Container.Resolve<IServiceLocator>());
}
⑦ 配置Region Adapter的Mapping,主要有三种Region Adapter的mapping:Selector,ItemsControl,ContentControl (Bootstrapper实现,virtual方法)
this.ConfigureRegionAdapterMappings();
⑧ 配置默认的Region Behaviors(Bootstrapper实现,virtaul方法。)主要有: BindRegionContextToDependencyObjectBehavior,RegionActiveAwareBehavior,SyncRegionContextWithHostBehavior等,具体可参考Boostrapper里ConfigureDefaultRegionBehaviors方法。
this.ConfigureDefaultRegionBehaviors();
⑨ 注册Prism framework throw出来的Exception 类型(重写Boostrapper方法)
protected override void RegisterFrameworkExceptionTypes()
{
base.RegisterFrameworkExceptionTypes();
ExceptionExtensions.RegisterFrameworkExceptionType(
typeof(Microsoft.Practices.Unity.ResolutionFailedException));
}
⑩ 创建应用程序Shell(调用Boostrapper定义的abstract方法),在使用Prism时,需要实现此方法。
this.Shell = this.CreateShell();
⑪ 如果创建Shell成功,则调用RegionManager里的SetRegionManager和UpdateRegions方法来处理Region的关联。后面再调用父类的初始化Shell方法(virtual,没有具体实现)。
if (this.Shell != null)
{
this.Logger.Log(Resources.SettingTheRegionManager, Category.Debug, Priority.Low);
RegionManager.SetRegionManager(this.Shell,this.Container.Resolve<IRegionManager>());
this.Logger.Log(Resources.UpdatingRegions, Category.Debug, Priority.Low);
RegionManager.UpdateRegions();
this.Logger.Log(Resources.InitializingShell, Category.Debug, Priority.Low);
this.InitializeShell();
}
⑫ 初始化Modules,重写父类方法
if (this.Container.IsRegistered<IModuleManager>())
{
this.Logger.Log(Resources.InitializingModules, Category.Debug, Priority.Low);
this.InitializeModules();
}
至此,Run()方法的工作完成。
4.HelloWorldModule
HelloWorldModule类的代码如下:
public class HelloWorldModule : IModule
{
private readonly IRegionViewRegistry regionViewRegistry;
public HelloWorldModule(IRegionViewRegistry registry)
{
this.regionViewRegistry = registry;
}
public void Initialize()
{
regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(Views.HelloWorldView));
}
}
它继承了IModule接口,并且实现了IModule接口的Initialize方法,RegisterViewWithRegion(“MainRegion”, typeof(Views.HelloWorldView))的作用是将HelloWorldView视图注册到MainRegion区域,可能大家会有疑问,MainRegion区域是什么,观察Shell.xaml中的以下一行代码:
<ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />
这里cal:RegionManager.RegionName是一个依赖属性,我们将它与ItemsControl控件相关联,MainRegion就是一个占位符,可以通俗的理解为这块地(区域)被开发商(MainRegion)占着,之后开发商(MainRegion)会在这块地(区域)上开发各种楼盘。这里的楼盘就是上述的HelloWorldView视图。
总结:这是Prism的入门Demo,但也涉及到了区域,视图,Module及其它的基本知识。后续的笔记会有更多的Prism框架的知识。
本文详细解析了WPF Prism框架的HelloWorld示例,从程序入口、Bootstrapper、Module Catalog到具体模块的初始化过程。通过App.xaml和App.xaml.cs设置启动项,Bootstrapper执行一系列配置,包括DI Container、Region Adapter Mapping和Region Behavior。HelloWorldModule注册视图到MainRegion,展示了Prism框架区域管理的基础概念。这是一个学习Prism的入门教程。

373

被折叠的 条评论
为什么被折叠?



