来自Caliburn.Micro.Bootstrapper的自定义引导程序,什么是Bootstrapper <icontract> for?</icontract>

时间:2012-11-24 19:21:13

标签: wpf caliburn.micro

我从Caliburn.Micro.Bootstrapper派生了一个自定义引导程序,我注意到它可以采用泛型类型参数 - 这是为了什么?

public class SimpleInjectorBootstrapper : Caliburn.Micro.Bootstrapper
{
    private Container container;

    public SimpleInjectorBootstrapper()
    {
    }

    protected override void Configure()
    {
        this.container = new Container();
        this.container.Register<IWindowManager, WindowManager>();
        this.container.Register<IEventAggregator, EventAggregator>();
        this.container.Register<IAppViewModel, AppViewModel>();
    }

    protected override object GetInstance(Type serviceType, string key)
    {
        return this.container.GetInstance(serviceType);
    }

    protected override IEnumerable<object> GetAllInstances(Type serviceType)
    {
        return this.container.GetAllInstances(serviceType);
    }

    protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
    {
        base.OnStartup(sender, e);
        var appViewModel = this.container.GetInstance<IAppViewModel>();
        var windowManager = this.container.GetInstance<IWindowManager>();
        windowManager.ShowWindow(appViewModel);
    }
}

1 个答案:

答案 0 :(得分:1)

它是用作起始视图模型的视图模型类型。 Caliburn.Micro将从IoC容器中解析类型,并在WPF中使用WindowManager来显示根视图。基本上你在OnStartup覆盖中正在做什么。