2012-09-18 15 views

Odpowiedz

4

Istnieje wersja here.

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; 
    } 
} 
+2

Po prostu zwrócę uwagę, że ten plik nie ma klasy "NotifyPropertyChanged", do której się odwołuje, a większość ludzi wie, jak ją odtworzyć, ale dobrze jest dołączyć ją do tych, którzy jej nie mają. –

+2

@OwenJohnson 'NotifyPropertyChanged' znalezione tutaj: https://mytoolkit.svn.codeplex.com/svn/Shared/MVVM/NotifyPropertyChanged.cs – mydogisbox

+0

Zobacz https://xp-dev.com/svn/mytoolkit/Shared/MVVM/ NotifyPropertyChanged.cs –

1

Nie ma realizacja jeśli ICommand przewidziane w metro, choć istnieje kilka wersji dostępne, takie jak ten na CodeProject.

0

Prism dla aplikacji Windows Store jest już dostępna, zawiera DelegateCommand (która implementuje ICommand), a także implementację OnPropertyChanged.

Powiązane problemy