wpf 使用 MVVM Light 的 Messenger 在视图模型之间传递值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18087906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:22:55  来源:igfitidea点击:

Use MVVM Light's Messenger to Pass Values Between View Model

c#wpfmvvm-light

提问by DanteTheEgregore

Could someone be so kind as to explain MVVM Light's Messenger for me? I was reading a post on StackOverflow here: MVVM pass values between view modelstrying to get this. The documentation on MVVM Light's not that great at this point so I'm completely unsure where to go.

有人可以为我解释 MVVM Light 的 Messenger 吗?我正在阅读 StackOverflow 上的一篇文章:MVVM 在视图模型之间传递值试图得到这个。关于 MVVM Light 的文档目前还不是很好,所以我完全不确定该去哪里。

Say I have two ViewModels and a ViewModelLocator. I want to be able to pass parameters between all three without issue. How would I go about doing this with the messenger? Is it capable of that?

假设我有两个 ViewModel 和一个 ViewModelLocator。我希望能够在所有三个之间传递参数而不会出现问题。我将如何与信使一起做这件事?它有这个能力吗?

Edit: Here's my new implementation. As of now, it looks as if MessengerInstance doesn't call for a token. I'm terribly confused.

编辑:这是我的新实现。到目前为止,看起来 MessengerInstance 没有调用令牌。我非常困惑。

In the first ViewModel:

在第一个 ViewModel 中:

MessengerInstance.Send<XDocument>(SelectedDocument);

And in the second:

在第二个:

MessengerInstance.Register<XDocument>(this, xdoc => CopySettings(xdoc));

Could be completely wrong. Haven't gotten a chance to test it, but visual studio gets less angry with me when I do it this way. Also the MessengerInstance does register before the Message is sent.

可能完全错误。还没有机会测试它,但是当我这样做时,visual studio 对我的生气就少了。此外 MessengerInstance 确实在发送消息之前注册。

回答by devuxer

Say I have two ViewModels and a ViewModelLocator. I want to be able to pass parameters between all three without issue. How would I go about doing this with the messenger? Is it capable of that?

假设我有两个 ViewModel 和一个 ViewModelLocator。我希望能够在所有三个之间传递参数而不会出现问题。我将如何与信使一起做这件事?它有这个能力吗?

That's exactly what it's for, yes.

这正是它的用途,是的。

To send a message:

发送消息:

MessengerInstance.Send(payload, token);

To receive a message:

接收消息:

MessengerInstance.Register<PayloadType>(
    this, token, payload => SomeAction(payload));

There are many overloads, so without knowing exactly what you're trying to accomplish via the messenger, I won't go into all of them, but the above should cover the simple case of wanting to send and receive a message with a payload.

有许多重载,因此在不确切知道您要通过 Messenger 完成什么的情况下,我不会介绍所有这些重载,但以上内容应该涵盖想要发送和接收带有有效载荷的消息的简单情况。

Note that "token" can be really anything that identifies the message. While a string is often used for this, I prefer to use an enum because it's a little safer and enables intellisense, "find usages", etc.

请注意,“令牌”实际上可以是任何标识消息的东西。虽然字符串经常用于此,但我更喜欢使用枚举,因为它更安全一点,并且启用智能感知、“查找用法”等。

For example:

例如:

public enum MessengerToken
{
    BrushChanged,
    WidthChanged,
    HeightChanged
}

Then your send/receive would be something like:

那么您的发送/接收将类似于:

// sending view model
MessengerInstance.Send(Brushes.Red, MessengerToken.BrushChanged);

// receiving view model

// put this line in the constructor
MessengerInstance.Register<Brush>(this, token, brush => ChangeColor(brush));

public void ChangeColor(Brush brush)
{
    Brush = brush;
}

[EDIT] URL to devuxer's comment below changed to: http://blog.galasoft.ch/posts/2009/09/mvvm-light-toolkit-messenger-v2/

[编辑] 下面 devuxer 评论的 URL 更改为:http: //blog.galasoft.ch/posts/2009/09/mvvm-light-toolkit-messenger-v2/