MvvmLight之Messenger使用介绍

简介

GalaSoft.MvvmLight.Messaging:消息类,提供全局的消息通知,可以理解为全局的Event事件。

可解决的问题

  • 1、如何从业务逻辑中,打开子窗口,进行交互结果返回。
  • 2、如何处理各层级之间的相互通信。

本质上Messenger的功能是通过封装的全局委托来实现的。

框架提供的接口函数如下:
在这里插入图片描述

使用介绍

1.常规使用

注册:

        public MainWindow()
        {
            InitializeComponent();
            // 1、通过recipient进行区分,发送的时候需要指类型,不指定的话,所有此种类型的注册都可以接收消息
            Messenger.Default.Register<string>(this, (o) =>
            {
            });
            // 2、通过token进行区分
            Messenger.Default.Register<string>(this, "token", (o) =>
            {
            });

            // 3、receiveDerivedMessagesToo参数控制是否接收派生类的消息,true时接收派生类的消息;false时不接收派生类消息
            Messenger.Default.Register<IA>(this, false, (o) =>
            {
            });
        }

    interface IA
    {
        void Show();
    }

    public class B : IA
    {
        public void Show()
        {
            throw new NotImplementedException();
        }
    }

触发:

        public ICommand TestCommand
        {
            get
            {
                if (_testCommand == null)
                    _testCommand = new RelayCommand(() =>
                    {
                        // 1、常规发送信息
                        Messenger.Default.Send("123");
                        Messenger.Default.Send("123", "token");
                        
            			// 2、指定接口或其派生类接收消息,判断receiveDerivedMessagesToo参数
            			Messenger.Default.Send<IA>(new B());
            			Messenger.Default.Send<B>(new B());

            			// 3、指定相应类型接收这个消息
            			Messenger.Default.Send<string, MainWindow>("123");
            		});
                return _testCommand;
            }
        }
2.NotificationMessage

作用:将传递的消息封装为NotificationMessage对象

非泛型类:
在这里插入图片描述
泛型类:
在这里插入图片描述
注册:

        public MainWindow()
        {
            InitializeComponent();
            Messenger.Default.Register<NotificationMessage>(this, msg =>
            {
                //if (msg.Target is MainWindow)
                //{
                //}
                if (msg.Sender is ViewModelBase)
                {
                }
            });
            Messenger.Default.Register<NotificationMessage<int>>(this, msg =>
            {
                //if (msg.Target is MainWindow)
                //{
                //}
                if (msg.Sender is ViewModelBase)
                {
                }
            });
            Messenger.Default.Register<NotificationMessage<string>>(this, msg =>
            {
                //if (msg.Target is MainWindow)
                //{
                //}
                if (msg.Sender is ViewModelBase)
                {
                }
                if (msg.Content == "")
                {
                }
            })
        }

触发:

        public ICommand TestCommand
        {
            get
            {
                if (_testCommand == null)
                    _testCommand = new RelayCommand(() =>
                    {
            			// 触发NotificationMessage类型的消息
            			Messenger.Default.Send<NotificationMessage>(new NotificationMessage(new B(), "123"));
            			Messenger.Default.Send<NotificationMessage<int>>(new NotificationMessage<int>(this, 1, "123"));
            		});
                return _testCommand;
            }
        }
3.NotificationMessageAction

作用:将传递的消息封装为NotificationMessageAction 对象,支持Action回调

在这里插入图片描述
注册:

        public MainWindow()
        {
            InitializeComponent();                       									
            Messenger.Default.Register<NotificationMessageAction<bool>>(this, msg =>
            {
                var rresult = MessageBox.Show(msg.Notification, "消息对话框", MessageBoxButton.YesNo, MessageBoxImage.Question);
                //执行Send里面的回调函数
                msg.Execute(rresult == MessageBoxResult.Yes);
            });
        }

触发:

        public ICommand TestCommand
        {
            get
            {
                if (_testCommand == null)
                    _testCommand = new RelayCommand(() =>
                    {
                        NotificationMessageAction<bool> nma = new NotificationMessageAction<bool>("123", (r) =>
                        {
                        	//执行Registerzh
                        });
                        Messenger.Default.Send<NotificationMessageAction<bool>>(nma);
                    });
                return _testCommand;
            }
        }
4.PropertyChangedMessage

作用:属性值变化时就会触发注册的函数。
需要将属性通知RaisePropertyChanged中的broadcast参数设为true。
在这里插入图片描述

注册:

        public MainWindow()
        {
            InitializeComponent();                       									
            Messenger.Default.Register<PropertyChangedMessage<string>>(this, (o) =>
            {
                // 区别一下哪个属性进行了变化
                if (o.PropertyName == "MyProperty")
                {
                }
            });

触发:

        public ICommand TestCommand
        {
            get
            {
                if (_testCommand == null)
                    _testCommand = new RelayCommand(() =>
                    {
                        this.MyProperty = "123";
                        this.VVV = "123";
                    });
                return _testCommand;
            }
        }
        
		private string myVar;
		//需要将RaisePropertyChanged中的broadcast参数设为true;
        public string MyProperty
        {
            get { return myVar; }
            set
            {
                myVar = value;
                this.RaisePropertyChanged<string>("MyProperty", "1", "2", true);
            }
        }

        private string _fvv;
        public string VVV
        {
            get { return _fvv; }
            set
            {
                _fvv = value;
                this.RaisePropertyChanged<string>("VVV", "1", "2", true);
            }
        }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值