windows-8 – 任何WinRT iCommand / CommandBinding实现示例?
发布时间:2020-12-13 20:24:19 所属栏目:Windows 来源:网络整理
导读:将视图模型中的抽象命令作为XAML / MVVM项目的一个有价值的实践.我明白了.而且,我在WinRT中看到ICommand;但是,我们如何实现呢?我没有找到一个实际工作的样本.有人知道吗 我所有的时间最喜欢的都是由PnP团队提供的DelegateCommand.它允许您创建一个键入的命
将视图模型中的抽象命令作为XAML / MVVM项目的一个有价值的实践.我明白了.而且,我在WinRT中看到ICommand;但是,我们如何实现呢?我没有找到一个实际工作的样本.有人知道吗
我所有的时间最喜欢的都是由PnP团队提供的DelegateCommand.它允许您创建一个键入的命令:
MyCommand = new DelegateCommand<MyEntity>(OnExecute); ... private void OnExecute(MyEntity entity) {...} 它还允许提供一种方法来提升CanExecuteChanged事件(禁用/启用命令) MyCommand.RaiseCanExecuteChanged(); 以下是代码: public class DelegateCommand<T> : ICommand { private readonly Func<T,bool> _canExecuteMethod; private readonly Action<T> _executeMethod; #region Constructors public DelegateCommand(Action<T> executeMethod) : this(executeMethod,null) { } public DelegateCommand(Action<T> executeMethod,Func<T,bool> canExecuteMethod) { _executeMethod = executeMethod; _canExecuteMethod = canExecuteMethod; } #endregion Constructors #region ICommand Members public event EventHandler CanExecuteChanged; bool ICommand.CanExecute(object parameter) { try { return CanExecute((T)parameter); } catch { return false; } } void ICommand.Execute(object parameter) { Execute((T)parameter); } #endregion ICommand Members #region Public Methods public bool CanExecute(T parameter) { return ((_canExecuteMethod == null) || _canExecuteMethod(parameter)); } public void Execute(T parameter) { if (_executeMethod != null) { _executeMethod(parameter); } } public void RaiseCanExecuteChanged() { OnCanExecuteChanged(EventArgs.Empty); } #endregion Public Methods #region Protected Methods protected virtual void OnCanExecuteChanged(EventArgs e) { var handler = CanExecuteChanged; if (handler != null) { handler(this,e); } } #endregion Protected Methods } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 托管 – Windows 2008 VPS的内存量是多少?
- windows – 证书在计算机重启后停止工作
- windows – 我可以通过其他LDAP服务器获取AD进行身份验证吗
- Windows – SQL Server Express 2008 R2或SQL Server 2008
- windows-phone-8.1 – 如何以编程方式检查Windows Phone 8.
- Qt部署;无法找到过程入口点….
- windows-xp – Windows XP挂在登录屏幕上
- Windows – 来自CMD的Cygwin命令很慢;从bash是快
- windows-server-2008 – 如何在Windows Server 2008上禁用N
- 如何在Windows中运行Apache基准测试?
推荐文章
站长推荐
- Grails 2.0.3错误从Windows7中的命令行开始
- windows-server-2008-r2 – 我的WSUS应该在完全同
- 如何使Windows文件锁定更像UNIX文件锁定?
- windows-server-2008-r2 – 入站Windows防火墙核
- windows-server-2008-r2 – 更改系统时间后认证再
- windows-phone-8 – 如何为caliburn.micro添加自
- microsoft-band – 很难创建Microsoft Band磁贴图
- win2003 IIS6+PHP5+MySQL5+Zend Optimizer+phpMy
- windows-server-2008-r2 – 自动从商店中删除受信
- CMake安装不是在Windows上安装库
热点阅读