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模式的命令、绑定、事件的更多相关文章

  1. 【工作笔记二】ASP.NET MVC框架下使用MVVM模式

    ASP.NET MVC框架下使用MVVM模式 原文:http://www.cnblogs.com/n-pei/archive/2011/07/21/2113022.html 对于asp.net mvc ...

  2. 【转】ASP.NET MVC框架下使用MVVM模式-KnockOutJS+JQ模板例子

    KnockOutJS学习系列----(一) 好几个月没去写博客了,最近也是因为项目紧张,不过这个不是借口,J. 很多时候可能是因为事情一多,然后没法静下来心来去写点东西,学点东西. 也很抱歉,突然看到 ...

  3. ReactiveCocoa框架下的MVVM模式解读

    记录一些MVVM文章中关于ReactiveCocoa的代码: 实例一:带有分页的文章列表,根据文章类别过滤出文章的列表,可以进入文章详细页面 1:YFBlogListViewModel 首先了解关于列 ...

  4. MVVM模式的命令绑定

    命令绑定要达到的效果 命令绑定要关注的核心就是两个方面的问题,命令能否执行和命令怎么执行.也就是说当View中的一个Button绑定了ViewModel中一个命令后,什么时候这个Button是可用的, ...

  5. WPF自学入门(十一)WPF MVVM模式Command命令 WPF自学入门(十)WPF MVVM简单介绍

    WPF自学入门(十一)WPF MVVM模式Command命令   在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新.但是这并不是我们使用MVVM的正确方式 ...

  6. WPF学习11:基于MVVM Light 制作图形编辑工具(2)

    本文是WPF学习10:基于MVVM Light 制作图形编辑工具(1)的后续 这一次的目标是完成 两个任务. 画布 效果: 画布上,选择的方案是:直接以Image作为画布,使用RenderTarget ...

  7. 在Jena框架下基于MySQL数据库实现本体的存取操作

    在Jena框架下基于MySQL数据库实现本体的存取操作 转自:http://blog.csdn.net/jtz_mpp/article/details/6224311 最近在做一个基于本体的管理系统. ...

  8. WPF学习12:基于MVVM Light 制作图形编辑工具(3)

    本文是WPF学习11:基于MVVM Light 制作图形编辑工具(2)的后续 这一次的目标是完成 两个任务. 本节完成后的效果: 本文分为三个部分: 1.对之前代码不合理的地方重新设计. 2.图形可选 ...

  9. 前端笔记之微信小程序(二){{}}插值和MVVM模式&数据双向绑定&指令&API

    一.双花括号{{}}插值和MVVM模式 1.1 体会{{}}插值 index.wxml的标签不是html的那些标签,这里的view就是div. {{}}这样的插值写法,叫做mustache语法.mus ...

随机推荐

  1. CMake Tutorial

    1.最简实例 使用cmake的最简实例是由一个源程序文件生成一个可执行文件.例如由下述C++源程序文件生成可执行文件tutorial. main.cpp #include<iostream> ...

  2. WGCNA算法研究笔记

    转自:http://www.gogoqq.com/ASPX/8390905/JournalContent/1303140588.aspx 研究了近半年的算法,记录下来给自己一个交代,也应该是考G前地最 ...

  3. Aptana插件安装、配置

    本文讲解在线安装的方式: 1.eclipse->help->Install New SoftWare... 在弹出的对话框Work with中填入[http://download.apta ...

  4. tomcat 安全文件夹(Java之负基础实战)

    tomcat 解析网站的时候,会寻找一个文件叫  WEB-INF 这些文件外部无法访问

  5. Selenium2(java)TestNG的使用 七

      TestNG,即Testing Next Generation,下一代测试技术,是一套根据JUnit和NUnit思想而构建的利用注释来强化测试功能的一个测试框架,即可以用来做单元测试,也可以用来做 ...

  6. KingbaseES的DBLink创建

    创建一个KDB库,建立dblink,需要在KDB库内访问KINGBASE库中的数据: KINGBASE=#CREATE DATABASE KDB [kingbase@localhost config] ...

  7. Flex4 布局 元素index

    Flex4 布局 元素index <?xml version="1.0" encoding="utf-8"?> <s:Application ...

  8. 将SWF文件用作资源打包

    使用Flash开发网页游戏少不了与各种美术资源打交道.对于静态资源的那就是各种图片,对于会动的资源可以考虑直接做成swf.制作成swf的美术资源又可以分为两种:一种是直接将关键帧罗列在主时间轴上,那么 ...

  9. 小试 Ninja

    Ninja 是最近冒出来的一个 build system,它很像 make,然而效率更高,对大项目支持更好.当然我用 Ninja 和效率无关(我又没有那种有几百个中间目标的 C++ 项目要 build ...

  10. log4j.appender.stdout.layout.ConversionPattern

    http://501565246-qq-com.iteye.com/blog/1991881 http://wenku.baidu.com/link?url=e4Z9v9CY_gwRxHrggzHXx ...