123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- // This file is inspired from the MvvmLight library (lbugnion/MvvmLight),
- // more info in ThirdPartyNotices.txt in the root of the project.
- using System;
- using System.Diagnostics.CodeAnalysis;
- using System.Runtime.CompilerServices;
- namespace CommunityToolkit.Mvvm.Input;
- /// <summary>
- /// A command whose sole purpose is to relay its functionality to other
- /// objects by invoking delegates. The default return value for the <see cref="CanExecute"/>
- /// method is <see langword="true"/>. This type does not allow you to accept command parameters
- /// in the <see cref="Execute"/> and <see cref="CanExecute"/> callback methods.
- /// </summary>
- public sealed partial class RelayCommand : IRelayCommand
- {
- /// <summary>
- /// The <see cref="Action"/> to invoke when <see cref="Execute"/> is used.
- /// </summary>
- private readonly Action execute;
- [AllowNull]
- private readonly Action<object?> execute1;
- /// <summary>
- /// The optional action to invoke when <see cref="CanExecute"/> is used.
- /// </summary>
- private readonly Func<bool>? canExecute;
- /// <inheritdoc/>
- public event EventHandler? CanExecuteChanged;
- /// <summary>
- /// Initializes a new instance of the <see cref="RelayCommand"/> class that can always execute.
- /// </summary>
- /// <param name="execute">The execution logic.</param>
- /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="execute"/> is <see langword="null"/>.</exception>
- public RelayCommand(Action execute)
- {
- ArgumentNullException.ThrowIfNull(execute);
- this.execute = execute;
- }
- public RelayCommand(Action<object?> execute)
- {
- ArgumentNullException.ThrowIfNull(execute);
- this.execute1 = execute;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="RelayCommand"/> class.
- /// </summary>
- /// <param name="execute">The execution logic.</param>
- /// <param name="canExecute">The execution status logic.</param>
- /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="execute"/> or <paramref name="canExecute"/> are <see langword="null"/>.</exception>
- public RelayCommand(Action execute, Func<bool> canExecute)
- {
- ArgumentNullException.ThrowIfNull(execute);
- ArgumentNullException.ThrowIfNull(canExecute);
- this.execute = execute;
- this.canExecute = canExecute;
- }
- public RelayCommand(Action<object?> execute, Func<bool> canExecute)
- {
- ArgumentNullException.ThrowIfNull(execute);
- ArgumentNullException.ThrowIfNull(canExecute);
- this.execute1 = execute;
- this.canExecute = canExecute;
- }
- /// <inheritdoc/>
- public void NotifyCanExecuteChanged()
- {
- CanExecuteChanged?.Invoke(this, EventArgs.Empty);
- }
- /// <inheritdoc/>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool CanExecute(object? parameter)
- {
- return this.canExecute?.Invoke() != false;
- }
- public void Execute(object? sender, object? parameter)
- {
- if (execute1 == null) execute();
- else execute1(sender);
- }
- public void Execute(object? parameter)
- {
- if (execute1 == null) execute();
- else execute1(null);
- }
- }
|