
WPF Prism 框架初始化
什么是 Prism Bootstrapper?
Prism Bootstrapper 是一个抽象类,它定义了一个基本的启动序列,用于初始化应用程序的关键组件。
它允许你在应用程序启动时配置和初始化各个模块。
创建一个自定义 Bootstrapper
要使用 Prism Bootstrapper,你需要创建一个继承自 PrismBootstrapper 的自定义类,并重写其方法以配置你的应用程序。
public class Bootstrapper : PrismBootstrapper
{
/// <summary>
/// 创建应用程序的 Shell(主窗口)
/// </summary>
/// <returns></returns>
protected override DependencyObject CreateShell()
{
// 使用 Unity 容器解析 MainWindow 作为应用程序的 Shell
return Container.Resolve<MainWindow>();
}
/// <summary>
/// 注册应用程序中使用的类型到依赖注入容器中
/// </summary>
/// <param name="containerRegistry"></param>
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// 在此处注册应用程序中使用的各种服务和视图模型到依赖注入容器中
// 例如:
// containerRegistry.Register<IMyService, MyServiceImpl>();
}
}
然后我们修改一下App.xaml
,将StartupUri="MainWindow.xaml">
注释掉。
<Application x:Class="LearningPrism.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LearningPrism"
>
<!--StartupUri="MainWindow.xaml">-->
<Application.Resources>
</Application.Resources>
</Application>
然后我们在App.xaml.cs
中的构造函数运行我们刚刚的Bootstrapper
。
public partial class App : Application
{
public App() {
new Bootstrapper().Run();
}
}
这样我们就使用上了WPF Prism Unity
的IOC了。
运行一下没有任何问题。
直接使用PrismApplication初始化
我们首先新建一个LearningPrismUnityIoC
项目,防止学习混乱。
安装好下面三个包:
<ItemGroup>
<PackageReference Include="Prism.Core" Version="9.0.537" />
<PackageReference Include="Prism.Unity" Version="9.0.537" />
<PackageReference Include="Prism.Wpf" Version="9.0.537" />
</ItemGroup>
然后我们修改App.xaml
标签,将Application
标签改成prism:PrismApplication
,注意还需要引用xmlns:prism="http://prismlibrary.com/"
。
<prism:PrismApplication
xmlns:prism="http://prismlibrary.com/"
x:Class="LearningPrismUnityIoC.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LearningPrismUnityIoC">
<Application.Resources>
</Application.Resources>
</prism:PrismApplication>
然后我们在App.xaml.cs
将继承的基类改为PrismApplication
。
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
为了方便演示我在MainWindow.xaml
中添加了一个按钮。
<Window x:Class="LearningPrismUnityIoC.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LearningPrismUnityIoC"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="按钮" Margin="0,0,672,364"></Button>
</Grid>
</Window>
登录示例一
如果我们在进入到MainWindow
之前,我们需要先进入登录页LoginWindow.xaml
怎么做呢?
首先我们需要创建一下LoginWindow.xaml
,然后添加一个按钮并设置当按钮被按下时DialogResult
的值改为true
。
<Window x:Class="LearningPrismUnityIoC.LoginWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LearningPrismUnityIoC"
mc:Ignorable="d"
Title="LoginWindow" Height="450" Width="800">
<Grid>
<Button Content="打开MainWindows" FontSize="50" Click="Button_Click"></Button>
</Grid>
</Window>
public partial class LoginWindow : Window
{
public LoginWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
}
然后我们在App
中的CreateShell
方法中进行判断当LoginWindow
被打开并且DialogResult
值为true
的时候,将会打开MainWindow
窗体。
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
if (Container.Resolve<LoginWindow>().ShowDialog() == true)
{
return Container.Resolve<MainWindow>();
}
return null;
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
除此之外,我们还需要在App.xaml
中添加一个属性ShutdownMode="OnExplicitShutdown"
,完整代码如下所示:
<prism:PrismApplication
xmlns:prism="http://prismlibrary.com/"
x:Class="LearningPrismUnityIoC.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LearningPrismUnityIoC"
ShutdownMode="OnExplicitShutdown"
>
<Application.Resources>
</Application.Resources>
</prism:PrismApplication>
当 ShutdownMode
设置为 OnExplicitShutdown
时,应用程序不会在任何窗口关闭时自动终止,而是必须通过显式调用 Application.Current.Shutdown()
方法来手动结束。
好我们开始运行测试一下:
当然我们可以不用ShutdownMode="OnExplicitShutdown"
还有另外一种写法,通过InitializeShell
方法进行实现完成。
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
//if (Container.Resolve<LoginWindow>().ShowDialog() == true)
//{
// return Container.Resolve<MainWindow>();
//}
//return null;
}
protected override void InitializeShell(Window shell)
{
var loginWin = Container.Resolve<LoginWindow>();
if (loginWin == null || loginWin.ShowDialog() != true) {
Application.Current.Shutdown();
}
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

