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. HTML 样式- CSS

    如何使用CSS CSS 是在 HTML 4 开始使用的,是为了更好的渲染HTML元素而引入的. CSS 可以通过以下方式添加到HTML中: 内联样式- 在HTML元素中使用"style&qu ...

  2. AIX上面Oracle数据库相关启动

    1,启动停止Oracle实例 (1) su -oracle (2) echo $ORACLE_SID (3) sqlplus /nolog //以不登录到数据库的方式进入sqlplus环境 (4) c ...

  3. Firebug及YSlow简介与使用图文详解

    Firebug本是Firefox浏览器下一个出色的网页设计插件,随着浏览器的发展,Firebug也推出了支持IE.Opera.Chrome的Firebug Lite.凭借Firebug的出色代码调试功 ...

  4. MyBatis CRUD Java POJO操作

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...

  5. OpenCV框架介绍

    OpenCV框架介绍 概述 OpenCV是一个开放源代码的计算机视觉应用平台,由英特尔公司下属研发中心俄罗斯团队发起该项目,开源BSD证书,OpenCV的目标是实现实时计算机视觉,,是一个跨平台的计算 ...

  6. RIP 相对寻址

    知识共享许可协议本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/rip-relative-addressing 本博客 ...

  7. POJ1753 搜索

    Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on ...

  8. webpack基础入门

    我相信,有不少的朋友对webpack都有或多或少的了解.网上也有了各种各样的文章,文章内作者也写出了不少自己对于webpack这个工具的理解.在我刚刚接触webpack的时候,老实说,网上大部分的文章 ...

  9. IE6支持透明PNG图片解决方案:DD_belatedPNG.js

    DD_belatedPNG.js 是一个能是IE6支持p显示ng透明图片,而且还支持背景循环(background-repeat)和定位(backgrond-position) ,支持focus,Ho ...

  10. Windows服务的创建、安装、卸载

    1.新建Window服务项目 2.添加安装配置文件 3.serviceProcessInstaller1右键属性,设置Account属性为LocalSystem. serviceInstaller1右 ...