我有一个Caliburn.Micro项目,我正在尝试从它的SimpleContainer移植到Autofac。
我使用的是这段代码,这是本指南中代码的更新版本。使用SimpleContainer,我只做了(在引导程序中)
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
this.DisplayRootViewFor<IScreen>(); // where ShellViewModel : Screen
}现在这已经不起作用了,那么我应该如何将Autofac与Caliburn.Micro集成呢?
发布于 2013-11-19 10:00:02
你的解决方案有几个问题。
首先,没有任何东西调用您的AppBootstrapper。这通常是在Caliburn.Micro中通过将引导程序类型添加为App.xaml中的资源来完成的。有关WPF的说明,请参见这里。
也就是说,您的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用于视图位置的Caliburn.Micro实例中的依赖关系。因此,只需填充此程序集源集合即可。通过在您的SelectAssemblies中重写AppBootstrapper来做到这一点。
protected override IEnumerable<Assembly> SelectAssemblies()
{
return new[]
{
GetType().Assembly,
typeof(ShellViewModel).Assembly,
typeof(ShellView).Assembly
};
}https://stackoverflow.com/questions/20056673
复制相似问题