问题描述
我正在使用MVVM Light Toolkit的3.0.3.19版本.
I am using version 3.0.3.19 of the MVVM Light Toolkit.
来自 http://blog.galasoft.ch/archive/2010/03/16/whatrsquos-new-in-mvvm-light-v3.aspx :
-
要发送带有令牌的消息,请使用重载Send(TMessage消息,对象令牌)方法.
To send a message with token, use the method overload Send(TMessage message, object token).
要接收带有令牌的消息,请使用方法Register(对象收件人,对象令牌,Action操作)或Register(对象收件人, 对象令牌,bool receiveDerivedMessagesToo,操作操作)
To receive a message with token, use the methods Register(object recipient, object token, Action action) or Register(object recipient, object token, bool receiveDerivedMessagesToo, Action action)
令牌可以是简单值(int,字符串等)或实例 一堂课该邮件不会传递给已注册的收件人 带有其他令牌或完全没有令牌.
The token can be a simple value (int, string, etc…) or an instance of a class. The message is not delivered to recipients who registered with a different token, or with no token at all.
根据上述文档,我已经在ViewModel A中尝试了以下方法:
According to the documentation above, I have tried the following in ViewModel A:
Messenger.Default.Send(new NotificationMessage("message"), "token");
在ViewModel B中附带以下内容:
Along with the following in ViewModel B:
Messenger.Default.Register<NotificationMessage>(this, "token", (msg) => Console.WriteLine(msg.Notification));
但是,永远不会执行该回调.我在做什么错了?
However, the callback is never executed. What am I doing wrong?
推荐答案
我的ViewModelLocator在ViewModel B之前初始化ViewModelA.换句话说,该消息已由ViewModel A正确发送,但是ViewModel B尚未真正发送收到.
My ViewModelLocator was initializing ViewModel A before ViewModel B. In other words, the message was being sent properly by ViewModel A, but ViewModel B was not yet around to actually receive it.
我在ViewModelLocator中更改了初始化顺序,问题得以解决.此外,请验证Messenger是否可以使用String以外的其他类型的令牌.
I changed the order of initialization in the ViewModelLocator and the problem was solved. Also, verified that the Messenger worked with tokens of other types besides String.
这篇关于在Messenger类中使用令牌的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!