• MVVM Light Messenger的使用


    MVVM Light Messenger的使用

    参见文章:https://msdn.microsoft.com/zh-cn/magazine/dn745866.aspx

    MVVM Light Messenger 旨在通过简单的前提来精简此场景:任何对象都可以是接收端;任何对象都可以是发送端;任何对象都可以是消息。

    使用如下:

    1.使用默认的 Messenger 并检查发送端

    public class FirstViewModel
     { 
        public FirstViewModel()
        { 
            Messenger.Default.Register<NotificationMessage>( this, message => 
            {
                if (message.Sender is MainViewModel) 
                { // 此消息是要发送给我。 
                } 
            }); 
        }
    } 
        
    public class SecondViewModel 
    { 
        public SecondViewModel() 
        { 
            Messenger.Default.Register<NotificationMessage>( this, message => 
            { 
                if (message.Sender is SettingsViewModel) 
                { // 此消息是要发送给我 
                } 
            }); 
        }
    }

    2.专用消息

    public class MainViewModel 
    { 
        private Messenger _privateMessenger;
        public MainViewModel() 
        { 
            _privateMessenger = new Messenger(); 
            SimpleIoc.Default.Register(() => _privateMessenger, "PrivateMessenger"); 
        }
        public void Update() 
        { 
            _privateMessenger.Send(new NotificationMessage("DoSomething"));
        } 
    } 
    public class FirstViewModel 
    { 
        public FirstViewModel() 
        { 
            var messenger = SimpleIoc.Default.GetInstance<Messenger>("PrivateMessenger"); 
            messenger.Register<NotificationMessage>( this, message => 
         { // 此消息是要发送给我。
         });
    } }

    3. 使用令牌的不同信道

    public class MainViewModel 
    { 
        public static readonly Guid Token = Guid.NewGuid(); 
        public void Update() 
        { 
            Messenger.Default.Send(new NotificationMessage("DoSomething"), Token); 
        } 
    } 
    
    public class FirstViewModel 
    { 
        public FirstViewModel() 
        { 
            Messenger.Default.Register<NotificationMessage>( this, MainViewModel.Token, message => 
            { // 此消息是要发送给我。
             }); 
        } 
    }

    4. 使用消息类型来定义上下文

    public class Sender 
    { 
        public void SendBoolean() 
        { 
            Messenger.Default.Send(true); 
        }
        public void SendNotification() 
        { 
            Messenger.Default.Send( new NotificationMessage<bool>(true, Notifications.PlayPause)); 
        } 
    }
    
    public class Receiver
    { 
        public Receiver() 
        { 
            Messenger.Default.Register<bool>( this, b => 
            { // 不确定如何处理此布尔。
            }); 
            Messenger.Default.Register<NotificationMessage<bool>>( this, message => 
            { 
                if (message.Notification == Notifications.PlayPause) 
                { // 对消息执行某操作。
                    Content.Debug.WriteLine(message.Notification + ":"+ message.Content); 
                } 
            }); 
        } 
    }

    5. 发送 PropertyChangedMessage

    public class BankViewModel :ViewModelBase 
    { 
        public const string BalancePropertyName = "Balance"; 
        private double _balance; 
        public double Balance 
        { 
            get 
            { 
                return _balance; 
            } 
            set 
            { 
                if (Math.Abs(_balance - value) < 0.001) 
                { return; } 
                var oldValue = _balance; 
                _balance = value; 
                RaisePropertyChanged(BalancePropertyName, oldValue, value, true);
            } 
        } 
    } 
    public class Receiver 
    { 
        public Receiver() 
        { 
            Messenger.Default.Register<PropertyChangedMessage<double>>( this, message => 
            { 
                if (message.PropertyName == BankViewModel.BalancePropertyName) 
                { 
                    Debug.WriteLine( message.OldValue + " --> " + message.NewValue); 
                } 
            }); 
        } 
    }

    未取消注册时的内存泄漏风险

    可见性 WPF Silverlight Windows Phone 8
    静态 无风险 无风险 无风险
    公共 无风险 无风险 无风险
    内部 无风险 风险 风险
    专用 无风险 风险 风险
    匿名 Lambda 无风险 风险 风险
  • 相关阅读:
    += 和 =+
    Ubuntu分区方案(菜鸟方案、常用方案和进阶方案)
    Apache ab测试工具使用方法(无参、get传参、post传参)(转)
    硬盘安装ubuntu遇到的问题
    Promise
    A Kill Cord for your Laptop
    python 2week
    PDCA循环原理
    python自学基础1week
    14链表中倒数第k个结点
  • 原文地址:https://www.cnblogs.com/devin_zhou/p/8729041.html
Copyright © 2020-2023  润新知