处理MVVM Light中的消息循环

时间:2014-06-27 17:49:17

标签: c# mvvm-light

我的应用中有多个对象,它们都发送和接收相同的消息。例如:当用户拖放网格上的列,用户使用网格设置对话框以及用户加载不同报表时,可以更改网格的列顺序。因此,Grid可以发送一个" Column Order Changed"消息,以及接收消息。

有没有办法阻止Grid收回自己的消息?因此,当用户拖动一列时,网格会发送一个"列顺序更改"向所有感兴趣的听众发送消息,但我不希望Grid接到对其订阅的回复。我可以使用谓词并在消息中嵌入一些发件人信息,但我想知道Mvvm Light是否可以自己处理这种情况。

1 个答案:

答案 0 :(得分:1)

检查IMessenger Register方法的这个重载:

/// <summary>
/// Registers a recipient for a type of message TMessage.
///             The action parameter will be executed when a corresponding
///             message is sent. See the receiveDerivedMessagesToo parameter
///             for details on how messages deriving from TMessage (or, if TMessage is an interface,
///             messages implementing TMessage) can be received too.
/// 
/// <para>
/// Registering a recipient does not create a hard reference to it,
///             so if this recipient is deleted, no memory leak is caused.
/// </para>
/// 
/// </summary>
/// <typeparam name="TMessage">The type of message that the recipient registers
///             for.</typeparam><param name="recipient">The recipient that will receive
///             the messages.</param><param name="token">A token for a messaging
///             channel. If a recipient registers using a token, and a sender sends
///             a message using the same token, then this message will be delivered to
///             the recipient. Other recipients who did not use a token when
///             registering (or who used a different token) will not get the message.
///             Similarly, messages sent without any token, or with a different
///             token, will not be delivered to that recipient.</param><param name="receiveDerivedMessagesToo">If true, message types deriving from
///             TMessage will also be transmitted to the recipient. For example, if a SendOrderMessage
///             and an ExecuteOrderMessage derive from OrderMessage, registering for OrderMessage
///             and setting receiveDerivedMessagesToo to true will send SendOrderMessage
///             and ExecuteOrderMessage to the recipient that registered.
/// 
/// <para>
/// Also, if TMessage is an interface, message types implementing TMessage will also be
///             transmitted to the recipient. For example, if a SendOrderMessage
///             and an ExecuteOrderMessage implement IOrderMessage, registering for IOrderMessage
///             and setting receiveDerivedMessagesToo to true will send SendOrderMessage
///             and ExecuteOrderMessage to the recipient that registered.
/// </para>
/// </param><param name="action">The action that will be executed when a message
///             of type TMessage is sent.</param>
void Register<TMessage>(object recipient, object token, bool receiveDerivedMessagesToo, Action<TMessage> action);

注意:

  

消息传递通道的令牌。如果收件人使用令牌注册,并且发件人使用相同的令牌发送邮件,则此邮件将被传递给收件人。注册时未使用令牌的其他收件人(或使用其他令牌的用户)将无法收到该邮件。同样,没有任何令牌或使用不同令牌发送的邮件也不会传递给该收件人。

您可以做的是让该消息的其他寄存器向令牌注册,然后当网格发送带有该令牌的消息时。网格的注册不应包含令牌。