2025年轻量级MVVM框架Stylet介绍:(3)关于Bootstrapper「建议收藏」

轻量级MVVM框架Stylet介绍:(3)关于Bootstrapper「建议收藏」Bootstrapper 负责引导应用程序 用于配置 IoC 容器 创建根 ViewModel 的新实例 并使用显示 WindowManage 出来 它还提供了各种其他功能 如下所述 引导程序有两种风格 Bootstrapper 它要求您自己配置 IoC 容器 以及 Bootstrapper 这使用 Stylet 的内置 IoC 容器 StyletIoC 示例引导程序 使用

Bootstrapper负责引导应用程序,用于配置 IoC 容器,创建根 ViewModel 的新实例,并使用显示WindowManager出来。它还提供了各种其他功能,如下所述。

引导程序有两种风格:BootstrapperBase
,它要求您自己配置 IoC 容器,以及Bootstrapper
,这使用 Stylet 的内置 IoC 容器 StyletIoC。

示例引导程序,使用 StyletIoC:

class Bootstrapper : Bootstrapper
{
protected override void OnStart()
{
// This is called just after the application is started, but before the IoC container is set up.
// Set up things like logging, etc
}

protected override void ConfigureIoC(IStyletIoCBuilder builder)
{
// Bind your own types. Concrete types are automatically self-bound.
builder.Bind().To();
}

protected override void Configure()
{
// This is called after Stylet has created the IoC container, so this.Container exists, but before the
// Root ViewModel is launched.
// Configure your services, etc, in here
}

protected override void OnLaunch()
{
// This is called just after the root ViewModel has been launched
// Something like a version check that displays a dialog might be launched from here
}

protected override void OnExit(ExitEventArgs e)
{
// Called on Application.Exit
}

protected override void OnUnhandledException(DispatcherUnhandledExceptionEventArgs e)
{
// Called on Application.DispatcherUnhandledException
}
}

使用定制的IOC容器

将另一个 IoC 容器与 Stylet 配合使用非常简单。我已经在Bootstrappers项目中包含了许多流行的IoC容器的引导程序。这些都是经过单元测试的,但未经实战测试:随意自定义它们。

请注意,Stylet nuget 包/ dll 不包含这些,因为它会添加不必要的依赖项。同样,我不会发布特定于 IoC 容器的包,因为这是浪费精力。

将所需的引导程序从上面的链接复制到项目中的某个位置。然后对它进行子类化,就像您通常对 上文所述进行子类化一样。然后将子类添加到 App.xaml.cs,如快速入门中所述,例如Bootstrapper

public class Bootstrapper : AutofacBootstrapper
{
}

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="https://github.com/canton7/Stylet"
xmlns:local="clr-namespace:Stylet.Samples.Hello">







如果您想为另一个 IoC 容器编写自己的引导程序,那也很容易。看看上面的引导程序,看看你需要做什么。

将资源字典添加到 App.xaml

s:ApplicationLoader 本身就是一个资源字典。如果您需要将自己的资源字典添加到 App.xaml,则必须将 s:ApplicationLoader 嵌套在资源字典中作为合并词典,如下所示:













编程小号
上一篇 2025-08-31 15:40
下一篇 2025-08-09 21:33

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/114489.html