使用Caliburn Micro将EventAggregator注入ViewModel [英] Inject EventAggregator into ViewModel with Caliburn Micro
问题描述
摘自EventAggregator上的Caliburn Micro文档:
// 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;
}
}
所以问题是如何获取由Bootstrapper创建的EA实例以注入到您的VM中?
So the question is how do you get the instance of EA created by Bootstrapper to inject into your VM?
var svm = new SomeViewModel(?);
我尝试使用Caliburn.Micro.IoC.Get方法,但是没有用...
I tried using Caliburn.Micro.IoC.Get method but that didn't work...
推荐答案
不,您不var svm = new SomeViewModel(?)
也不使用IoC.Get,因为服务位置正在成为一种反模式.
本文作者建议的模式是最佳实践,也就是说,您应该通过构造函数注入将依赖项注入需要它们的对象.
我不知道该怎么说,但是要使您的应用程序可组合并为您的表示层创建体系结构.
我会查看屏幕,导体和成分文章,因为它具有与我所说的内容有关的一些很棒的主意以及它所伴随的应用程序就是一个好主意.
我还将阅读有关依赖注入的信息.
No, you don't var svm = new SomeViewModel(?)
and don't use IoC.Get because service location is becoming an anti-pattern.
The pattern suggested by the article author is best-practice, that is you should inject your dependencies to the objects that need them through constructor injection.
I don't know how to say it in any other way, but Make Your Application Composable and create an Architecture for you Presentation Layer.
I would check the Screens, Conductors and Composition article because it has some great ideas related to what i am saying and the application that accompanies it is a great one.
I would also read about dependency injection.
这篇关于使用Caliburn Micro将EventAggregator注入ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!