2.初始化(第一种)

初始化

Prism框架项目的初始化,可以使用两种启动器类型来进行,分别是PrismBootstrapperPrismApplication

一、PrismBootstrapper

 

使用PrismBootstrapper来进行WPF项目的初始化,需要通过PrismBootstrapper对象来进行项目的启动。
PrismBootstrapper进行项目的启动,主要是为了通过PrismBootstrapper对象来进行项目IOC容器的初始化,为后续IOC容器管理对象做基础。
要通过PrismBootstrapper来启动项目,首先要创建一个PrismBootstrapper的子类,并实现两个方法成员:

DependencyObject CreateShell():进行主窗口对象的创建。
主窗口对象需要通过Container.Resolve<MainWindow>()来创建。
void RegisterTypes(IContainerRegistry containerRegistry):这个函数主要用于进行IOC容器管理类型的注册。
最终在App后台代码中调用PrismBootstrapper对象的Run()方法来启动项目

Run():PrismBootstrapperBase的实例方法,用于初始化、启动IOC容器、展示窗口等操作。


创建启动类

public class Bootstrapper : PrismBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        //创建并返回一个主窗口对象
        return Container.Resolve<MainWindow>();
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        //这个函数中进行一些类型注册
    }
}

消除App.xaml的启动属性

<Application x:Class="Command.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Command">
                         <!--这里消除掉了原有的 StartupUri="MainWindow.xaml" 属性-->
    <Application.Resources>
    </Application.Resources>
</Application>

在App后台代码中通过PrismBootstrapper来启动项目

public partial class App : Application
{
    public App()
    {
        new Bootstrapper().Run();
    }
}

 

posted @ 2025-12-19 13:47  家煜宝宝  阅读(0)  评论(0)    收藏  举报