Prism MVVM - 如何将 IEventAggregator 传递给我的 ViewModel [英] Prism MVVM - How to pass an IEventAggregator to my ViewModel

查看:125
本文介绍了Prism MVVM - 如何将 IEventAggregator 传递给我的 ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始在 Silverlight 中使用 Prism.我想使用 EventAggregator 在两个 ViewModel 之间订阅和发布事件.正如我在一些指南中看到的,ViewModel 的构造函数应该接受 IEventAggregator 作为参数.我不知道如何做到这一点,因此我的视图总是想用无参数的构造函数初始化 ViewModel.

recently I started working with Prism in Silverlight. I want to use the EventAggregator to Subscribe and Publish events between two ViewModels. As I saw on some guides, the ViewModel's ctor should accept IEventAggregator as a parameter. I can't find out how to do this hence my View always wants to initialize the ViewModel with a parameterless ctor.

我的 ViewModel ctor:

My ViewModel ctor:

MyViewModel(IEventAggregator eventAggregator)
{
    // get the event....
}

我的观点:

<UserControl ....>

    <UserControl.Resources>
        <ViewModels:MyViewModel x:Key="MyViewModel"/>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MyViewModel}}">
    ....
    <Grid/>

</UserControl>

我可以在 View 的 ctor 中实例化 ViewModel,然后将其分配给它的 DataContext,但是我的 View 中必须有一个 IEventAggregator,我也无法获得.但这可能不是将 IEventAggregator(或任何其他对象!-例如 IUnityContainer)传递给 ViewModel 的正确方法.

I can instantiate the ViewModel in the ctor of the View, and then assign it to its DataContext, but then I must have an IEventAggregator in my View, which I also cannot get. but this is probably not the correct way to pass an IEventAggregator (or any other object! - IUnityContainer for example) to the ViewModel.

谁能告诉我我做错了什么?

Can someone tell me what I'm doing wrong?

推荐答案

你必须通过统一来解决你的依赖.看看棱镜 MVVM 示例和 ui 组合.在那里,视图不会创建视图模型,但恰恰相反.视图模型通过构造函数注入获取注入的视图.视图模型将自己设置为视图的视图模型:

You have to resolve your dependency via unity. Have a look at the prism MVVM examples and the ui composition. There the view does not create the view model, but it is exactly the other way round. The view model gets the view injected via constructor injection. The view model sets itself as view model for the view:

public interface IView
{
    IViewModel ViewModel{get;set;}
}

public interface IViewModel { }

public View:UserControl, IView
{
    public IViewModel ViewModel
    {
        get{return DataContext as IViewModel;}
        set{DataContext = value;}
    }
}

public ViewModel:IViewModel
{
    public ViewModel(IView view, IEventAggregator eventAggregator)
    {
        view.ViewModel = this;
        //get the event...
    }
}

使用这种方法,您必须将视图模型和视图注册为统一.之后你只需要解析视图模型,视图由容器注入.

Using this approach you have to register the view model and the view to unity. Afterwards you only have to resolve the view model, the view is injected by the container.

要将视图放置在用户界面上的正确位置,您必须使用 RegionManager 将视图注册到区域.设置完成后,创建一个新的视图模型实例会将视图添加到注册区域中,以便它显示在用户界面上.

To get the view to the right place on the user interface you have to register the view to a region using the RegionManager. When this is all set up, creating a new view model instance results in adding the view into the registered region so that it shows up on the user interface.

这篇关于Prism MVVM - 如何将 IEventAggregator 传递给我的 ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆