AsyncCommand.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.ComponentModel;
  3. using System.Threading.Tasks;
  4. using System.Windows.Input;
  5. namespace InABox.Mobile
  6. {
  7. /// <summary>
  8. /// Simplifies using an "async" method as the implementor of a Command.
  9. /// Given "async Task SomeMethod() { ... }", replaces "yourCommand = new Command(async () => await SomeMethod());"
  10. /// with "yourCommand = new AsyncCommand(SomeMethod);".
  11. /// Also works for methods that take a parameter: Given "async Task SomeMethod(object param) { ... }",
  12. /// Usage: "yourCommand = new Command(async (param) => await SomeMethod(param));" again becomes "yourCommand = new AsyncCommand(SomeMethod);".
  13. /// </summary>
  14. public class AsyncCommand : ICommand
  15. {
  16. Func<object, Task> _execute;
  17. Func<object, bool> _canExecute;
  18. /// <summary>
  19. /// Use this constructor for commands that have a command parameter.
  20. /// </summary>
  21. /// <param name="execute"></param>
  22. /// <param name="canExecute"></param>
  23. /// <param name="notificationSource"></param>
  24. public AsyncCommand(Func<object,Task> execute, Func<object, bool> canExecute = null, INotifyPropertyChanged notificationSource = null)
  25. {
  26. _execute = execute;
  27. _canExecute = canExecute ?? (_ => true);
  28. if (notificationSource != null)
  29. {
  30. notificationSource.PropertyChanged += (s, e) => RaiseCanExecuteChanged();
  31. }
  32. }
  33. /// <summary>
  34. /// Use this constructor for commands that don't have a command parameter.
  35. /// </summary>
  36. public AsyncCommand(Func<Task> execute, Func<bool> canExecute = null, INotifyPropertyChanged notificationSource = null)
  37. :this(_ => execute.Invoke(), _ => (canExecute ?? (() => true)).Invoke(), notificationSource)
  38. {
  39. }
  40. public bool CanExecute(object param = null) => _canExecute.Invoke(param);
  41. public Task ExecuteAsync(object param = null) => _execute.Invoke(param);
  42. public async void Execute(object param = null)
  43. {
  44. // TBD: Consider adding exception-handling logic here.
  45. // Without such logic, quoting https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming
  46. // "With async void methods, there is no Task object, so any exceptions thrown out of an async void method will be raised directly on the SynchronizationContext that was active when the async void method started."
  47. await ExecuteAsync(param);
  48. }
  49. public event EventHandler CanExecuteChanged;
  50. public void RaiseCanExecuteChanged()
  51. {
  52. CanExecuteChanged?.Invoke(this, EventArgs.Empty);
  53. }
  54. }
  55. }