1. Simple MVVM Toolkit 簡單介紹:
Simple MVVM Toolkit是一個開源的MVVM框架,提供VS項目和項的模板,依賴注入,支持深拷貝以及模型和視圖模型之間的屬性關聯。想了解具體介紹的請點擊這里。
2. Simple MVVM Toolkit 的 Messaging 簡單介紹:
Simple MVVM Toolkit 的 Messaging 能夠達到不同View-Model之間松耦合的通訊。
3. 通過一個MVVM的示例,體會 Simple MVVM Toolkit 的 Messaging 機制如何實現不同 View-Model 之間的通訊:
需求說明:
a)客戶列表窗體和客戶列表對應的客戶的明細窗體同時存在;
b)點擊客戶列表的某個客戶,為其年齡增加1歲,此變更會通知到列表對應客戶的明細窗體。
步驟:
3.1 新建WPF項目,項目命名為MessagingSimple。

注意:新建完項目之后請添加對SimpleMvvmToolkit-WPF.dll的引用。
3.2 右鍵項目,添加文件夾 Models、ViewModels 和 Views。
4. 首先定義客戶實體,右鍵Models文件夾,添加類 Customer:
1 public class Customer : ModelBase<Customer> 2 { 3 private string customerName; 4 5 public string CustomerName 6 { 7 get { return customerName; } 8 set 9 { 10 customerName = value; 11 NotifyPropertyChanged(m => m.CustomerName); 12 } 13 } 14 15 private int age; 16 17 public int Age 18 { 19 get { return age; } 20 set 21 { 22 age = value; 23 NotifyPropertyChanged(m => m.Age); 24 } 25 } 26 27 // Manufacture a list of customers 28 private static ObservableCollection<Customer> customersList; 29 30 public static ObservableCollection<Customer> CustomersList 31 { 32 get 33 { 34 if (customersList == null) 35 { 36 customersList = new ObservableCollection<Customer> 37 { 38 new Customer { CustomerName = "Bill Gates", Age = 10 }, 39 new Customer { CustomerName = "Steve Jobs", Age = 20 }, 40 new Customer { CustomerName = "Mark Zuckerberg", Age = 30 } 41 }; 42 } 43 return customersList; 44 } 45 } 46 }
5. 其次,定義顯示數據的視圖,右鍵Views文件夾,添加用戶控件CustomerList和CustomerDetail:
CustomerList.xaml:
1 <UserControl x:Class="MessagingSimple.Views.CustomerList" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 mc:Ignorable="d" 7 d:DesignHeight="250" 8 d:DesignWidth="250" 9 Width="300"> 10 <Grid> 11 <Grid.RowDefinitions> 12 <RowDefinition /> 13 <RowDefinition /> 14 </Grid.RowDefinitions> 15 <ListBox ItemsSource="{Binding Path=Customers}" 16 SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"> 17 <ListBox.ItemTemplate> 18 <DataTemplate> 19 <StackPanel Orientation="Horizontal"> 20 <TextBlock Text="{Binding Path=CustomerName}" 21 Padding="5,5,0,5" /> 22 <TextBlock Text=":" 23 Padding="5,5,0,5" /> 24 <TextBlock Text="{Binding Path=Age}" 25 Padding="5,5,0,5" 26 FontWeight="Bold" /> 27 <TextBlock Text="Age" 28 Padding="5,5,5,5" /> 29 </StackPanel> 30 </DataTemplate> 31 </ListBox.ItemTemplate> 32 </ListBox> 33 34 </Grid> 35 </UserControl>
CustomerList.xaml.cs:
1 /// <summary> 2 /// CustomerList.xaml 的交互邏輯 3 /// </summary> 4 public partial class CustomerList : UserControl 5 { 6 public CustomerList() 7 { 8 InitializeComponent(); 9 10 this.DataContext = new CustomerListViewModel(); 11 } 12 }
CustomerDetail.xaml:
1 <UserControl x:Class="MessagingSimple.Views.CustomerDetail" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:vm="clr-namespace:MessagingSimple.ViewModels" 7 mc:Ignorable="d" 8 d:DesignHeight="180" 9 d:DesignWidth="345"> 10 <UserControl.DataContext> 11 <vm:CustomerDetailViewModel /> 12 </UserControl.DataContext> 13 <Grid> 14 <TextBlock Height="23" 15 HorizontalAlignment="Left" 16 Margin="24,37,0,0" 17 Name="textBlock1" 18 Text="Customer Name :" 19 VerticalAlignment="Top" /> 20 <TextBlock Height="23" 21 HorizontalAlignment="Left" 22 Margin="24,82,0,0" 23 Name="textBlock2" 24 Text="Customer Age :" 25 VerticalAlignment="Top" /> 26 <TextBox Height="23" 27 HorizontalAlignment="Left" 28 Margin="137,34,0,0" 29 Name="textBox1" 30 Text="{Binding Path=Customer.CustomerName}" 31 VerticalAlignment="Top" 32 Width="172" /> 33 <TextBox Height="23" 34 Text="{Binding Path=Customer.Age}" 35 HorizontalAlignment="Left" 36 Margin="137,79,0,0" 37 Name="textBox2" 38 VerticalAlignment="Top" 39 Width="172" /> 40 </Grid> 41 </UserControl>
CustomerDetail.xaml.cs:
1 /// <summary> 2 /// CustomerDetail.xaml 的交互邏輯 3 /// </summary> 4 public partial class CustomerDetail : UserControl 5 { 6 private CustomerDetailViewModel viewModel; 7 8 public CustomerDetail() 9 { 10 InitializeComponent(); 11 12 viewModel = (CustomerDetailViewModel)DataContext; 13 } 14 15 private int customerIndex; 16 17 public int CustomerIndex 18 { 19 get { return customerIndex; } 20 set 21 { 22 customerIndex = value; 23 viewModel.Customer = Customer.CustomersList[value]; 24 } 25 } 26 }
6. 最后為視圖 CustomerList 和 CustomerDetail 創建對應的View-Model類,右鍵ViewModels文件夾,添加類 CustomerListViewModel 和 CustomerDetailViewModel:
CustomerListViewModel.cs:
1 public class CustomerListViewModel : ViewModelBase<CustomerListViewModel> 2 { 3 public CustomerListViewModel() 4 { 5 Customers = Customer.CustomersList; 6 } 7 8 private ObservableCollection<Customer> customers = 9 new ObservableCollection<Customer>(); 10 11 public ObservableCollection<Customer> Customers 12 { 13 get { return customers; } 14 set { customers = value; } 15 } 16 17 private Customer selectedCustomer; 18 19 public Customer SelectedCustomer 20 { 21 get { return selectedCustomer; } 22 set 23 { 24 selectedCustomer = value; 25 NotifyPropertyChanged(m => m.SelectedCustomer); 26 } 27 } 28 }
CustomerDetailViewModel.cs:
1 public class CustomerDetailViewModel : ViewModelDetailBase<CustomerDetailViewModel, Customer> 2 { 3 public Customer Customer 4 { 5 get { return base.Model; } 6 set 7 { 8 base.Model = value; 9 NotifyPropertyChanged(m => m.Customer); 10 } 11 } 12 }
7. 現在運行項目,效果如下,客戶列表和客戶明細之間是相互獨立的。

為了達到使用 Messaging 前后的比較效果,所以我打算分開寫。未完待續。。。
點擊這里下載源代碼。(MessagingSimple-MessagingBefore)
