Simple MVVM Toolkit 之 Messaging(A)

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)

作者:backslash112 (美国CS研究生在读/机器人工程师)
出处:http://sirkevin.cnblogs.com
GitHub:https://github.com/backslash112
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/sirkevin/p/2721293.html