处理MVVM Light中的消息循环

本文关键字:消息 循环 MVVM Light 处理 | 更新日期: 2023-09-27 18:10:31

我有多个对象在我的应用程序都是发送和接收相同的消息。例如:当用户在网格上拖放列时,当用户使用网格设置对话框时,当用户加载不同的报告时,可以更改网格的列顺序。因此,网格既可以发送"Column Order Changed"消息,也可以接收该消息。

是否有一种方法可以防止网格获得自己的消息返回?因此,当用户拖动一列时,Grid会向所有感兴趣的侦听器发送"column Order Changed"消息,但我不希望Grid在订阅时收到回调。我可以使用谓词并在消息中嵌入一些发送方信息,但我想知道Mvvm Light是否可以自己处理此场景。

处理MVVM Light中的消息循环

查看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);

注意:

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

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