继续上一篇《Simple MVVM Toolkit 之 Messaging(A)》。
接下来使用Messaging实现客户列表View-Model和客户明细View-Model之间的通讯。
1. 为 Messaging 准备辅助类和窗体:
1.1 右键Models文件夹,添加类 MessageTokens,此类目的是提供一个消息标识符。
1.2 右键Models文件夹,添加类 IncreaseInfo,此类是一个增量信息的实体。
1 public class IncreaseInfo 2 { 3 public IncreaseInfo() 4 { } 5 6 public IncreaseInfo(string customerName, int age) 7 { 8 // TODO: Complete member initialization 9 this.CustomerName = customerName; 10 this.Age = age; 11 } 12 13 public string CustomerName { get; set; } 14 15 public int Age { get; set; } 16 }
1.3 右键Models文件夹,添加类 ApprovalInfo,此类表示的是弹出提示窗口的消息实体。
1 public class ApprovalInfo 2 { 3 public ApprovalInfo() 4 { } 5 6 public ApprovalInfo(string customerName, int age, bool result) 7 { 8 this.CustomerName = customerName; 9 this.Age = age; 10 this.Result = result; 11 } 12 13 public string CustomerName { get; set; } 14 15 public int Age { get; set; } 16 17 public bool Result { get; set; } 18 }
1.4 右键Views文件夹,添加窗体 ApprovalIncrease,此窗体是一个提示框。
1 <Window x:Class="MessagingSimple.Views.ApprovalIncrease" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="ApproveIncrease" Height="174" Width="352"> 5 <Grid> 6 <Grid.RowDefinitions> 7 <RowDefinition Height="81*" /> 8 <RowDefinition Height="54*" /> 9 </Grid.RowDefinitions> 10 11 <StackPanel Orientation="Horizontal"> 12 <StackPanel.Resources> 13 <Style TargetType="TextBlock"> 14 <Setter Property="VerticalAlignment" Value="Center" /> 15 </Style> 16 </StackPanel.Resources> 17 <TextBlock Text="Do you approve the increase of " /> 18 <TextBlock Name="quantityText" /> 19 <TextBlock Text=" for " /> 20 <TextBlock Name="customerText" /> 21 <TextBlock Text="?" /> 22 </StackPanel> 23 <Button Content="No" 24 Height="23" 25 HorizontalAlignment="Left" 26 Margin="202,12,0,0" 27 Name="btnNo" 28 VerticalAlignment="Top" 29 Width="75" 30 Click="btnNo_Click" 31 Grid.Row="1" /> 32 <Button Content="Yes" 33 Height="23" 34 HorizontalAlignment="Left" 35 Margin="73,12,0,0" 36 Name="btnYes" 37 VerticalAlignment="Top" 38 Width="75" 39 Click="btnYes_Click" 40 Grid.Row="1" /> 41 </Grid> 42 </Window>
1 /// <summary> 2 /// ApproveIncrease.xaml 的交互逻辑 3 /// </summary> 4 public partial class ApprovalIncrease : Window 5 { 6 public ApprovalIncrease() 7 { 8 InitializeComponent(); 9 } 10 public ApprovalIncrease(IncreaseInfo increaseInfo) 11 : this() 12 { 13 this.quantityText.Text = increaseInfo.Age.ToString(); 14 this.customerText.Text = increaseInfo.CustomerName; 15 } 16 17 private void btnYes_Click(object sender, RoutedEventArgs e) 18 { 19 this.DialogResult = true; 20 } 21 22 private void btnNo_Click(object sender, RoutedEventArgs e) 23 { 24 this.DialogResult = false; 25 } 26 27 28 29 30 }
2 在Views-CustomerList.xaml中增加按钮和消息栏。
1 <Button Content="Increase Age" 2 Height="23" 3 Grid.Row="1" 4 HorizontalAlignment="Center" 5 Command="{Binding Path=IncreaseAgeCommand}" 6 VerticalAlignment="Center" 7 Width="120" 8 Margin="0,21,0,81" /> 9 <TextBlock Grid.Row="1" 10 Height="42" 11 HorizontalAlignment="Center" 12 Margin="0,61,0,0" 13 Name="textBlock1" 14 Text="{Binding Path=MessageText}" 15 VerticalAlignment="Top" 16 Width="245" />
3 实现 Messaging 通讯五步骤:
3.1 第一步:定义通讯标识。(MessageTokens.cs)
1 //Step 1 定义增长年龄标识。 2 public const string IncreaseAge = "AgeIncreaseToken";
3.2 第二步:当年龄增加之后获取通知的Register。(CustomerDetailViewModel.cs)
1 //Step 2 当年龄增加之后获取通知的Register。 2 RegisterToReceiveMessages<IncreaseInfo, ApprovalInfo>( 3 MessageTokens.IncreaseAge, OnOrdersIncrease);
3.3 第三步:处理客户列表年龄增加的通知消息。(CustomerDetailViewModel.cs)
1 //Step 3 处理客户列表年龄增加的通知消息。 2 private void OnOrdersIncrease(object sender, NotificationEventArgs 3 <IncreaseInfo, ApprovalInfo> e) 4 { 5 if (e.Data.CustomerName != Customer.CustomerName) 6 return; 7 ApprovalIncrease approvalIncrease = new ApprovalIncrease(e.Data); 8 9 approvalIncrease.Closed += (s, ea) => 10 { 11 ApprovalInfo approvalInfo = new ApprovalInfo( 12 Customer.CustomerName, 13 e.Data.Age, 14 (bool)approvalIncrease.DialogResult); 15 16 e.Completed(approvalInfo); 17 }; 18 19 approvalIncrease.ShowDialog(); 20 }
3.4 第四步:发送消息。(CustomerListViewModel.cs)
1 //Step 4 2 SendMessage(MessageTokens.IncreaseAge, 3 new NotificationEventArgs<IncreaseInfo, ApprovalInfo>( 4 null, increaseInfo, OnIncreaseResponse));
3.5 第五步:处理通知回调。
1 //Step 5 处理通知回调。 2 void OnIncreaseResponse(ApprovalInfo approve) 3 { 4 string resultText = approve.Result ? "approved" : "rejected"; 5 MessageText = string.Format( 6 "Age quantity of {0} {1} for {2}", 7 approve.Age, resultText, approve.CustomerName); 8 9 if (!approve.Result) 10 { 11 SelectedCustomer.Age -= 1; 12 } 13 }
流程如下:
运行效果如下。
点击这里下载源代码。(MessagingSimple-MessagingEnd)