2012-09-18 17 views

उत्तर

4

एक संस्करण 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

बस यह इंगित करने के लिए कि इस फ़ाइल में 'NotifyPropertyChanged' क्लास संदर्भ नहीं है, और अधिकांश लोग इसे पुनर्निर्मित करने के बारे में जानेंगे, लेकिन यह उन लोगों के लिए शामिल करना अच्छा हो सकता है जो नहीं करते हैं। –

+2

@ ओवेन जोहानसन 'नोटिफ़ाईप्रॉपर्टी चेंजेड' यहां मिला: https://mytoolkit.svn.codeplex.com/svn/Shared/MVVM/NotifyPropertyChanged.cs – mydogisbox

+0

https://xp-dev.com/svn/mytoolkit/Shared/MVVM/ देखें NotifyPropertyChanged.cs –

1

कोई कार्यान्वयन अगर ICommand मेट्रो में प्रदान की, हालाँकि CodeProject पर इस एक के रूप में उपलब्ध कई संस्करण, कर रहे हैं।

0

विंडोज स्टोर ऐप्स के लिए प्रिज्म अब उपलब्ध है, जिसमें प्रतिनिधिमंडल (जो आईसीओएमएंड लागू करता है), साथ ही ऑनप्रॉपर्टी चेंग्ड का कार्यान्वयन भी शामिल है।

संबंधित मुद्दे