简单使用
- 新建 WPF 项目,我是基于 .net 5.0-windows 创建的。
- 引入 Prism.DryIoc(8.1.97) 的 Nuget 包。
- App.xaml 中引入命名空间。
xmlns:prism="http://prismlibrary.com/"
- 将 App.xaml 中 Application 标签更改成 prism:PrismApplication 并去除 StartUri 属性,将 App.xaml.cs 中 Application 更改成 PrismApplication。
- 实现 PrismApplication(实际上是 PrismApplicationBase) 中的抽象方法。
protected override Window CreateShell() // Creates the shell or main window of the application
{
return new MainWindow();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry) // Used to register types with the container that will be used by your application.
{
}
- 运行成功(如遇到问题可一起交流)。
更进一步
以下将以 prism-samples-wpf 里的项目为说明对象。
01-BootStrapperShell/BootStrapperShell
此项目中并没有在 App.xaml 和 App.xaml.cs 中引入 PrismApplication ,而是通过新建继承自 PrismBootstrapper 的 BootStrapper 类型,在该类型中实现了 PrismBootStrapperBase 中的两个方法。
protected override Window CreateShell() // Creates the shell or main window of the application. { return Container.Resolve<MainWindow>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) // Used to register types with the container that will be used by your application. { }
同 简单使用 里 App.xaml.cs 实现的两个方法名称与参数都一样,实际上我猜测作用也可能是一样的。
值得注意的是,在BootStrapper类型中CreateShell方法通过 Container.Resolve < MainWindow >() 返回主窗口, 暂且定义此方法的作用是通过类型返回实例。
02-Regions/Regions
此项目以及后续项目都是采用如 简单使用 中的写法,在 Views/MainWindow.xaml 中采用了如下写法:
<ContentControl prism:RegionManager.RegionName="ContentRegion" /