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 }