我正在学习用于 C# Silverlight 开发的 MVVM
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
我对上下文中提到的 RelayCommand 类感到困惑。代码是:
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion // ICommand Members
}
为此,我真的不明白在这种情况下是如何工作的_execute
。_canExecute
(我是 C# 新手,甚至不确定什么是 Action 和 Predicate。我知道它们是代表,但它们之间有什么区别以及它们是如何工作的?)
而且,在程序中,我没有明白这一点
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
谁能给我解释一下,谢谢!