Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Caliburn Micro将EventAggregator注入ViewModel_C#_Mvvm_Caliburn.micro - Fatal编程技术网

C# 使用Caliburn Micro将EventAggregator注入ViewModel

C# 使用Caliburn Micro将EventAggregator注入ViewModel,c#,mvvm,caliburn.micro,C#,Mvvm,Caliburn.micro,摘自关于EventAggregator的Caliburn Micro文档: //将EventAggregator创建为单例。 公共类引导程序:引导程序数据库{ 专用只读SimpleContainer\u容器= 新的SimpleContainer(); //…其他引导程序配置 受保护的覆盖无效配置(){ _container.Singleton(); } //…其他引导程序配置 } //获取viewModel中的EventAggregator。 公共类视图模型{ 私有只读ieventagegrat

摘自关于EventAggregator的Caliburn Micro文档:

//将EventAggregator创建为单例。
公共类引导程序:引导程序数据库{
专用只读SimpleContainer\u容器=
新的SimpleContainer();
//…其他引导程序配置
受保护的覆盖无效配置(){
_container.Singleton();
}
//…其他引导程序配置
}
//获取viewModel中的EventAggregator。
公共类视图模型{
私有只读ieventagegrator\u事件聚合器;
public FooViewModel(IEventAggregator事件聚合器){
_eventAggregator=eventAggregator;
}
}
所以问题是如何让Bootstrapper创建的EA实例注入到您的VM中

var-svm=新的SomeViewModel(?)


我尝试使用Caliburn.Micro.IoC.Get方法,但没有成功…

不,您没有
var svm=new SomeViewModel(?)
也没有使用IoC.Get,因为服务位置正在成为一种反模式。
文章作者建议的模式是最佳实践,即您应该通过构造函数注入将依赖项注入到需要它们的对象中。
我不知道如何用任何其他方式来表达,但要使您的应用程序可组合,并为您的表示层创建一个体系结构。
我会检查这篇文章,因为它有一些与我所说的内容相关的好主意,并且它附带的应用程序是一个好主意。

我还将阅读有关依赖注入的文章。

我写了您正在引用的文章。嗅探器是正确的(请把绿色记号留给他)。Micro在一个叫做合成的概念上投入了大量资金。这意味着整个对象图是在运行时隐式构造的,如果您愿意,也可以是组合的

其思想是,您的“shell”视图模型由引导程序创建,shell反过来创建其他视图模型,依此类推。这允许使用构造函数注入,并提供最佳的可组合性

然而,有时这不是我们想要的功能,为此,我们通过IoC类提供了一个服务定位器;正如Sniffer所说,服务位置的大多数用例都被认为是反模式的,因此它的使用应该被严格审查,否则它会在路上咬你的屁股


我正在为IoC和我们的内置依赖容器SimpleContainer的两篇新文章做最后的润色,一旦这些文章完成,我将添加到EventAggregator文档的相关链接,这些文档将提供更多关于注入站点和最佳实践的上下文。

这只是一个例子,一旦您将IEventAggregator eventAggregator添加到ViewModel构造函数中,并且不再具有默认构造函数,则在实例化VM时必须传递EA实例。。。我的问题是如何让实例将其传递到VM构造函数…@DeanKuga这是我的观点,您不需要手动创建视图模型,而是让CaliburnMicro从引导程序为您创建整个对象图,如果您有一个ViewModel,您通过构造函数接收eventAggregator,通过只读实例变量保留对它的引用,如果ViewModel需要手动创建在其构造函数中采用IEventaggGator的其他ViewModel,请在ViewModel中使用此实例。您有这些链接吗?非常感兴趣的是以“适当”的方式将UserControls添加到需要依赖项注入的视图中。Caliburn.Micro的新增功能。它们现在分别在EventAggregator和SimpleContainer下运行
// 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;
    }
}