RelayCommand.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // This file is inspired from the MvvmLight library (lbugnion/MvvmLight),
  5. // more info in ThirdPartyNotices.txt in the root of the project.
  6. using System;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Runtime.CompilerServices;
  9. namespace CommunityToolkit.Mvvm.Input;
  10. /// <summary>
  11. /// A command whose sole purpose is to relay its functionality to other
  12. /// objects by invoking delegates. The default return value for the <see cref="CanExecute"/>
  13. /// method is <see langword="true"/>. This type does not allow you to accept command parameters
  14. /// in the <see cref="Execute"/> and <see cref="CanExecute"/> callback methods.
  15. /// </summary>
  16. public sealed partial class RelayCommand : IRelayCommand
  17. {
  18. /// <summary>
  19. /// The <see cref="Action"/> to invoke when <see cref="Execute"/> is used.
  20. /// </summary>
  21. private readonly Action execute;
  22. [AllowNull]
  23. private readonly Action<object?> execute1;
  24. /// <summary>
  25. /// The optional action to invoke when <see cref="CanExecute"/> is used.
  26. /// </summary>
  27. private readonly Func<bool>? canExecute;
  28. /// <inheritdoc/>
  29. public event EventHandler? CanExecuteChanged;
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="RelayCommand"/> class that can always execute.
  32. /// </summary>
  33. /// <param name="execute">The execution logic.</param>
  34. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="execute"/> is <see langword="null"/>.</exception>
  35. public RelayCommand(Action execute)
  36. {
  37. ArgumentNullException.ThrowIfNull(execute);
  38. this.execute = execute;
  39. }
  40. public RelayCommand(Action<object?> execute)
  41. {
  42. ArgumentNullException.ThrowIfNull(execute);
  43. this.execute1 = execute;
  44. }
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="RelayCommand"/> class.
  47. /// </summary>
  48. /// <param name="execute">The execution logic.</param>
  49. /// <param name="canExecute">The execution status logic.</param>
  50. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="execute"/> or <paramref name="canExecute"/> are <see langword="null"/>.</exception>
  51. public RelayCommand(Action execute, Func<bool> canExecute)
  52. {
  53. ArgumentNullException.ThrowIfNull(execute);
  54. ArgumentNullException.ThrowIfNull(canExecute);
  55. this.execute = execute;
  56. this.canExecute = canExecute;
  57. }
  58. public RelayCommand(Action<object?> execute, Func<bool> canExecute)
  59. {
  60. ArgumentNullException.ThrowIfNull(execute);
  61. ArgumentNullException.ThrowIfNull(canExecute);
  62. this.execute1 = execute;
  63. this.canExecute = canExecute;
  64. }
  65. /// <inheritdoc/>
  66. public void NotifyCanExecuteChanged()
  67. {
  68. CanExecuteChanged?.Invoke(this, EventArgs.Empty);
  69. }
  70. /// <inheritdoc/>
  71. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72. public bool CanExecute(object? parameter)
  73. {
  74. return this.canExecute?.Invoke() != false;
  75. }
  76. public void Execute(object? sender, object? parameter)
  77. {
  78. if (execute1 == null) execute();
  79. else execute1(sender);
  80. }
  81. public void Execute(object? parameter)
  82. {
  83. if (execute1 == null) execute();
  84. else execute1(null);
  85. }
  86. }