TriggerAction扩展----ExInvokeCommandAction
Wp&Win8中使用命令绑定时,除了Button控件自带命令绑定,其他的时候是用Interactivity库中的InvokeCommandAction实现的(Win8 需要额外安装第三方NuGet包才可使用,我的MVFM示例博客中带有这个库),但使用过程中发现InvokeCommandAction并不能满足我们的要求,主要有以下几点:
/// <summary>
/// 扩展CommandParameter,使CommandParameter可以带事件参数
/// </summary>
public class ExCommandParameter
{
/// <summary>
/// 事件触发源
/// </summary>
public object Sender { get; set; }
/// <summary>
/// 事件参数
/// </summary>
public object EventArgs { get; set; }
/// <summary>
/// 参数
/// </summary>
public object Parameter { get; set; }
/// <summary>
/// 扩展参数
/// </summary>
public object Parameter2 { get; set; }
/// <summary>
/// 扩展参数
/// </summary>
public object Parameter3 { get; set; }
}
然后定义处理的TriggerAction:
/// <summary>
/// 扩展的InvokeCommandAction
/// </summary>
public class ExInvokeCommandAction : CustomTriggerActionBase
{
private string commandName;
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ExInvokeCommandAction), null);
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExInvokeCommandAction), null);
public static readonly DependencyProperty CommandParameter2Property = DependencyProperty.Register("CommandParameter2", typeof(object), typeof(ExInvokeCommandAction), null);
public static readonly DependencyProperty CommandParameter3Property = DependencyProperty.Register("CommandParameter3", typeof(object), typeof(ExInvokeCommandAction), null); /// <summary>
/// 获得或设置此操作应调用的命令的名称。
/// </summary>
/// <value>此操作应调用的命令的名称。</value>
/// <remarks>如果设置了此属性和 Command 属性,则此属性将被后者所取代。</remarks>
public string CommandName
{
get
{
return this.commandName;
}
set
{
if (this.commandName != value)
{
this.commandName = value;
}
}
} /// <summary>
/// 获取或设置此操作应调用的命令。这是依赖属性。
/// </summary>
/// <value>要执行的命令。</value>
/// <remarks>如果设置了此属性和 CommandName 属性,则此属性将优先于后者。</remarks>
public ICommand Command
{
get
{
return (ICommand)base.GetValue(ExInvokeCommandAction.CommandProperty);
}
set
{
base.SetValue(ExInvokeCommandAction.CommandProperty, value);
}
}
/// <summary>
/// 获得或设置命令参数。这是依赖属性。
/// </summary>
/// <value>命令参数。</value>
/// <remarks>这是传递给 ICommand.CanExecute 和 ICommand.Execute 的值。</remarks>
public object CommandParameter
{
get
{
return base.GetValue(ExInvokeCommandAction.CommandParameterProperty);
}
set
{
base.SetValue(ExInvokeCommandAction.CommandParameterProperty, value);
}
} public object CommandParameter2
{
get
{
return base.GetValue(ExInvokeCommandAction.CommandParameter2Property);
}
set
{
base.SetValue(ExInvokeCommandAction.CommandParameter2Property, value);
}
} public object CommandParameter3
{
get
{
return base.GetValue(ExInvokeCommandAction.CommandParameter3Property);
}
set
{
base.SetValue(ExInvokeCommandAction.CommandParameter3Property, value);
}
} /// <summary>
/// 调用操作。
/// </summary>
/// <param name="parameter">操作的参数。如果操作不需要参数,则可以将参数设置为空引用。</param>
protected override void Invoke(object parameter)
{
ICommand command = this.ResolveCommand(); ExCommandParameter exParameter = new ExCommandParameter
{
Sender = this.AssociatedObject,
Parameter = GetValue(CommandParameterProperty),
Parameter2 = GetValue(CommandParameter2Property),
Parameter3 = GetValue(CommandParameter3Property),
EventArgs = parameter }; if (command != null && command.CanExecute(exParameter))
{
command.Execute(exParameter);
}
} //手动触发
public void TriggerCommand()
{
Invoke(null);
} public void TriggerCommand(object CommandParameter)
{
TriggerCommand(null, CommandParameter);
} public void TriggerCommand(object sender = null, object commandParameter = null, object commandParameter2 = null, object commandParameter3 = null)
{
this.CommandParameter = commandParameter;
this.CommandParameter2 = commandParameter2;
this.CommandParameter3 = commandParameter3;
Invoke(null);
} protected ICommand ResolveCommand()
{
ICommand result = null;
if (this.Command != null)
{
result = this.Command;
}
else
{
foreach (PropertyInfo propertyInfo in this.AssociatedObject.GetType().GetTypeInfo().DeclaredProperties)
{
if (typeof(ICommand).GetTypeInfo().IsAssignableFrom(propertyInfo.PropertyType.GetTypeInfo()) && string.Equals(propertyInfo.Name, this.CommandName, StringComparison.Ordinal))
{
result = (ICommand)propertyInfo.GetValue((object)this.AssociatedObject, (object[])null);
}
}
}
return result;
}
}
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<Behavior:ExInvokeCommandAction Command="{Binding Command,Source={StaticResource ViewModel}}" CommandParameter="1" CommandParameter2="2" CommandParameter3="3"/>
</i:EventTrigger>
</i:Interaction.Triggers>
TriggerAction扩展----ExInvokeCommandAction的更多相关文章
- Asp.net Boilerplate之AbpSession扩展
当前Abp版本1.2,项目类型为MVC5. 以属性的形式扩展AbpSession,并在"记住我"后,下次自动登录也能获取到扩展属性的值,版权归"角落的白板报"所 ...
- 恢复SQL Server被误删除的数据(再扩展)
恢复SQL Server被误删除的数据(再扩展) 大家对本人之前的文章<恢复SQL Server被误删除的数据> 反应非常热烈,但是文章里的存储过程不能实现对备份出来的日志备份里所删数据的 ...
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类
.NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...
- 采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2)
前言 Entity Framework 延伸系列目录 今天我们来讲讲EntityFramework.Extended 首先科普一下这个EntityFramework.Extended是什么,如下: 这 ...
- Dapper扩展之~~~Dapper.Contrib
平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill 上一篇文章:Dapper逆天入门~强类型,动态类型 ...
- ExtJS 4.2 Date组件扩展:添加清除按钮
ExtJS中除了提供丰富的组件外,我们还可以扩展他的组件. 在这里,我们将在Date日期组件上添加一个[清除]按钮,用于此组件已选中值的清除. 目录 1. Date组件介绍 2. 主要代码说明 3. ...
- .NET Core的文件系统[5]:扩展文件系统构建一个简易版“云盘”
FileProvider构建了一个抽象文件系统,作为它的两个具体实现,PhysicalFileProvider和EmbeddedFileProvider则分别为我们构建了一个物理文件系统和程序集内嵌文 ...
- Hawk 6. 编译和扩展开发
Hawk是开源项目,因此任何人都可以为其贡献代码.作者也非常欢迎使用者能够扩展出更有用的插件. 编译 编译需要Visual Stuido,版本建议使用2015, 2010及以上没有经过测试,但应该可以 ...
随机推荐
- 接口规范,js处理json,php返回给ajax的数据格式
ajax异步获取php数据. 一般php会在后台处理请求,并返回结果给前端. 必须是echo的方式,不然ajax获取不到. 返回的类型包括,字符串,数字,json. 最常用的就是json. 返回后,前 ...
- C#使用自定义字体
/// <summary> /// 设置字体 /// </summary> /// <param name="path">字体文件路径,包含字体 ...
- oracle ---中文乱码问题
---- 1.原因分析 ---- 通过对用户反映情况的分析,发现字符集的设置不当是影响ORACLE数据库汉字显示的关键问题.那么字符集是怎么一会事呢?字符集是ORACLE 为适应不同语言文字显示而设定 ...
- 使用ResultSet结果集查询数据
直接上下代码: package com.learn.jdbc.chap05; import java.sql.Connection; import java.sql.PreparedStatement ...
- entityframework.extended 配置mysql
entityframework.extended 这个是个很好的扩展,不过由于默认是配置成MSSQL的,今天在github上面 看到一个解决方案,亲测可用,下面贴代码 1.在DbContext 修改默 ...
- MySQL的FORMAT函数用法规则
1.FORMAT函数在mysql中是数据内容格式化的,格式化后得到结果:###,###,#####. ,); 输出结果: ,000.00 2.可以格式化数据为整数或者浮点数. ); 输出结果: 100 ...
- 一个简单的AXIS远程调用Web Service示例
我们通常都将编写好的Web Service发布在Tomcat或者其他应用服务器上,然后通过浏览器调用该Web Service,返回规范的XML文件.但是如果我们不通过浏览器调用,而是通过客户端程序调用 ...
- 【285】ArcPy 暗色窗体设置
预览图 设置如下 Default:
- shelve和hashlib模块
一.shelve模块 shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式. 注意: shelve模块封装了pickle模块,,允许 ...
- UIAtlas
[UIAtlas] UIAtlas is a container that has coordinate information for a bunch of sprites. AtlasType有2 ...