使用MVVM光的使者传递到视图模型之间的值 [英] Use MVVM Light's Messenger to Pass Values Between View Model
问题描述
可能有人会这么好心来解释MVVM光的使者给我吗?我在这里读在计算器上一个帖子: MVVM视图模型之间传递值努力得到这个。在MVVM光的文档是不是很大在这一点上,所以我完全不能确定去哪里。
说我有两个的ViewModels和ViewModelLocator。我希望能够通过这三个参数之间没有问题。我怎么会去的使者这样做呢? ?难道能够说
编辑:这是我的新的实现。截至目前,它看起来好像MessengerInstance并不要求一个令牌。 。我非常困惑
在第一个视图模型:
MessengerInstance.Send<&的XDocument GT;(SelectedDocument);
和第二:
MessengerInstance.Register<&的XDocument GT;(这一点,XDOC => CopySettings(XDOC));
可能是完全错误的。没有得到一个机会来测试,但视觉工作室变得少跟我生气时,我做这种方式。另外,MessengerInstance并注册发送邮件之前。
说我有两个的ViewModels和ViewModelLocator。我希望能够通过这三个参数之间没有问题。我怎么会去的使者这样做呢?难道能够说?
块引用>
这也正是它的,是的。
要发送消息:
MessengerInstance.Send(有效载荷,令牌);
要收到一条消息:
MessengerInstance.Register< PayloadType>(
本,令牌,有效载荷= GT; SomeAction(负载));
有很多重载,所以不知道你想通过信使来完成什么,我不会进入所有的人,但上面应覆盖想要发送和接收有效载荷的消息的简单情况。
请注意,令牌可以是真的什么标识消息。虽然字符串经常被用于此,我更喜欢,因为它是一个小更安全,更使智能感知使用一个枚举,发现习惯等。
例如:
公共枚举MessengerToken
{
BrushChanged,
WidthChanged,
HeightChanged
}
然后您发送/接收会是这样的:
//发送视图模型
MessengerInstance.Send(Brushes.Red,MessengerToken.BrushChanged);
//接收视图模型
//把这个线在构造函数中
MessengerInstance.Register<刷>(这一点,道理,刷=> ChangeColor(刷));
公共无效ChangeColor(刷刷)
{
=刷刷;
}
URL以下改为devuxer的评论:
< A HREF =http://blog.galasoft.ch/posts/2009/09/mvvm-light-toolkit-messenger-v2/相对=nofollow> http://blog.galasoft.ch/posts/2009 / 09 / MVVM光的工具包信使-V2 /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 models trying to get this. The documentation on MVVM Light's not that great at this point so I'm completely unsure where to go.
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?
Edit: Here's my new implementation. As of now, it looks as if MessengerInstance doesn't call for a token. I'm terribly confused.
In the first 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.
解决方案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?
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.
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/
这篇关于使用MVVM光的使者传递到视图模型之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!