DropDownMenuCommand.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Input;
  7. namespace InABox.Mobile
  8. {
  9. /// <summary>
  10. /// A command whose sole purpose is to relay its functionality
  11. /// to other objects by invoking delegates.
  12. /// The default return value for the CanExecute method is 'true'.
  13. /// <see cref="RaiseCanExecuteChanged"/> needs to be called whenever
  14. /// <see cref="CanExecute"/> is expected to return a different value.
  15. /// </summary>
  16. public class DropDownMenuCommand : ICommand
  17. {
  18. private readonly Action _execute;
  19. private readonly Func<bool> _canExecute;
  20. /// <summary>
  21. /// Raised when RaiseCanExecuteChanged is called.
  22. /// </summary>
  23. public event EventHandler CanExecuteChanged;
  24. /// <summary>
  25. /// Creates a new command that can always execute.
  26. /// </summary>
  27. /// <param name="execute">The execution logic.</param>
  28. public DropDownMenuCommand(Action execute)
  29. : this(execute, null)
  30. {
  31. }
  32. /// <summary>
  33. /// Creates a new command.
  34. /// </summary>
  35. /// <param name="execute">The execution logic.</param>
  36. /// <param name="canExecute">The execution status logic.</param>
  37. public DropDownMenuCommand(Action execute, Func<bool> canExecute)
  38. {
  39. if (execute == null)
  40. throw new ArgumentNullException("execute");
  41. _execute = execute;
  42. _canExecute = canExecute;
  43. }
  44. /// <summary>
  45. /// Determines whether this <see cref="RelayCommand"/> can execute in its current state.
  46. /// </summary>
  47. /// <param name="parameter">
  48. /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
  49. /// </param>
  50. /// <returns>true if this command can be executed; otherwise, false.</returns>
  51. public bool CanExecute(object parameter)
  52. {
  53. return _canExecute == null ? true : _canExecute();
  54. }
  55. /// <summary>
  56. /// Executes the <see cref="RelayCommand"/> on the current command target.
  57. /// </summary>
  58. /// <param name="parameter">
  59. /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
  60. /// </param>
  61. public void Execute(object parameter)
  62. {
  63. _execute();
  64. }
  65. /// <summary>
  66. /// Method used to raise the <see cref="CanExecuteChanged"/> event
  67. /// to indicate that the return value of the <see cref="CanExecute"/>
  68. /// method has changed.
  69. /// </summary>
  70. public void RaiseCanExecuteChanged()
  71. {
  72. var handler = CanExecuteChanged;
  73. if (handler != null)
  74. {
  75. handler(this, EventArgs.Empty);
  76. }
  77. }
  78. }
  79. }