zoukankan      html  css  js  c++  java
  • 轻量级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="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">
        <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>
    
  • 相关阅读:
    solr6.5搭建以及使用经验
    Intellij IDEA svn的使用记录
    初次从eclipse转到intellij idea上的一些经验
    Linux一键安装PHP/JAVA环境OneinStack
    CentOS下yum安装mysql,jdk以及tomcat
    centos yum换阿里云源
    ehcache配置:使用Spring+SpringMVC+Mybatis或者有shiro
    微博短链接的生成算法(Java版本)
    手把手教你使用Git
    工作中有关分布式缓存的使用和需要注意的问题梳理
  • 原文地址:https://www.cnblogs.com/qouoww/p/15785963.html
Copyright © 2011-2022 走看看