应用启动引导
约 148 字小于 1 分钟
PrismC#MVVMWPF
2025-06-10
在通过 nuget 安装 Prism 后需要对程序进行修改:
- 添加 Bootstrapper 类
- 修改 App.xaml / App.xaml.cs
Bootstrapper.cs 代码:
public class Bootstrapper : PrismBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}这里重写了两个方法,CreateShell 用来返回启动的页面,RegisterTypes 用来进行 IoC 容器内容的注册
App.xaml 需要删除 StartupUri="MainWindow.xaml" 这部分,用来阻止原 WPF 启动流程
App.xaml.cs 增加代码: 在启动时调用 Bootstrapper
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var bootstrapper = new Bootstrapper();
bootstrapper.Run();
}