MVVM光使者不按预期 [英] MVVM Light Messenger Not Functioning As Expected
问题描述
前阵子我在这里问发现了一个问题:的使用MVVM光的使者传递到视图模型之间的值
A while back I asked a question found here: Use MVVM Light's Messenger to Pass Values Between View Model
我今天去测试答案,它似乎没有奏效。我的实现看起来如下:
I went to test the answer today and it doesn't appear to be working. My implementation looks as follows:
MessengerInstance.Send(SelectedDocument, Model.StaticEnums.Tokens.SettingstoMain);
和
MessengerInstance.Register<XDocument>(this, Model.StaticEnums.Tokens.SettingstoMain, settings => CopySettings(settings));
我的问题是,这个实现不工作。相反,对于MessengerInstance.Send和MessengerInstance.Register参数都似乎是在回答实施显着的不同。
My problem is, this implementation doesn't work. Instead, the arguments for MessengerInstance.Send and MessengerInstance.Register both appear to be strikingly different from the implementation in the answer.
我是什么错在这里做什么?在回答我刚才的问题实施正确的?
What am I doing wrong here? Is the implementation in the answer to my previous question correct?
推荐答案
我没有在过去的几个月里做了很多与MVVM光。但是,我发送和注册消息总是这样(见代码)。也许有新版本更好的方法。但我不认为
I didn't worked a lot with MVVM light in the past few months. But I send and registered messages always this way (see code). Maybe there are better ways in the new version. But I don't think.
GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<string>(this, (a) => { MessageBox.Show(a); });
GalaSoft.MvvmLight.Messaging.Messenger.Default.Send<string>("abc");
请确保您先注册的消息在发送前。
Make sure that you first REGISTER the message before you send it.
编辑:对于每一个消息类型我创建了一个自定义邮件类。所以它更容易在应用程序中使用该消息的代码找到。
For each message type I created a custom message class. So it's easier to find in the code where the message is used in the application.
这篇关于MVVM光使者不按预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!