WPF MVVMToolkit框架学习-ObservableRecipient

ObservableRecipient 就是用来在VM之间相互传值的

比ObservableObject多了一个属性 IsActive(用来激活VM,使它能够接受到消息) 和一个Messenger 用来注册和发送消息

有两种机制可以使用 

第一种继承 ObservableRecipient  

后台代码

 1 using Microsoft.Toolkit.Mvvm.ComponentModel;
 2 using Microsoft.Toolkit.Mvvm.Input;
 3 using Microsoft.Toolkit.Mvvm.Messaging;
 4 using System;
 5 using System.Collections.Generic;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Input;
10 
11 namespace MVVMToolkit框架学习
12 {
13     public class SendVM : ObservableObject
14     {
15         private string _showMsg;
16 
17         public string ShowMsg
18         {
19             get => _showMsg;
20             set => SetProperty(ref _showMsg, value);
21         }
22 
23         public ICommand SendCmd { get; set; }
24 
25         public SendVM()
26         {
27             SendCmd = new RelayCommand(() =>
28             {
29                 //如果没有继承至ObservableRecipient,使用WeakReferenceMessenger发送和注册消息
30                 WeakReferenceMessenger.Default.Send(this);
31             });
32         }
33     }
34 }
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MVVMToolkit框架学习
{
    public class RecipientA1VM : ObservableRecipient
    {
        private string _displayMsg = "我是接收者";

        public string DisplayMsg
        {
            get => _displayMsg;
            set => SetProperty(ref _displayMsg, value);
        }

        public RecipientA1VM()
        {
            //VM需要是激活的才能接受到消息
            IsActive = true;
        }

        protected override void OnActivated()
        {
            //注册消息,有多个重载方法,按需使用 如果使用多个相同类型注册,可以使用Token来进行区分
            Messenger.Register<RecipientA1VM, SendVM>(this, (r, s) => r.Receive(s));
        }

        private void Receive(SendVM msg)
        {
            //对收到的消息处理
            DisplayMsg = "接收到了" + msg.GetType().Name + "的消息:" + msg.ShowMsg;
        }
    }
}

界面代码

 1 <Window
 2     x:Class="MVVMToolkit框架学习.SendView"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 6     xmlns:local="clr-namespace:MVVMToolkit框架学习"
 7     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 8     Title="SendView"
 9     Width="800"
10     Height="450"
11     d:DataContext="{d:DesignInstance Type={x:Type local:SendVM}}"
12 
13     mc:Ignorable="d">
14     <Window.DataContext>
15         <local:SendVM />
16     </Window.DataContext>
17     <Grid>
18         <StackPanel
19             MinWidth="200"
20             HorizontalAlignment="Center"
21             VerticalAlignment="Center"
22             Orientation="Vertical">
23             <Button Click="OpenRecipientView" Content="打开接受窗口" />
24 
25             <StackPanel Orientation="Horizontal">
26                 <TextBlock Text="要发送的消息:" />
27                 <TextBox MinWidth="150" Text="{Binding ShowMsg}" />
28             </StackPanel>
29             <Button Command="{Binding SendCmd}" Content="发送消息" />
30         </StackPanel>
31     </Grid>
32 </Window>
 1 <Window
 2     x:Class="MVVMToolkit框架学习.RecipientView"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 6     xmlns:local="clr-namespace:MVVMToolkit框架学习"
 7     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 8     Title="RecipientView"
 9     Width="800"
10     Height="450"
11     d:DataContext="{d:DesignInstance Type={x:Type local:RecipientA1VM}}"
12     mc:Ignorable="d">
13     <Window.DataContext>
14         <local:RecipientA1VM />
15     </Window.DataContext>
16     <Grid>
17         <TextBlock
18             MinWidth="75"
19             HorizontalAlignment="Center"
20             VerticalAlignment="Center"
21             FontSize="30"
22             Text="{Binding DisplayMsg}" />
23     </Grid>
24 </Window>

另外一种是继承IRecipient<T>接口,同样也能实现同样的目的

 接受VM的代码

 1 using Microsoft.Toolkit.Mvvm.ComponentModel;
 2 using Microsoft.Toolkit.Mvvm.Messaging;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace MVVMToolkit框架学习
10 {
11     public class RecipientA2VM : ObservableObject, IRecipient<string>
12     {
13         private string _displayMsg = "我是接收者";
14 
15         public string DisplayMsg
16         {
17             get => _displayMsg;
18             set => SetProperty(ref _displayMsg, value);
19         }
20 
21         public RecipientA2VM()
22         {
23             WeakReferenceMessenger.Default.Register(this);
24         }
25 
26         public void Receive(string message)
27         {
28             DisplayMsg = message;
29         }
30     }
31 }

 

 

posted @ 2021-12-08 20:03  只吃肉不喝酒  阅读(2598)  评论(3编辑  收藏  举报