Je lis Josh Smith WPF Apps With The Model-View-ViewModel Design Pattern tutorielBesoin d'aide pour comprendre MVVM Tutorial, RelayCommand et utilisation des lambdas
Je ne comprends pas ce que le code ci-dessous tente de faire.
D'abord, la syntaxe me rappelle les propriétés, mais avec add/remove à la place. Mais qu'est-ce que CommandManager.RequerySuggested
?
Il délègue la souscription d'événement pour l'événement CommandManager.RequerySuggested . Cela garantit que l'infrastructure commandant WPF demande à tous objets RelayCommand si elles peuvent exécuter chaque fois qu'il demande au haut- commandes
//Figure 3 The RelayCommand Class
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 }
En outre, commande de sauvegarde est configuré avec lambdas. 1er, il y a 2 variables param. Vont-ils en conflit? Je ne peux pas faire quelque chose comme RelayCommand(this.Save(), this.CanSave)
ou est-ce qu'il n'y a pas une telle syntaxe.
_saveCommand = new RelayCommand(param => this.Save(),
param => this.CanSave);
Cela pourrait vous aider. http://stackoverflow.com/questions/2281566/is-josh-smiths-implementation-of-the-relaycommand-flawed –