/// <summary>
/// Registers a recipient for a type of message tmessage.
/// 为T类型的参数 注册一个接受者,
/// The action parameter will be executed when a corresponding (相应的)
/// (当接收到) 参数action 这个方法将被执行
/// 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.
/// token 是一个 栏目的标记
/// If a recipient registers
/// using a token, and a sender sends a message using the same token, then this
/// 发送和接收方使用同一个标记 token接收 才能 接收到
/// 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>
public virtual void Register<TMessage>(//TMessage接收的 类型
object recipient,//接收的对象一般是viewmodel
object token,//标记 ,细分类型下面的 分类。
bool receiveDerivedMessagesToo,// 是否也接收 (派生类的) 消息
Action<TMessage> action) {//当有消息来临的时候的 回调函数
var messageType = typeof(TMessage);
Dictionary<Type, List<WeakActionAndToken>> recipients;
//============================ 是否也接收 (派生类的) 消息
if (receiveDerivedMessagesToo) {
if (_recipientsOfSubclassesAction == null) {
_recipientsOfSubclassesAction = new Dictionary<Type, List<WeakActionAndToken>>();
}
recipients = _recipientsOfSubclassesAction;
}
// 默认是 false -即 不接收 (派生类的) 消息
else {
if (_recipientsStrictAction == null) {
_recipientsStrictAction = new Dictionary<Type, List<WeakActionAndToken>>();
}
recipients = _recipientsStrictAction;
}
//============================
List<WeakActionAndToken> list;
if (!recipients.ContainsKey(messageType)) { //如果不存在 List 就添加
list = new List<WeakActionAndToken>();
recipients.Add(messageType, list);
} else {
list = recipients[messageType];//存在就取出
}
// 新建一个 weakAction 弱引用 方法
var weakAction = new WeakAction<TMessage>(recipient, action);// new WeakAction<TMessage>(viewmodel, function);
var item = new WeakActionAndToken {
Action = weakAction,
Token = token
};
list.Add(item);
//清除已经被垃圾回收的对象
Cleanup();
}
- 海报