轻量级MVVM框架Stylet介绍:(3)关于Bootstrapper

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

轻量级MVVM框架Stylet介绍:(3)关于Bootstrapper Bootstrapper负责引导应⽤程序,⽤于配置 IoC 容器,创建根 ViewModel 的新实例,并使⽤显⽰WindowManager出来。

它还提供了各种其他功能,如下所述。

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

⽰例引导程序,使⽤ StyletIoC:
class Bootstrapper : Bootstrapper<MyRootViewModel>
{
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<IMyInterface>().To<MyType>();
}
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<TRootViewModel>
public class Bootstrapper : AutofacBootstrapper<ShellViewModel>
{
}
<Application x:Class="Stylet.Samples.Hello.App"
xmlns="/winfx/2006/xaml/presentation"
xmlns:x="/winfx/2006/xaml"
xmlns:s="https:///canton7/Stylet"
xmlns:local="clr-namespace:Stylet.Samples.Hello">
<Application.Resources>
<s:ApplicationLoader>
<s:ApplicationLoader.Bootstrapper>
<local:Bootstrapper/>
</s:ApplicationLoader.Bootstrapper>
</s:ApplicationLoader>
</Application.Resources>
</Application>
如果您想为另⼀个 IoC 容器编写⾃⼰的引导程序,那也很容易。

看看上⾯的引导程序,看看你需要做什么。

将资源字典添加到 App.xaml
s:ApplicationLoader 本⾝就是⼀个资源字典。

如果您需要将⾃⼰的资源字典添加到 App.xaml,则必须将 s:ApplicationLoader 嵌套在资源字典中作为合并词典,如下所⽰:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<s:ApplicationLoader>
<s:ApplicationLoader.Bootstrapper>
<local:Bootstrapper/>
</s:ApplicationLoader.Bootstrapper>
</s:ApplicationLoader>
<ResourceDictionary Source="YourDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>。

相关文档
最新文档