MVVM模式的View与ViewModel的三大通讯方式:Binding Data(实现数据的传递)、Command(实现操作的调用)和Attached Behavior(实现控件加载过程中的操作)。
下面通过一个实例实现MVVM模式的Command通讯
(1)MainPage.xaml文件的代码,实现View层
< phone:PhoneApplicationPage |
x:Class = "CommandDemo.MainPage"
|
xmlns:phone = "clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
|
xmlns:shell = "clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
|
xmlns:my = "clr-namespace:CommandDemo.ViewModel" |
xmlns:my_Interactivity = "clr-namespace:CommandDemo.Command" |
xmlns:Custom = "clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ic = "clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" |
mc:Ignorable = "d" d:DesignWidth = "480" d:DesignHeight = "768"
|
FontFamily = "{StaticResource PhoneFontFamilyNormal}"
|
FontSize = "{StaticResource PhoneFontSizeNormal}"
|
Foreground = "{StaticResource PhoneForegroundBrush}"
|
SupportedOrientations = "Portrait" Orientation = "Portrait"
|
shell:SystemTray.IsVisible = "True" >
|
< phone:PhoneApplicationPage.DataContext >
|
</ phone:PhoneApplicationPage.DataContext >
|
< Grid x:Name = "LayoutRoot" Background = "Transparent" >
|
< RowDefinition Height = "Auto" />
|
< RowDefinition Height = "*" />
|
< StackPanel x:Name = "TitlePanel" Grid.Row = "0" Margin = "12,17,0,28" >
|
< TextBlock x:Name = "ApplicationTitle" Text = "MY APPLICATION" Style = "{StaticResource PhoneTextNormalStyle}" />
|
< TextBlock x:Name = "PageTitle" Text = "Command" Margin = "9,-7,0,0" Style = "{StaticResource PhoneTextTitle1Style}" />
|
< Grid x:Name = "ContentPanel" Grid.Row = "1" Margin = "12,0,12,0" >
|
Height = "{Binding Radius}" Width = "{Binding Radius}" |
HorizontalAlignment = "Left" Margin = "119,84,0,0" Name = "ellipse1" Stroke = "Black" StrokeThickness = "1" VerticalAlignment = "Top" />
|
< Button Content = "小" Height = "72" HorizontalAlignment = "Left" Margin = "0,385,0,0" Name = "button1" VerticalAlignment = "Top" Width = "160" >
|
< Custom:Interaction.Triggers >
|
< Custom:EventTrigger EventName = "Click" >
|
< my_Interactivity:ExecuteCommandAction CommandName = "MinRadius" />
|
</ Custom:Interaction.Triggers >
|
< Button Content = "中" Height = "72" HorizontalAlignment = "Left" Margin = "149,384,0,0" Name = "button2" VerticalAlignment = "Top" Width = "160" >
|
< Custom:Interaction.Triggers >
|
< Custom:EventTrigger EventName = "Click" >
|
< my_Interactivity:ExecuteCommandAction CommandName = "MedRadius" />
|
</ Custom:Interaction.Triggers >
|
< Button Content = "大" Height = "72" HorizontalAlignment = "Left" Margin = "299,382,0,0" Name = "button3" VerticalAlignment = "Top" Width = "160" >
|
< Custom:Interaction.Triggers >
|
< Custom:EventTrigger EventName = "Click" >
|
< my_Interactivity:ExecuteCommandAction CommandName = "MaxRadius" />
|
</ Custom:Interaction.Triggers >
|
</ phone:PhoneApplicationPage >
|
(2)RadiusViewModel.cs文件的代码,实现ViewModel层
using System.Windows.Input;
|
using System.ComponentModel;
|
using Microsoft.Expression.Interactivity.Core;
|
namespace CommandDemo.ViewModel
|
public class RadiusViewModel : INotifyPropertyChanged
|
MinRadius = new ActionCommand(p => Radius = 100);
|
MedRadius = new ActionCommand(p => Radius = 200);
|
MaxRadius = new ActionCommand(p => Radius = 300);
|
public event PropertyChangedEventHandler PropertyChanged;
|
public ICommand MinRadius
|
public ICommand MedRadius
|
public ICommand MaxRadius
|
OnPropertyChanged( "Radius" );
|
protected virtual void OnPropertyChanged( string propertyName)
|
var propertyChanged = PropertyChanged; |
if (propertyChanged != null )
|
propertyChanged( this , new PropertyChangedEventArgs(propertyName));
|
(3)ExecuteCommandAction.cs类,实现Command操作
using System.Windows.Input;
|
using System.Windows.Interactivity;
|
namespace CommandDemo.Command
|
public class ExecuteCommandAction : TriggerAction<FrameworkElement>
|
public static readonly DependencyProperty CommandNameProperty =
|
DependencyProperty.Register( "CommandName" , typeof ( string ), typeof (ExecuteCommandAction), null );
|
public static readonly DependencyProperty CommandParameterProperty =
|
DependencyProperty.Register( "CommandParameter" , typeof ( object ), typeof (ExecuteCommandAction), null );
|
protected override void Invoke( object parameter)
|
if (AssociatedObject == null )
|
var dataContext = AssociatedObject.DataContext; |
foreach (var info in dataContext.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
|
if (IsCommandProperty(info) && String.Equals(info.Name, CommandName, StringComparison.Ordinal))
|
command = (ICommand)info.GetValue(dataContext, null );
|
if ((command != null ) && command.CanExecute(CommandParameter))
|
command.Execute(CommandParameter); |
private static bool IsCommandProperty(PropertyInfo property)
|
return typeof (ICommand).IsAssignableFrom(property.PropertyType);
|
public string CommandName
|
return ( string )GetValue(CommandNameProperty);
|
SetValue(CommandNameProperty, value); |
public object CommandParameter
|
return GetValue(CommandParameterProperty);
|
SetValue(CommandParameterProperty, value); |
分享到:
相关推荐
Wpf Mvvm模式下窗口ShowDialog的实现
MVVM模式MVVM模式MVVM模式MVVM模式MVVM模式MVVM模式MVVM模式MVVM模式MVVM模式
MVVM模式实现图片分页显示 (1)数据使用WCF服务加载 (2)定制ListBox数据模板 (3)采用MVVM模式实现分页 注意:在运行前需要修改WCF服务Service1.svc方法GetAllPictureData中的图片文件夹路径,默认是C:\Users\My...
开发实例篇以应用实例的方式综合地讲解了Windows Phone 7各种编程技术的运用,以实例的训练来加强对Windows Phone 7的技术知识的理解。 本书覆盖面广,实例丰富,注重理论与实践的结合,并提供了书中所有范例的源...
学习WPF时做的小例子,MVVM模式延时加载一棵树。解决系统中数据量比较大,树加载过程过慢的问题。
WPF MVVM Command
这个Demo是WPF的MVVM模式的一个登录窗口的完整实例,包含了在MVVM模式下的数据绑定、命令和事件、PasswordBox的绑定、RadioButton等一对多控件的绑定、关闭窗口和打开新窗口和数据验证等内容。
WPF-MVVM WPF MVVM模式开发的例子程序
用MVVM架构实现的计算器小程序实例, 包括MVVM架构的命令对象和消息通知对象,小计算器加减乘除的实现逻辑.页面布局等简单知识.供WPF和MVVM初学者参考. 参考代码: /// /// 加法命令 /// public BaseCommand Add...
C#_WPF_MVVM模式完整实例(比较不错) 介绍的很好
实现mvvm模式 Silverlight
wpf闹钟 设计模式 Mvvm模式 Mvvmlight
非常适合初学者的文档。一旦开发者习惯于WPF和MVVM,将很难区分两者的区别, MVVM是WPF开发者的通用语,因为他很适合于WPF平台。经理翻译英文版给我看的。
MVVM中的,如何通过命令实现窗体之间的跳转、拖动以及显隐控制一直是困扰初学者的一个难题,本程序通过简单的示例代码,实现了这些功能,可以帮助初学者快速掌握这些基础操作。
WPF设计的一个简单的计算器 使用MVVM模式 调用委托 对于WPF入门有很大的帮助 C#
使用TypeScript创建MVVM框架模型,以此来开发大型的windows应用商店程序或者其它web程序。 Use TypeScript to accomplish MVVM pattern to develop windows store app or other web application.
MVVM模式主要是为了实现视图和逻辑的分离,通常在实现的例子之中,在ViewModel一层会使用RealCommand,下面是我在网上根据实践,得到的完整实例
简单手写android开发音乐中MVP模式和MVVM模式的区别,play目录下是mvp模式,myplayer目录下时mvvmm模式-主要是 实现DataListenerContainer容器,musicList 目录主要是解决UI线程和工作线程的切换导致的问题
MVVM模式 小例子,用来帮助理解WPF的MVVM模式
最近项目中要使用到MVC模式进行开发,写了一个小例子,希望对部分朋友有一定的帮助。 另附有一份MVVM模式源码。