2010-07-27 4 views
2

J'utilise ce code pour faire une commande simple:WPF SimpleCommand possible avec les génériques?

public class SimpleCommand : ICommand 
{ 
    public Predicate<object> CanExecuteDelegate { get; set; } 
    public Action<object> ExecuteDelegate { get; set; } 

    #region ICommand Members 

    public bool CanExecute(object parameter) 
    { 
     if (CanExecuteDelegate != null) 
      return CanExecuteDelegate(parameter); 
     return true;// if there is no can execute default to true 
    } 

    public event EventHandler CanExecuteChanged 
    { 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 

    public void Execute(object parameter) 
    { 
     if (ExecuteDelegate != null) 
      ExecuteDelegate(parameter); 
    } 

    #endregion 
} 

Je n'ai pas écrit cela. Mais j'aime l'utiliser. Quand je l'utilise, il finit par être comme ceci:

// This is the value that gets set to the command in the UI 
public SimpleCommand DoSomethingCommand { get; set; } 


public DoSomethingCommandConstructor() 
{ 
    DoSomethingCommand = new SimpleCommand 
         { 
          ExecuteDelegate = x => RunCommand(x) 
         }; 
} 

private void RunCommand(object o) 
{ 
    // Run the command. 
} 

Le seul problème est que le paramètre de RunCommand est un objet. Je pense que j'ai été gâté par les génériques. Je veux toujours que l'EDI/compilateur sache juste avec quel type de travail je travaille.

Est-il possible de changer cette classe SimpleCommand pour qu'elle soit implémentée en utilisant des génériques?

Répondre

5

Bien sûr. Je voulais vous montrer l'implémentation de Prism, mais l'onglet source de CodePlex ne semble pas fonctionner. Il ressemblerait à quelque chose comme:

public class SimpleCommand<T> : ICommand 
{ 
    public Predicate<T> CanExecuteDelegate { get; set; } 
    public Action<T> ExecuteDelegate { get; set; } 

    #region ICommand Members 

    public bool CanExecute(object parameter) 
    { 
     if (CanExecuteDelegate != null) 
      return CanExecuteDelegate((T)parameter); 
     return true;// if there is no can execute default to true 
    } 

    public event EventHandler CanExecuteChanged 
    { 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 

    public void Execute(object parameter) 
    { 
     if (ExecuteDelegate != null) 
      ExecuteDelegate((T)parameter); 
    } 

    #endregion 
} 

Soit dit en passant, votre utilisation de SimpleCommand dans votre question est un peu rond-point. Au lieu de cela:

DoSomethingCommand = new SimpleCommand 
        { 
         ExecuteDelegate = x => RunCommand(x) 
        }; 

Vous pourriez avoir:

DoSomethingCommand = new SimpleCommand 
        { 
         ExecuteDelegate = this.RunCommand 
        }; 

La spécification d'un lambda est vraiment utile que si vous faites la ligne comme de travail ceci:

DoSomethingCommand = new SimpleCommand 
        { 
         ExecuteDelegate = o => this.SelectedItem = o, 
         CanExecuteDelegate = o => o != null 
        }; 
+0

Merci pour la réponse et le point vers Prism. Je pense que c'est le lien que vous alliez me montrer: http://compositewpf.codeplex.com/SourceControl/changeset/view/48968#839830 – Vaccano

Questions connexes