IAsyncRelayCommand.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.ComponentModel;
  5. using System.Threading.Tasks;
  6. namespace CommunityToolkit.Mvvm.Input;
  7. /// <summary>
  8. /// An interface expanding <see cref="IRelayCommand"/> to support asynchronous operations.
  9. /// </summary>
  10. public interface IAsyncRelayCommand : IRelayCommand, INotifyPropertyChanged
  11. {
  12. /// <summary>
  13. /// Gets the last scheduled <see cref="Task"/>, if available.
  14. /// This property notifies a change when the <see cref="Task"/> completes.
  15. /// </summary>
  16. Task? ExecutionTask { get; }
  17. /// <summary>
  18. /// Gets a value indicating whether a running operation for this command can currently be canceled.
  19. /// </summary>
  20. /// <remarks>
  21. /// The exact sequence of events that types implementing this interface should raise is as follows:
  22. /// <list type="bullet">
  23. /// <item>
  24. /// The command is initially not running: <see cref="IsRunning"/>, <see cref="CanBeCanceled"/>
  25. /// and <see cref="IsCancellationRequested"/> are <see langword="false"/>.
  26. /// </item>
  27. /// <item>
  28. /// The command starts running: <see cref="IsRunning"/> and <see cref="CanBeCanceled"/> switch to
  29. /// <see langword="true"/>. <see cref="IsCancellationRequested"/> is set to <see langword="false"/>.
  30. /// </item>
  31. /// <item>
  32. /// If the operation is canceled: <see cref="CanBeCanceled"/> switches to <see langword="false"/>
  33. /// and <see cref="IsCancellationRequested"/> switches to <see langword="true"/>.
  34. /// </item>
  35. /// <item>
  36. /// The operation completes: <see cref="IsRunning"/> and <see cref="CanBeCanceled"/> switch
  37. /// to <see langword="false"/>. The state of <see cref="IsCancellationRequested"/> is undefined.
  38. /// </item>
  39. /// </list>
  40. /// This only applies if the underlying logic for the command actually supports cancelation. If that is
  41. /// not the case, then <see cref="CanBeCanceled"/> and <see cref="IsCancellationRequested"/> will always remain
  42. /// <see langword="false"/> regardless of the current state of the command.
  43. /// </remarks>
  44. bool CanBeCanceled { get; }
  45. /// <summary>
  46. /// Gets a value indicating whether a cancelation request has been issued for the current operation.
  47. /// </summary>
  48. bool IsCancellationRequested { get; }
  49. /// <summary>
  50. /// Gets a value indicating whether the command currently has a pending operation being executed.
  51. /// </summary>
  52. bool IsRunning { get; }
  53. /// <summary>
  54. /// Provides a more specific version of <see cref="System.Windows.Input.ICommand.Execute"/>,
  55. /// also returning the <see cref="Task"/> representing the async operation being executed.
  56. /// </summary>
  57. /// <param name="parameter">The input parameter.</param>
  58. /// <returns>The <see cref="Task"/> representing the async operation being executed.</returns>
  59. /// <exception cref="System.ArgumentException">Thrown if <paramref name="parameter"/> is incompatible with the underlying command implementation.</exception>
  60. Task ExecuteAsync(object? parameter);
  61. /// <summary>
  62. /// Communicates a request for cancelation.
  63. /// </summary>
  64. /// <remarks>
  65. /// If the underlying command is not running, or if it does not support cancelation, this method will perform no action.
  66. /// Note that even with a successful cancelation, the completion of the current operation might not be immediate.
  67. /// </remarks>
  68. void Cancel();
  69. }