我在学习Caliburn Micro,并尝试使用official site中的EventAggregator
。Caliburn微型构造器注入失败
但是,我得到了一个异常
“这个对象定义无参数的构造函数。”
消息本身很明显,但该示例并未包含无参数构造函数。如果我添加一个,带参数的构造函数没有命中,并且IEventAggregator
仍未正确注入。
这里是我的出版商VM添加参数的构造函数后(没有它,将引发异常):
public MainViewModel() { }
public MainViewModel(IEventAggregator ea) : this()
{
eventAggregator = ea;
}
这里是我使用的引导程序:
public class AppBootstrapper : Bootstrapper<MainViewModel>
{
private readonly SimpleContainer container = new SimpleContainer();
protected override void Configure()
{
container.Singleton<IEventAggregator, EventAggregator>();
}
}
这里是从CM的例子:
// Creating the EventAggregator as a singleton.
public class Bootstrapper : BootstrapperBase {
private readonly SimpleContainer _container =
new SimpleContainer();
// ... Other Bootstrapper Config
protected override void Configure(){
_container.Singleton<IEventAggregator, EventAggregator>();
}
// ... Other Bootstrapper Config
}
// Acquiring the EventAggregator in a viewModel.
public class FooViewModel {
private readonly IEventAggregator _eventAggregator;
public FooViewModel(IEventAggregator eventAggregator) {
_eventAggregator = eventAggregator;
}
}
我检查了这个帖子(Inject EventAggregator into ViewModel with Caliburn Micro),但它只是不说为什么CM不用注入来调用构造函数。
我也检查了CM的样品解决方案,但它使用MEF作为DI解决方案。
我错过了什么?
它已经有一段时间,因为我看着卡利,但你肯定你的引导程序实际上是启动,可以得到的情况下,等?查看这个http://caliburnmicro.codeplex.com/SourceControl/latest#samples/Caliburn.Micro.HelloSimpleContainer/Caliburn.Micro.HelloSimpleContainer/Bootstrapper.cs简单容器示例,它不使用MEF或任何其他 –
@MaksimS。 Configure()被执行;我试图将Start()添加到引导程序构造函数,但没有运气。此外,该示例不会在虚拟机中注入任何内容。 – ender
目前没有任何东西来测试您的代码;然而,只是看看最新版本中的EventAggregator代码,它_does_包含无参数的构造函数,至少在micro.wpf中。你是否有机会使用Prism的EventAggregator,或者其他的东西,而不是Caliburn的?我知道这听起来很愚蠢,但仍然:) –