2

在我的 WPF 应用程序中,我有许多字符串消息的订阅者和许多字符串消息发送。我知道我可以使用令牌来区分消息以选择特定的收件人。请直接指向执行此操作的示例。

4

1 回答 1

3

好吧,如果您有两个类ClassA,并且ClassB都注册了字符串类型的消息,并且类似于以下内容

Messenger.Default.Register<string>(this, OnStringMessageReceived);

那么您只能ClassA通过在发送模板调用中声明类类型来发送消息,例如

Messenger.Default.Send<string, ClassA>("Message to Only ClassA");

MVVM Light 作者的博客有一些您可能想阅读的相关文档

http://blog.galasoft.ch/archive/2009/09/27/mvvm-light-toolkit-messenger-v2-beta.aspx

从上面的链接中提取(我们用于您的要求的功能):

/// <summary>
/// Sends a message to registered recipients. The message will
/// reach only recipients that registered for this message type
/// using one of the Register methods, and that are
/// of the targetType.
/// </summary>
/// <typeparam name="TMessage">The type of message that will be sent.</typeparam>
/// <typeparam name="TTarget">The type of recipients that will receive
/// the message. The message won't be sent to recipients of another type.</typeparam>
/// <param name="message">The message to send to registered recipients.</param>
void Send<TMessage, TTarget>(TMessage message);
于 2013-03-24T20:52:07.110 回答