Caliburn.Micro + Autofac bootstrapping

对着背影说爱祢 提交于 2019-12-04 11:05:24

There are a couple of things wrong with your solution.

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.

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>

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).

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
               };
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!