Windows Phone 7 MVVM模式通讯方式之实现Command

简介:
MVVM模式的View与ViewModel的三大通讯方式:Binding Data(实现数据的传递)、Command(实现操作的调用)和Attached Behavior(实现控件加载过程中的操作)。
下面通过一个实例实现MVVM模式的Command通讯

 

 

(1)MainPage.xaml文件的代码,实现View层

 

 
  1.  
  2. <phone:PhoneApplicationPage 
  3.  
  4.     x:Class="CommandDemo.MainPage" 
  5.  
  6.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  7.  
  8.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  9.  
  10.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  11.  
  12.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  13.  
  14.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  15.  
  16.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  17.  
  18.     xmlns:my="clr-namespace:CommandDemo.ViewModel" 
  19.  
  20.     xmlns:my_Interactivity="clr-namespace:CommandDemo.Command" 
  21.  
  22.     xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" 
  23.  
  24.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
  25.  
  26.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  27.  
  28.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  29.  
  30.     Foreground="{StaticResource PhoneForegroundBrush}" 
  31.  
  32.     SupportedOrientations="Portrait" Orientation="Portrait" 
  33.  
  34.     shell:SystemTray.IsVisible="True"> 
  35.  
  36.     <!--设置整个页面的上下文数据DataContext为RadiusViewModel--> 
  37.  
  38.     <phone:PhoneApplicationPage.DataContext> 
  39.  
  40.         <my:RadiusViewModel/> 
  41.  
  42.     </phone:PhoneApplicationPage.DataContext> 
  43.  
  44.    
  45.  
  46.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  47.  
  48.         <Grid.RowDefinitions> 
  49.  
  50.             <RowDefinition Height="Auto"/> 
  51.  
  52.             <RowDefinition Height="*"/> 
  53.  
  54.         </Grid.RowDefinitions> 
  55.  
  56.    
  57.  
  58.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  59.  
  60.             <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/> 
  61.  
  62.             <TextBlock x:Name="PageTitle" Text="Command" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  63.  
  64.         </StackPanel> 
  65.  
  66.    
  67.  
  68.    
  69.  
  70.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  71.  
  72.             <Ellipse Fill="Red" 
  73.  
  74.                      Height="{Binding Radius}" Width="{Binding Radius}" 
  75.  
  76.                      HorizontalAlignment="Left" Margin="119,84,0,0" Name="ellipse1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" /> 
  77.  
  78.             <Button Content="小" Height="72" HorizontalAlignment="Left" Margin="0,385,0,0" Name="button1" VerticalAlignment="Top" Width="160"> 
  79.  
  80.                 <Custom:Interaction.Triggers> 
  81.  
  82.                     <Custom:EventTrigger EventName="Click"> 
  83.  
  84.                         <my_Interactivity:ExecuteCommandAction CommandName="MinRadius"/> 
  85.  
  86.                     </Custom:EventTrigger> 
  87.  
  88.                 </Custom:Interaction.Triggers> 
  89.  
  90.             </Button> 
  91.  
  92.             <Button Content="中" Height="72" HorizontalAlignment="Left" Margin="149,384,0,0" Name="button2" VerticalAlignment="Top" Width="160" > 
  93.  
  94.                 <Custom:Interaction.Triggers> 
  95.  
  96.                     <Custom:EventTrigger EventName="Click"> 
  97.  
  98.                         <my_Interactivity:ExecuteCommandAction CommandName="MedRadius"/> 
  99.  
  100.                     </Custom:EventTrigger> 
  101.  
  102.                 </Custom:Interaction.Triggers> 
  103.  
  104.             </Button> 
  105.  
  106.             <Button Content="大" Height="72" HorizontalAlignment="Left" Margin="299,382,0,0" Name="button3" VerticalAlignment="Top" Width="160" > 
  107.  
  108.                 <Custom:Interaction.Triggers> 
  109.  
  110.                     <Custom:EventTrigger EventName="Click"> 
  111.  
  112.                         <my_Interactivity:ExecuteCommandAction  CommandName="MaxRadius"/> 
  113.  
  114.                     </Custom:EventTrigger> 
  115.  
  116.                 </Custom:Interaction.Triggers> 
  117.  
  118.             </Button> 
  119.  
  120.         </Grid> 
  121.  
  122.     </Grid> 
  123.  
  124.    
  125.  
  126. </phone:PhoneApplicationPage> 

(2)RadiusViewModel.cs文件的代码,实现ViewModel层

 

 
  1.  
  2. using System;  
  3.  
  4. using System.Windows.Input;  
  5.  
  6. using System.ComponentModel;  
  7.  
  8. using Microsoft.Expression.Interactivity.Core;  
  9.  
  10.    
  11.  
  12. namespace CommandDemo.ViewModel  
  13.  
  14. {  
  15.  
  16.     public class RadiusViewModel : INotifyPropertyChanged  
  17.  
  18.     {  
  19.  
  20.         private Double radius;  
  21.  
  22.    
  23.  
  24.         public RadiusViewModel()  
  25.  
  26.         {  
  27.  
  28.             Radius = 0;  
  29.  
  30.             MinRadius = new ActionCommand(p => Radius = 100);  
  31.  
  32.             MedRadius = new ActionCommand(p => Radius = 200);  
  33.  
  34.             MaxRadius = new ActionCommand(p => Radius = 300);  
  35.  
  36.         }  
  37.  
  38.    
  39.  
  40.         public event PropertyChangedEventHandler PropertyChanged;  
  41.  
  42.    
  43.  
  44.         public ICommand MinRadius  
  45.  
  46.         {  
  47.  
  48.             get; private set;  
  49.  
  50.         }  
  51.  
  52.    
  53.  
  54.         public ICommand MedRadius  
  55.  
  56.         {  
  57.  
  58.             get;  
  59.  
  60.             private set;  
  61.  
  62.         }  
  63.  
  64.    
  65.  
  66.         public ICommand MaxRadius  
  67.  
  68.         {  
  69.  
  70.             get;  
  71.  
  72.             private set;  
  73.  
  74.         }  
  75.  
  76.    
  77.  
  78.         public Double Radius  
  79.  
  80.         {  
  81.  
  82.             get  
  83.  
  84.             {  
  85.  
  86.                 return radius;  
  87.  
  88.             }  
  89.  
  90.             set  
  91.  
  92.             {  
  93.  
  94.                 radius = value;  
  95.  
  96.                 OnPropertyChanged("Radius");  
  97.  
  98.             }  
  99.  
  100.         }  
  101.  
  102.    
  103.  
  104.         protected virtual void OnPropertyChanged(string propertyName)  
  105.  
  106.         {  
  107.  
  108.             var propertyChanged = PropertyChanged;  
  109.  
  110.    
  111.  
  112.             if(propertyChanged != null)  
  113.  
  114.                 propertyChanged(this, new PropertyChangedEventArgs(propertyName));  
  115.  
  116.         }  
  117.  
  118.     }  
  119.  

(3)ExecuteCommandAction.cs类,实现Command操作

 
 

 
  1.  
  2. using System;  
  3.  
  4. using System.Windows;  
  5.  
  6. using System.Windows.Input;  
  7.  
  8. using System.Windows.Interactivity;  
  9.  
  10. using System.Reflection;  
  11.  
  12.    
  13.  
  14. namespace CommandDemo.Command  
  15.  
  16. {  
  17.  
  18.     public class ExecuteCommandAction : TriggerAction<FrameworkElement> 
  19.  
  20.     {  
  21.  
  22.         public static readonly DependencyProperty CommandNameProperty =  
  23.  
  24.              DependencyProperty.Register("CommandName", typeof(string), typeof(ExecuteCommandAction), null);  
  25.  
  26.    
  27.  
  28.         public static readonly DependencyProperty CommandParameterProperty =  
  29.  
  30.             DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExecuteCommandAction), null);  
  31.  
  32.    
  33.  
  34.         protected override void Invoke(object parameter)  
  35.  
  36.         {  
  37.  
  38.             if (AssociatedObject == null)  
  39.  
  40.                 return;  
  41.  
  42.    
  43.  
  44.             ICommand command = null;  
  45.  
  46.    
  47.  
  48.             var dataContext = AssociatedObject.DataContext;  
  49.  
  50.    
  51.  
  52.             foreach (var info in dataContext.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))  
  53.  
  54.             {  
  55.  
  56.                 if (IsCommandProperty(info) && String.Equals(info.Name, CommandName, StringComparison.Ordinal))  
  57.  
  58.                 {  
  59.  
  60.                     command = (ICommand)info.GetValue(dataContext, null);  
  61.  
  62.                     break;  
  63.  
  64.                 }  
  65.  
  66.             }  
  67.  
  68.    
  69.  
  70.             if ((command != null) && command.CanExecute(CommandParameter))  
  71.  
  72.             {  
  73.  
  74.                 command.Execute(CommandParameter);  
  75.  
  76.             }  
  77.  
  78.         }  
  79.  
  80.    
  81.  
  82.         private static bool IsCommandProperty(PropertyInfo property)  
  83.  
  84.         {  
  85.  
  86.             return typeof(ICommand).IsAssignableFrom(property.PropertyType);  
  87.  
  88.         }  
  89.  
  90.    
  91.  
  92.         public string CommandName  
  93.  
  94.         {  
  95.  
  96.             get  
  97.  
  98.             {  
  99.  
  100.                 return (string)GetValue(CommandNameProperty);  
  101.  
  102.             }  
  103.  
  104.             set  
  105.  
  106.             {  
  107.  
  108.                 SetValue(CommandNameProperty, value);  
  109.  
  110.             }  
  111.  
  112.         }  
  113.  
  114.    
  115.  
  116.         public object CommandParameter  
  117.  
  118.         {  
  119.  
  120.             get  
  121.  
  122.             {  
  123.  
  124.                 return GetValue(CommandParameterProperty);  
  125.  
  126.             }  
  127.  
  128.             set  
  129.  
  130.             {  
  131.  
  132.                 SetValue(CommandParameterProperty, value);  
  133.  
  134.             }  
  135.  
  136.         }  
  137.  
  138.     }  
  139.  

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078577

相关文章
|
21天前
|
固态存储 Windows
台式机的Windows10如何设置性能模式
【5月更文挑战第25天】台式机的Windows10如何设置性能模式
17 0
|
1月前
|
Web App开发 Windows
Windows【Chrome浏览器 02】Auto Dark Mode for Web Contents 无需安装插件开启chrome浏览器黑暗模式
Windows【Chrome浏览器 02】Auto Dark Mode for Web Contents 无需安装插件开启chrome浏览器黑暗模式
164 0
|
8月前
|
Windows
[笔记] Windows内核课程:保护模式《二》段寄存器介绍
[笔记] Windows内核课程:保护模式《二》段寄存器介绍
|
8月前
|
C语言 Windows
[笔记] Windows内核课程:保护模式《一》保护模式
[笔记] Windows内核课程:保护模式《一》保护模式
|
8月前
|
缓存 编译器 调度
[笔记]Windows核心编程《七》用户模式下的线程同步
[笔记]Windows核心编程《七》用户模式下的线程同步
|
Linux Windows
Elasticsearch-03 CentOS7 / Windows上部署Elasticsearch5.6.16集群模式
Elasticsearch-03 CentOS7 / Windows上部署Elasticsearch5.6.16集群模式
105 0
|
SQL 安全 数据库
SQL Server 2005 Windows验证如何改为混合模式验证
SQL Server 2005 Windows验证如何改为混合模式验证
257 0
|
开发工具 iOS开发 MacOS
windows虚拟机vim退出编辑模式
windows虚拟机vim退出编辑模式
432 0
|
Linux C++ Windows
Linux与Windows的UDP通讯代码实现
Linux与Windows的UDP通讯代码实现
350 0
Linux与Windows的UDP通讯代码实现
|
数据安全/隐私保护 Windows
Windows操作系统:测试模式禁用数字签名
程序必须在特殊的启动环境下才能正常使用,尤其是一些非官方或者需要数字签名的驱动程序,而Windows测试模式可以帮助我们解决类似的问题,开启后会帮助我们禁用驱动程序强制签名
306 0
Windows操作系统:测试模式禁用数字签名