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及以上没有经过测试,但应该可以 ...
随机推荐
- linux下安装ZipArchive扩展和libzip扩展
在项目开发的时候,由于要下载多个录音文件,我就需要打包下载这个功能 学习源头: https://www.landui.com/help/show-8079 https://www.aliyun.com ...
- eclipse-连接TFS错误 <the server to respond with a valid http response>解决方法
解决办法 如果普通凭证有多个,则将普通凭证给删除.
- cvc-complex-type.2.4.a: Invalid content was found starting with element 'async-supported'
<servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springfr ...
- 用 Python 实现文件查找
用 Python 实现文件查找(BIF实现及队列实现) (1)利用内置函数实现文件查找 1.功能:返回用户输入的文件的绝对路径 2.设计思路: (1)用户输入在哪个盘进行查找 (2)遍历此盘文件,若为 ...
- Object-C 多线程中锁的使用-NSLock
在多线程的编程环境中,锁的使用必不可少! 于是,今天来总结一下为共享资源加锁的操作方法. 一.使用synchronized方式 //线程1 dispatch_async(dispatch_ge ...
- Java开发进阶技能(附文章引用链接)
一.玩转源码 1.Java+Selenium3方法篇0-如何在Eclipse上查看Selenium源码 (在github上下载源码)
- 第10章 深入理解Session与Cookie
需要很多Cookie时,考虑HTTP对Cookie数量和大小的限制. 几百或更多台服务器的时候,如何解决Session在多态服务器之间共享的问题. 还有一些安全问题,如Cookie被盗,Cookie伪 ...
- kafka集群配置和java编写生产者消费者操作例子
kafka 安装 修改配置文件 java操作kafka kafka kafka的操作相对来说简单很多 安装 下载kafka http://kafka.apache.org/downloads tar ...
- 阿里云ECS centos7 支持IPv6
1.编辑 /etc/sysctl.conf 文件,将其中三条禁用IPv6的设置更改为: net.ipv6.conf.all.disable_ipv6 = 0 net.ipv6.conf.default ...
- 在.jsp中非表单请求action的几种方式总结
转自:https://www.jb51.net/article/35621.htm 1 一: 复制代码 代码如下: <a href="userAction.do?flag=user_r ...