Caliburn.Micro + Autofac引导程序 [英] Caliburn.Micro + Autofac bootstrapping

查看:297
本文介绍了Caliburn.Micro + Autofac引导程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Caliburn.Micro项目,我正尝试从其 Autofac .

I have a project with Caliburn.Micro, and I'm trying to port from its SimpleContainer to Autofac.

我正在使用此代码,它是本指南. 我使用SimpleContainer只是做了(在引导程序内部)

I'm using this code, that is an updated version of the code in this guide. Using SimpleContainer I simply did (inside the bootstrapper)

protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
    this.DisplayRootViewFor<IScreen>(); // where ShellViewModel : Screen
}

现在这不再起作用了,那么我应该怎么做才能将Autofac与Caliburn.Micro集成在一起?

Now this doesn't work anymore, so what should I do to integrate Autofac with Caliburn.Micro ?

推荐答案

您的解决方案有很多问题.

There are a couple of things wrong with your solution.

首先,没有任何东西可以调用您的AppBootstrapper.这通常是在Caliburn.Micro中通过将引导程序类型添加为App.xaml中的资源来完成的.有关说明,请参见此处. WPF.

Firstly, nothing invokes your AppBootstrapper. This is normally done in Caliburn.Micro by adding your bootstrapper type as a resource in App.xaml. See here for the instructions for WPF.

即您的App.xaml应该看起来像这样:

i.e. your App.xaml should look like this:

<Application x:Class="AutofacTests.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         xmlns:local="clr-namespace:AutofacTests">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:AppBootstrapper x:Key="bootstrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

第二,由于视图模型和视图位于不同的程序集中,因此Caliburn.Micro和Autofac都需要知道它们的位置(分别用于视图位置和依赖关系解析).

Secondly, as your view models and views are in different assemblies, both Caliburn.Micro and Autofac will need to know where they are located (for view location and dependency resolution respectively).

您正在使用的Autofac引导程序可解决Caliburn.Micro用于查看位置的AssemblySource实例中的依赖项.因此,您只需要填充此程序集源集合即可.您可以通过覆盖AppBootstrapper中的SelectAssemblies来实现此目的:

The Autofac bootstrapper you are using resolves dependencies from the AssemblySource instance that Caliburn.Micro uses for view location. Therefore, you just need to populate this assembly source collection. You do this by overridding SelectAssemblies in your AppBootstrapper:

protected override IEnumerable<Assembly> SelectAssemblies()
{
    return new[]
               {
                   GetType().Assembly, 
                   typeof(ShellViewModel).Assembly, 
                   typeof(ShellView).Assembly
               };
}

这篇关于Caliburn.Micro + Autofac引导程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆