1、概述express
Messenger中文解釋爲信使的意思,顧名思義,在MvvmLight中,它的主要做用是用於View和ViewModel、ViewModel和ViewModel之間的通訊。設計模式
考慮如下場景:this
如今有一個主窗體,主窗體上有一個按鈕,點擊按鈕的時候,彈出一個子窗體,用於實時的往主窗體上添加新的項。spa
這時候Messenger就發揮其強大的威力了。設計
Messenger類的主要交互模式就是信息的發送和接收(相似消息的發佈訂閱)。code
MVVM Light Messenger 旨在經過簡單的設計模式來精簡此場景:任何對象均可以是接收端;任何對象均可以是發送端;任何對象均可以是消息。component
Messenger的工做流程以下:orm
2、View和ViewModel之間通訊xml
在View中註冊消息 Messenger.Default.Register<string>(this, "ShowSubWindowToken", ShowSubWindow),至關於訂閱服務。對象
消息標誌token:ShowSubWindowToken,用於標識只閱讀某個或者某些Sender發送的消息,並執行相應的處理,因此Sender那邊的token要保持一致
消息處理Action:ShowSubWindow,參數類型爲string,用來執行接收到消息後的後續工做。
在ViewModel中發送消息 Messenger.Default.Send("Show subwindow","ShowSubWindowToken"),至關於發佈事件。
傳遞的消息參數爲Show subwindow,消息token爲ShowSubWindowToken,須要與接收者註冊的消息的Token一致。
代碼片斷以下:
using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Messaging; using MvvmLightClosableTabControl.Models; using System.Collections.ObjectModel; using System.Windows; namespace MvvmLightClosableTabControl.ViewModel { public class Page3ViewModel:ViewModelBase { public Page3ViewModel() { Messenger.Default.Register<string>(this, "AddItemToken", AddItem); } private ObservableCollection<ListBoxItemModel> listBoxData = new ObservableCollection<ListBoxItemModel>() { new ListBoxItemModel(){ Img="/MvvmLightClosableTabControl;component/Img/1.png",Info="Honey Peach " }, new ListBoxItemModel(){ Img="/MvvmLightClosableTabControl;component/Img/2.png",Info="Tomato" }, new ListBoxItemModel(){ Img="/MvvmLightClosableTabControl;component/Img/3.png",Info="Banana" }, new ListBoxItemModel(){ Img="/MvvmLightClosableTabControl;component/Img/4.png",Info="Chilli " }, new ListBoxItemModel(){ Img="/MvvmLightClosableTabControl;component/Img/5.png",Info="Apple" }, }; /// <summary> /// LisBox數據模板 /// </summary> public ObservableCollection<ListBoxItemModel> ListBoxData { get { return listBoxData; } set { listBoxData = value; RaisePropertyChanged(() => ListBoxData); } } private int selectedIndex = -1; public int SelectedIndex { get { return selectedIndex; } set { selectedIndex = value; RaisePropertyChanged(); string selValue = $"ImgPath: {listBoxData[selectedIndex].Img}\r\nInfo: {listBoxData[selectedIndex].Info}"; ViewModelLocator.DialogService.ShowInfoDialog($"您當前選擇的是:\r\n{selValue}"); } } #region Command private RelayCommand addItemCommand; public RelayCommand AddItemCommand { get { if (addItemCommand == null) { addItemCommand = new RelayCommand(AddOneItem); } return addItemCommand; } set { addItemCommand = value; } } private void AddOneItem() { int a = 3; Messenger.Default.Send("Show subwindow","ShowSubWindowToken"); } #endregion private void AddItem(string msg) { ListBoxData.Add(new ListBoxItemModel() { Img = "/MvvmLightClosableTabControl;component/Img/1.png", Info = msg }); MessageBox.Show(msg + " added completed!"); } } }
using GalaSoft.MvvmLight.Messaging; using System.Windows; using System.Windows.Controls; namespace MvvmLightClosableTabControl.Pages { /// <summary> /// Interaction logic for Page3.xaml /// </summary> public partial class Page3 : Page { public Page3() { InitializeComponent(); Messenger.Default.Register<string>(this, "ShowSubWindowToken", ShowSubWindow); } private void ShowSubWindow(string msg) { Page3SubWindow myWindow = new Page3SubWindow() { WindowStartupLocation = WindowStartupLocation.CenterOwner, Owner = Application.Current.MainWindow }; myWindow.ShowDialog(); } } }
<Page x:Class="MvvmLightClosableTabControl.Pages.Page3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:MvvmLightClosableTabControl.Pages" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Title="Page3"> <Page.DataContext> <Binding Path="Page3" Source="{StaticResource Locator}"/> </Page.DataContext> <Grid Background="#FFBBB415"> <Grid.RowDefinitions> <RowDefinition Height="70"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Label Content="This is page3." VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="40"></Label> <StackPanel Grid.Row="1" > <GroupBox Header="Messenger顯示子窗口添加用戶" FontSize="16" Foreground="DarkGreen"> <StackPanel Orientation="Horizontal"> <StackPanel> <Button Content="添加條目" Command="{Binding AddItemCommand}"></Button> <ListBox ItemsSource="{Binding ListBoxData}" DisplayMemberPath="Info" SelectedIndex="{Binding SelectedIndex}"> </ListBox> </StackPanel> </StackPanel> </GroupBox> </StackPanel> </Grid> </Page>
3、ViewModel和ViewModel之間通訊
模擬如下場景:
如今有一個主窗體,主窗體上有一個按鈕,點擊按鈕的時候,彈出一個子窗體,用於實時的往主窗體上添加新的項。
代碼片斷以下:
using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Messaging; using MvvmLightClosableTabControl.Models; using System.Collections.ObjectModel; using System.Windows; namespace MvvmLightClosableTabControl.ViewModel { public class Page3ViewModel:ViewModelBase { public Page3ViewModel() { Messenger.Default.Register<string>(this, "AddItemToken", AddItem); } private ObservableCollection<ListBoxItemModel> listBoxData = new ObservableCollection<ListBoxItemModel>() { new ListBoxItemModel(){ Img="/MvvmLightClosableTabControl;component/Img/1.png",Info="Honey Peach " }, new ListBoxItemModel(){ Img="/MvvmLightClosableTabControl;component/Img/2.png",Info="Tomato" }, new ListBoxItemModel(){ Img="/MvvmLightClosableTabControl;component/Img/3.png",Info="Banana" }, new ListBoxItemModel(){ Img="/MvvmLightClosableTabControl;component/Img/4.png",Info="Chilli " }, new ListBoxItemModel(){ Img="/MvvmLightClosableTabControl;component/Img/5.png",Info="Apple" }, }; /// <summary> /// LisBox數據模板 /// </summary> public ObservableCollection<ListBoxItemModel> ListBoxData { get { return listBoxData; } set { listBoxData = value; RaisePropertyChanged(() => ListBoxData); } } private int selectedIndex = -1; public int SelectedIndex { get { return selectedIndex; } set { selectedIndex = value; RaisePropertyChanged(); string selValue = $"ImgPath: {listBoxData[selectedIndex].Img}\r\nInfo: {listBoxData[selectedIndex].Info}"; ViewModelLocator.DialogService.ShowInfoDialog($"您當前選擇的是:\r\n{selValue}"); } } #region Command private RelayCommand addItemCommand; public RelayCommand AddItemCommand { get { if (addItemCommand == null) { addItemCommand = new RelayCommand(AddOneItem); } return addItemCommand; } set { addItemCommand = value; } } private void AddOneItem() { int a = 3; Messenger.Default.Send("Show subwindow","ShowSubWindowToken"); } #endregion private void AddItem(string msg) { ListBoxData.Add(new ListBoxItemModel() { Img = "/MvvmLightClosableTabControl;component/Img/1.png", Info = msg }); MessageBox.Show(msg + " added completed!"); } } }
using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Messaging; namespace MvvmLightClosableTabControl.ViewModel { public class Page3SubWindowViewModel { private string itemName = "Orange"; public string ItemName { get { return itemName; } set { itemName = value; } } #region Command private RelayCommand addItemCommand; public RelayCommand AddItemCommand { get { if (addItemCommand == null) { addItemCommand = new RelayCommand(AddOneItem); } return addItemCommand; } set { addItemCommand = value; } } private void AddOneItem() { Messenger.Default.Send(ItemName, "AddItemToken"); } #endregion } }