tnblog
首页
视频
资源
登录

WPF Prism 框架初始化

334人阅读 2025/4/24 17:31 总访问:3661674 评论:0 收藏:0 手机
分类: .net后台框架

WPF Prism 框架初始化

什么是 Prism Bootstrapper?


Prism Bootstrapper 是一个抽象类,它定义了一个基本的启动序列,用于初始化应用程序的关键组件。
它允许你在应用程序启动时配置和初始化各个模块。

创建一个自定义 Bootstrapper


要使用 Prism Bootstrapper,你需要创建一个继承自 PrismBootstrapper 的自定义类,并重写其方法以配置你的应用程序。

  1. public class Bootstrapper : PrismBootstrapper
  2. {
  3. /// <summary>
  4. /// 创建应用程序的 Shell(主窗口)
  5. /// </summary>
  6. /// <returns></returns>
  7. protected override DependencyObject CreateShell()
  8. {
  9. // 使用 Unity 容器解析 MainWindow 作为应用程序的 Shell
  10. return Container.Resolve<MainWindow>();
  11. }
  12. /// <summary>
  13. /// 注册应用程序中使用的类型到依赖注入容器中
  14. /// </summary>
  15. /// <param name="containerRegistry"></param>
  16. protected override void RegisterTypes(IContainerRegistry containerRegistry)
  17. {
  18. // 在此处注册应用程序中使用的各种服务和视图模型到依赖注入容器中
  19. // 例如:
  20. // containerRegistry.Register<IMyService, MyServiceImpl>();
  21. }
  22. }


然后我们修改一下App.xaml,将StartupUri="MainWindow.xaml">注释掉。

  1. <Application x:Class="LearningPrism.App"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:LearningPrism"
  5. >
  6. <!--StartupUri="MainWindow.xaml">-->
  7. <Application.Resources>
  8. </Application.Resources>
  9. </Application>


然后我们在App.xaml.cs中的构造函数运行我们刚刚的Bootstrapper

  1. public partial class App : Application
  2. {
  3. public App() {
  4. new Bootstrapper().Run();
  5. }
  6. }


这样我们就使用上了WPF Prism Unity的IOC了。
运行一下没有任何问题。

直接使用PrismApplication初始化


我们首先新建一个LearningPrismUnityIoC项目,防止学习混乱。
安装好下面三个包:

  1. <ItemGroup>
  2. <PackageReference Include="Prism.Core" Version="9.0.537" />
  3. <PackageReference Include="Prism.Unity" Version="9.0.537" />
  4. <PackageReference Include="Prism.Wpf" Version="9.0.537" />
  5. </ItemGroup>


然后我们修改App.xaml标签,将Application标签改成prism:PrismApplication,注意还需要引用xmlns:prism="http://prismlibrary.com/"

  1. <prism:PrismApplication
  2. xmlns:prism="http://prismlibrary.com/"
  3. x:Class="LearningPrismUnityIoC.App"
  4. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6. xmlns:local="clr-namespace:LearningPrismUnityIoC">
  7. <Application.Resources>
  8. </Application.Resources>
  9. </prism:PrismApplication>


然后我们在App.xaml.cs将继承的基类改为PrismApplication

  1. public partial class App : PrismApplication
  2. {
  3. protected override Window CreateShell()
  4. {
  5. return Container.Resolve<MainWindow>();
  6. }
  7. protected override void RegisterTypes(IContainerRegistry containerRegistry)
  8. {
  9. }
  10. }


为了方便演示我在MainWindow.xaml中添加了一个按钮。

  1. <Window x:Class="LearningPrismUnityIoC.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:LearningPrismUnityIoC"
  7. mc:Ignorable="d"
  8. Title="MainWindow" Height="450" Width="800">
  9. <Grid>
  10. <Button Content="按钮" Margin="0,0,672,364"></Button>
  11. </Grid>
  12. </Window>

登录示例一


如果我们在进入到MainWindow之前,我们需要先进入登录页LoginWindow.xaml怎么做呢?
首先我们需要创建一下LoginWindow.xaml,然后添加一个按钮并设置当按钮被按下时DialogResult的值改为true

  1. <Window x:Class="LearningPrismUnityIoC.LoginWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:LearningPrismUnityIoC"
  7. mc:Ignorable="d"
  8. Title="LoginWindow" Height="450" Width="800">
  9. <Grid>
  10. <Button Content="打开MainWindows" FontSize="50" Click="Button_Click"></Button>
  11. </Grid>
  12. </Window>
  1. public partial class LoginWindow : Window
  2. {
  3. public LoginWindow()
  4. {
  5. InitializeComponent();
  6. }
  7. private void Button_Click(object sender, RoutedEventArgs e)
  8. {
  9. this.DialogResult = true;
  10. }
  11. }


然后我们在App中的CreateShell方法中进行判断当LoginWindow被打开并且DialogResult值为true的时候,将会打开MainWindow窗体。

  1. public partial class App : PrismApplication
  2. {
  3. protected override Window CreateShell()
  4. {
  5. if (Container.Resolve<LoginWindow>().ShowDialog() == true)
  6. {
  7. return Container.Resolve<MainWindow>();
  8. }
  9. return null;
  10. }
  11. protected override void RegisterTypes(IContainerRegistry containerRegistry)
  12. {
  13. }
  14. }


除此之外,我们还需要在App.xaml中添加一个属性ShutdownMode="OnExplicitShutdown",完整代码如下所示:

  1. <prism:PrismApplication
  2. xmlns:prism="http://prismlibrary.com/"
  3. x:Class="LearningPrismUnityIoC.App"
  4. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6. xmlns:local="clr-namespace:LearningPrismUnityIoC"
  7. ShutdownMode="OnExplicitShutdown"
  8. >
  9. <Application.Resources>
  10. </Application.Resources>
  11. </prism:PrismApplication>

ShutdownMode 设置为 OnExplicitShutdown 时,应用程序不会在任何窗口关闭时自动终止,而是必须通过显式调用 Application.Current.Shutdown() 方法来手动结束。


好我们开始运行测试一下:


当然我们可以不用ShutdownMode="OnExplicitShutdown"还有另外一种写法,通过InitializeShell方法进行实现完成。

  1. protected override Window CreateShell()
  2. {
  3. return Container.Resolve<MainWindow>();
  4. //if (Container.Resolve<LoginWindow>().ShowDialog() == true)
  5. //{
  6. // return Container.Resolve<MainWindow>();
  7. //}
  8. //return null;
  9. }
  10. protected override void InitializeShell(Window shell)
  11. {
  12. var loginWin = Container.Resolve<LoginWindow>();
  13. if (loginWin == null || loginWin.ShowDialog() != true) {
  14. Application.Current.Shutdown();
  15. }
  16. }
  17. protected override void RegisterTypes(IContainerRegistry containerRegistry)
  18. {
  19. }


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价

WPF Prism 框架:打造高效、可维护的 WPF 应用

WPF Prism 框架:打造高效、可维护的 WPF 应用[TOC] Prism 框架简介Prism 是一个用于构建松耦合、可维护且可测试的 XAML ...

WPF Prism ViewModel的应用

WPF Prism ViewModel的应用[TOC] 在 WPF 开发中,Prism 是一个非常流行的框架,它基于 MVVM(Model-View-ViewModel)模式...

WPF Prism Dialog与Region

WPF Prism Dialog与Region[TOC] Prism框架中的Dialog子窗口处理在 WPF 应用程序开发中,对话框是一个常见的功能需求,无论...

WPF Prism 复合命令与模块化管理

WPF Prism 复合命令与模块化管理[TOC] Prism 中的 CompositeCommand 示例CompositeCommand 是 Prism 框架中用于组合多个子...
这一世以无限游戏为使命!
排名
2
文章
657
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 : 好是好,这个对效率影响大不大哇,效率高不高
ASP.NET Core 服务注册生命周期
剑轩 : http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术