relay命令在Windows 8 Windows应用商店的应用程序 [英] Relay Command in windows 8 windows store app
本文介绍了relay命令在Windows 8 Windows应用商店的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有RelayCommand的版本,因为命令管理是不是在win8的地铁应用程序可用?
Is there a version of RelayCommand, since CommandManager is not available in win8 metro apps?
推荐答案
有一个版本< A HREF =https://xp-dev.com/svn/mytoolkit/Shared/MVVM/RelayCommand.cs相对=nofollow>这里。
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;
}
}
这篇关于relay命令在Windows 8 Windows应用商店的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文