C# windows 8 windows应用商店应用程序中的中继命令
由于CommandManager在win8 metro应用程序中不可用,是否有RelayCommand的版本?有一个版本C# windows 8 windows应用商店应用程序中的中继命令,c#,xaml,mvvm,windows-8,C#,Xaml,Mvvm,Windows 8,由于CommandManager在win8 metro应用程序中不可用,是否有RelayCommand的版本?有一个版本 使用系统; 使用系统诊断; #如果地铁 使用Windows.UI.Xaml.Input; 使用System.Windows.Input; #否则 使用System.Windows.Input; #恩迪夫 名称空间MyToolkit.MVVM { #如果地铁 公共类RelayCommand:NotifyPropertyChanged,ICommand #否则 公共类RelayC
使用系统;
使用系统诊断;
#如果地铁
使用Windows.UI.Xaml.Input;
使用System.Windows.Input;
#否则
使用System.Windows.Input;
#恩迪夫
名称空间MyToolkit.MVVM
{
#如果地铁
公共类RelayCommand:NotifyPropertyChanged,ICommand
#否则
公共类RelayCommand:NotifyPropertyChanged,ICommand
#恩迪夫
{
私有只读操作执行;
私有只读功能可执行;
公共中继命令(操作执行)
:this(execute,null){}
公共RelayCommand(操作执行,函数执行)
{
if(execute==null)
抛出新的ArgumentNullException(“执行”);
this.execute=execute;
this.canExecute=canExecute;
}
bool ICommand.CanExecute(对象参数)
{
返回CanExecute;
}
public void Execute(对象参数)
{
执行();
}
公共图书馆
{
获取{return canExecute==null | | canExecute();}
}
public void raisecancecutechanged()
{
RaisePropertyChanged(“CanExecute”);
如果(CanExecuteChanged!=null)
CanExecuteChanged(这是新的EventArgs());
}
公共事件处理程序CanExecuteChanged;
}
公共类中继命令:ICommand
{
私有只读操作执行;
私有只读谓词canExecute;
公共中继命令(操作执行)
:此(执行,空)
{
}
公共RelayCommand(操作执行,谓词canExecute)
{
if(execute==null)
抛出新的ArgumentNullException(“执行”);
this.execute=execute;
this.canExecute=canExecute;
}
[调试步骤至]
公共布尔CanExecute(对象参数)
{
返回canExecute==null | | canExecute((T)参数);
}
public void Execute(对象参数)
{
执行((T)参数);
}
public void raisecancecutechanged()
{
如果(CanExecuteChanged!=null)
CanExecuteChanged(这是新的EventArgs());
}
公共事件处理程序CanExecuteChanged;
}
}
如果Metro中提供了ICommand,则没有实现,尽管有多个版本可用,例如上的这个版本。Prism for Windows应用商店应用现在可用,其中包含DelegateCommand(实现ICommand),以及OnPropertyChanged的一个实现。我要指出的是,该文件没有它引用的NotifyPropertyChanged
类,大多数人都知道如何重新创建它,但对于那些不这样做的人来说,最好将其包括在内。@OwenJohnsonNotifyPropertyChanged
可以在这里找到:
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<RelayCommand>, ICommand
#endif
{
private readonly Action execute;
private readonly Func<bool> canExecute;
public RelayCommand(Action execute)
: this(execute, null) { }
public RelayCommand(Action execute, Func<bool> 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<T> : ICommand
{
private readonly Action<T> execute;
private readonly Predicate<T> canExecute;
public RelayCommand(Action<T> execute)
: this(execute, null)
{
}
public RelayCommand(Action<T> execute, Predicate<T> 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;
}
}