WPF Prism的简单使用

简单使用

  1. 新建 WPF 项目,我是基于 .net 5.0-windows 创建的。
  2. 引入 Prism.DryIoc(8.1.97) 的 Nuget 包。
  3. App.xaml 中引入命名空间。
xmlns:prism="http://prismlibrary.com/"
  1. 将 App.xaml 中 Application 标签更改成 prism:PrismApplication 并去除 StartUri 属性,将 App.xaml.cs 中 Application 更改成 PrismApplication
  2. 实现 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.
{
   
    
}
  1. 运行成功(如遇到问题可一起交流)。

更进一步

以下将以 prism-samples-wpf 里的项目为说明对象。

01-BootStrapperShell/BootStrapperShell

此项目中并没有在 App.xamlApp.xaml.cs 中引入 PrismApplication ,而是通过新建继承自 PrismBootstrapperBootStrapper 类型,在该类型中实现了 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" /
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值