Csharp/C#教程:Windows 8 Windows应用中的中继命令分享


Windows 8 Windows应用中的中继命令

是否有版本的RelayCommand,因为在win8 metro应用程序中没有CommandManager?

这里有一个版本。

using System; using System.Diagnostics; #if METRO using Windows.UI.Xaml.Input; using System.Windows.Input; #else using System.Windows.Input; #endif namespace MyToolkit.MVVM { #if METRO public class RelayCommand : NotifyPropertyChanged, ICommand #else public class RelayCommand : NotifyPropertyChanged, ICommand #endif { private readonly Action execute; private readonly Func canExecute; public RelayCommand(Action execute) : this(execute, null) { } public RelayCommand(Action execute, Func canExecute) { if (execute == null) throw new ArgumentNullException("execute"); this.execute = execute; this.canExecute = canExecute; } bool ICommand.CanExecute(object parameter) { return CanExecute; } public void Execute(object parameter) { execute(); } public bool CanExecute { get { return canExecute == null || canExecute(); } } public void RaiseCanExecuteChanged() { RaisePropertyChanged("CanExecute"); if (CanExecuteChanged != null) CanExecuteChanged(this, new EventArgs()); } public event EventHandler CanExecuteChanged; } public class RelayCommand : ICommand { private readonly Action execute; private readonly Predicate canExecute; public RelayCommand(Action execute) : this(execute, null) { } public RelayCommand(Action execute, Predicate canExecute) { if (execute == null) throw new ArgumentNullException("execute"); this.execute = execute; this.canExecute = canExecute; } [DebuggerStepThrough] public bool CanExecute(object parameter) { return canExecute == null || canExecute((T)parameter); } public void Execute(object parameter) { execute((T)parameter); } public void RaiseCanExecuteChanged() { if (CanExecuteChanged != null) CanExecuteChanged(this, new EventArgs()); } public event EventHandler CanExecuteChanged; } } 

如果ICommand在Metro中提供,则没有实现,尽管有几个版本可用,例如CodeProject上的这个版本。

Windows Store应用程序的Prism现在可用,其中包含DelegateCommand(实现ICommand),以及OnPropertyChanged的实现。

上述就是C#学习教程:Windows 8 Windows应用中的中继命令分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1006611.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐