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 ...
随机推荐
- 转载 twisted(1)--何为异步
Reference: http://www.cnblogs.com/yueerwanwan0204/p/5589860.html 早就想写一篇文章,整体介绍python的2个异步库,twisted和t ...
- Delphi隐藏进程
interface function MyHideProcess: Boolean; implementation uses Windows, Classes, AclAPI, accCtrl; ty ...
- js架构设计模式——前端MVVM框架设计及实现(二)
前端MVVM框架设计及实现(二) 在前端MVVM框架设计及实现(一)中有一个博友提出一个看法: “html中使用mvvm徒增开发成本” 我想这位朋友要表达的意思应该是HTML定义了大量的语法标记,HT ...
- UGUI batch 规则和性能优化
UGUI batch 规则和性能优化 (基础) Unity 绘图性能优化 - Draw Call Batching : http://docs.unity3d.com/Manual/DrawCallB ...
- mysql数据库update时只更新部分数据方法
需求:更新url中最一个字符的'-1'改为'-5',前面的内容保持不变 url列的内容如下:http://h5game.ecs.cedarmg.com/a/captal/dispther.do?dev ...
- ThinkPHP--IS_AJAX
增加IS_GET,IS_POST,IS_PUT,IS_DELETE,IS_AJAX常量,方便除控制器外的地方判断方法,Action类的isGet isPost等方法暂时保留,但不建议使用.
- 理解FMS中的实例
FMS服务器端安装后,唯一需要注意的是设置端口,默认的访问端口是1935和80,如果服务器上安装了IIS提供 WEB服务,那么需要将80修改为其他端口如8080,否则,IIS将会无法工作.如果愿意,也 ...
- Canvas rotate- 旋转
Canvas rotate- 旋转 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- 锁 和 CopyOnWrite的实现
1.普通锁 只有lock功能, Java实现:ReentrantLock lock = new ReentrantLock(); lock和unlock: lock.lock(); lock.unlo ...
- WinForm 进程、线程
一.进程 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动. 它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体. Process 类,用来操作进程. 命名空间:using Sys ...