wpf – Caliburn.Micro Autofac bootstrapping

我有一个Caliburn.Micro的项目,我正试图从
SimpleContainer
Autofac.

我正在使用this code,这是this guide中代码的更新版本.
使用SimpleContainer我只是(在引导程序内)

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

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

最佳答案 您的解决方案存在一些问题.

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

即你的App.xaml应如下所示:

<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都需要知道它们的位置(分别用于视图位置和依赖项解析).

您正在使用的Autofac引导程序解析了Caliburn.Micro用于视图位置的AssemblySource实例的依赖关系.因此,您只需要填充此程序集源集合.您可以通过覆盖AppBootstrapper中的SelectAssemblies来执行此操作:

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