欢迎光临
我们一直在努力

MVVMLight源码分析之消息机制和ViewModelBase

 /// <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();
        }

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《MVVMLight源码分析之消息机制和ViewModelBase》
文章链接:https://www.456zj.com/20781.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址