WPF Prism框架下基于MVVM模式的命令、绑定、事件
Prism框架下的自定义路由事件和命令绑定 BaseCode
XAML代码:
<Button x:Class="IM.UI.CommandEx.PrismCommandEx"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Button>
CS代码:
public partial class PrismCommandEx : Button
{
public PrismCommandEx()
{
InitializeComponent();
}
//定义一个路由事件 ClickTimeEx
private readonly RoutedEvent ClickTimeExEvent = EventManager.RegisterRoutedEvent("ClickTimeEx", RoutingStrategy.Bubble, typeof(EventHandler<PrintTimeRoutedEventArgs>), typeof(PrismCommandEx));
public event RoutedEventHandler ClickTimeEx
{
add { AddHandler(ClickTimeExEvent, value); }
remove { RemoveHandler(ClickTimeExEvent, value); }
}
private PrintTimeRoutedEventArgs routeEventArgs = null; //重写Button的OnClick事件,让Click事件去触发定义的ClickTimeEx事件
protected override void OnClick()
{
OnClickEx();
base.OnClick();
}
//定义一个路由事件的处理函数
private void OnClickEx()
{
if (routeEventArgs == null) routeEventArgs = new PrintTimeRoutedEventArgs(ClickTimeExEvent, this, DateTime.Now);
RaiseEvent(routeEventArgs);
}
}
PrismCommandEx
public class PrintTimeRoutedEventArgs : RoutedEventArgs
{
public PrintTimeRoutedEventArgs(RoutedEvent routeEvent, object source)
: base(routeEvent, source)
{ } public PrintTimeRoutedEventArgs(RoutedEvent routeEvent, object source, DateTime clickTime)
: this(routeEvent, source)
{
this.ClickTime = clickTime;
}
public DateTime ClickTime { get; set; }
}
PrintTimeRoutedEventArgs
public class InteractivesCommand : TriggerAction<DependencyObject>
{
private string commandName; //命令名称
//这里其实才是真正的执行命令的中转站,通过给定的命令去执行ViewModel
protected override void Invoke(object parameter)
{
if (this.AssociatedObject != null)
{
ICommand command = this.ResolveCommand();
object[] tempObj = { parameter, CommandParameter, CommandParameterEx };
if ((command != null) && command.CanExecute(tempObj))
{
command.Execute(tempObj);
}
}
} public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(InteractivesCommand), new UIPropertyMetadata(null)); public object CommandParameter
{
get { return (object)GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(InteractivesCommand), new UIPropertyMetadata(null, new PropertyChangedCallback((s, e) =>
{
InteractivesCommand ic = s as InteractivesCommand;
if (ic != null) ic.SynchronizeElementState();
}))); public object CommandParameterEx
{
get { return (object)GetValue(CommandParameterExProperty); }
set { SetValue(CommandParameterExProperty, value); }
}
public static readonly DependencyProperty CommandParameterExProperty =
DependencyProperty.Register("CommandParameterEx", typeof(object), typeof(InteractivesCommand), new UIPropertyMetadata(null, (s, e) =>
{
InteractivesCommand ic = s as InteractivesCommand;
if (ic != null) ic.SynchronizeElementState();
})); #region CRL属性
public string CommandName
{
get
{
this.ReadPreamble(); return this.commandName;
}
set
{
if (this.CommandName != value)
{
this.WritePreamble(); this.commandName = value; this.WritePostscript();
}
}
}
#endregion private void SynchronizeElementState()
{
ICommand command = this.Command;
if (command != null)
{
FrameworkElement associatedObject = this.AssociatedObject as FrameworkElement;
if (associatedObject != null)
{
associatedObject.IsEnabled = command.CanExecute(CommandParameter);
}
}
} private ICommand ResolveCommand()
{
ICommand command = null;
if (this.Command != null)
{
return this.Command;
}
//在注册命令的时,Command为NULL,通过命令名称去在当前的依赖存储环境变量中去查找这个命令,并返回命令
//貌似记忆中有印象,就是说所有的依赖属性,在WPF中都有存放在某一个散列的集合中,在这个依赖属性被使用的时间,才会去创建一个实例,好像是依赖属性的特性
if (this.AssociatedObject != null)
{
foreach (PropertyInfo info in base.AssociatedObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (typeof(ICommand).IsAssignableFrom(info.PropertyType) && string.Equals(info.Name, this.CommandName, StringComparison.Ordinal))
{
command = (ICommand)info.GetValue(base.AssociatedObject, null);
}
}
}
return command;
} }
InteractivesCommand
public class PrismViewModel
{
public PrismViewModel() { }
private DelegateCommand<Object> _commandWithEventArgs;
public ICommand ClickTimeExCommand //要绑定的命令
{
get { return _commandWithEventArgs ?? (_commandWithEventArgs = new DelegateCommand<object>(executeMethod, canExecuteMethod)); }
} private void executeMethod(Object parameter)
{
//parameter 接收的值来源于 InteractivesCommand 类重写的Invoke方法,构建的object[]数据
MessageBox.Show("Prism框架MVVM设计模式测试");
} private bool canExecuteMethod(Object parameter)
{
return true;
}
}
PrismViewModel
控件调用:
<Window x:Class="IM.UI.WinPrismTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myCommandEx="clr-namespace:IM.UI.CommandEx"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
Title="WinPrismTest" Height="" Width="">
<Window.DataContext>
<myCommandEx:PrismViewModel/>
</Window.DataContext>
<Grid>
<myCommandEx:PrismCommandEx x:Name="btnPrismMvvm" Content="Prism框架MVVM测试" VerticalAlignment="Top">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ClickTimeEx">
<myCommandEx:InteractivesCommand Command="{Binding ClickTimeExCommand}" CommandName="ClickTimeExCommand" CommandParameter="" CommandParameterEx="" />
</i:EventTrigger>
</i:Interaction.Triggers>
</myCommandEx:PrismCommandEx>
</Grid>
</Window>
WPF Prism框架下基于MVVM模式的命令、绑定、事件的更多相关文章
- 【工作笔记二】ASP.NET MVC框架下使用MVVM模式
ASP.NET MVC框架下使用MVVM模式 原文:http://www.cnblogs.com/n-pei/archive/2011/07/21/2113022.html 对于asp.net mvc ...
- 【转】ASP.NET MVC框架下使用MVVM模式-KnockOutJS+JQ模板例子
KnockOutJS学习系列----(一) 好几个月没去写博客了,最近也是因为项目紧张,不过这个不是借口,J. 很多时候可能是因为事情一多,然后没法静下来心来去写点东西,学点东西. 也很抱歉,突然看到 ...
- ReactiveCocoa框架下的MVVM模式解读
记录一些MVVM文章中关于ReactiveCocoa的代码: 实例一:带有分页的文章列表,根据文章类别过滤出文章的列表,可以进入文章详细页面 1:YFBlogListViewModel 首先了解关于列 ...
- MVVM模式的命令绑定
命令绑定要达到的效果 命令绑定要关注的核心就是两个方面的问题,命令能否执行和命令怎么执行.也就是说当View中的一个Button绑定了ViewModel中一个命令后,什么时候这个Button是可用的, ...
- WPF自学入门(十一)WPF MVVM模式Command命令 WPF自学入门(十)WPF MVVM简单介绍
WPF自学入门(十一)WPF MVVM模式Command命令 在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新.但是这并不是我们使用MVVM的正确方式 ...
- WPF学习11:基于MVVM Light 制作图形编辑工具(2)
本文是WPF学习10:基于MVVM Light 制作图形编辑工具(1)的后续 这一次的目标是完成 两个任务. 画布 效果: 画布上,选择的方案是:直接以Image作为画布,使用RenderTarget ...
- 在Jena框架下基于MySQL数据库实现本体的存取操作
在Jena框架下基于MySQL数据库实现本体的存取操作 转自:http://blog.csdn.net/jtz_mpp/article/details/6224311 最近在做一个基于本体的管理系统. ...
- WPF学习12:基于MVVM Light 制作图形编辑工具(3)
本文是WPF学习11:基于MVVM Light 制作图形编辑工具(2)的后续 这一次的目标是完成 两个任务. 本节完成后的效果: 本文分为三个部分: 1.对之前代码不合理的地方重新设计. 2.图形可选 ...
- 前端笔记之微信小程序(二){{}}插值和MVVM模式&数据双向绑定&指令&API
一.双花括号{{}}插值和MVVM模式 1.1 体会{{}}插值 index.wxml的标签不是html的那些标签,这里的view就是div. {{}}这样的插值写法,叫做mustache语法.mus ...
随机推荐
- 隐马尔可夫模型(HMM)原理
本文主要讨论隐马尔科夫模型的三大要素,三大假设和三大问题. 1.引入 隐马尔可夫模型是一个关于时序的概率模型,它描述了一个由隐藏的马尔可夫链生成状态序列,再由状态序列生成观测序列的过程.其中,状态之间 ...
- ksoap2- webservice
1.概述 对于J2ME访问远端的Web Service,除了官方标准JSR 172,我们还有两种选择: l kSOAP l Wingfoot Wingfoot是由Win ...
- IOS 程序图标添加数字
+(void)setAppIconNumber:(NSInteger)num{ if ([UIParam getIOSVersion]>8.0) { UIUserNotificationSett ...
- MySQL连接数据库报时区错误:java.sql.SQLException: The server time zone value
连接MySQL数据库时报以下时区错误信息: java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized ...
- Windows7启用超级管理员administrator账户的多种方法
开启win7家庭普通版的超级管理员账户Administrotor WIN7家庭普通版没有"本地用户和组"项,不能通过"用计算机右键-管理"的方法开启超级管理员帐 ...
- js原生设计模式——2面向对象编程之继承—多继承
1.单对象克隆 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- [ Android 五种数据存储方式之一 ] —— SharedPreferences存储数据
SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数. 主要是保存一些常用的配置比如窗口状态,一般在Activity中 重载窗口状态onSaveInstanceS ...
- KingbaseES的DBLink创建
创建一个KDB库,建立dblink,需要在KDB库内访问KINGBASE库中的数据: KINGBASE=#CREATE DATABASE KDB [kingbase@localhost config] ...
- MyBatis CRUD Java POJO操作
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...
- js冒泡排序及计算其运行时间
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...