PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Foundation/RelayCommand.cs

https://github.com/cmath/SimpleLoginManager
C# | 58 lines | 46 code | 12 blank | 0 comment | 3 complexity | 87db72326566728241bcd1d510e7aa8c MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Input;
  8. namespace Login.Helpers
  9. {
  10. public class RelayCommand : ICommand
  11. {
  12. #region Fields
  13. readonly Action<object> _execute;
  14. readonly Predicate<object> _canExecute;
  15. #endregion // Fields
  16. #region Constructors
  17. public RelayCommand ( Action<object> execute )
  18. : this(execute, null)
  19. {
  20. }
  21. public RelayCommand ( Action<object> execute, Predicate<object> canExecute )
  22. {
  23. if ( execute == null )
  24. throw new ArgumentNullException("execute");
  25. _execute = execute;
  26. _canExecute = canExecute;
  27. }
  28. #endregion // Constructors
  29. #region ICommand Members
  30. [DebuggerStepThrough]
  31. public bool CanExecute ( object parameter )
  32. {
  33. return _canExecute == null ? true : _canExecute(parameter);
  34. }
  35. public event EventHandler CanExecuteChanged
  36. {
  37. add { CommandManager.RequerySuggested += value; }
  38. remove { CommandManager.RequerySuggested -= value; }
  39. }
  40. public void Execute ( object parameter )
  41. {
  42. _execute(parameter);
  43. }
  44. #endregion // ICommand Members
  45. }
  46. }