mirror of
https://github.com/CommunityToolkit/dotnet.git
synced 2026-10-20 06:07:37 +08:00
92 lines
3.2 KiB
C#
92 lines
3.2 KiB
C#
// 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.
|
|
|
|
#pragma warning disable SA1512
|
|
|
|
// <copyright file="RelayCommand.cs" company="GalaSoft Laurent Bugnion">
|
|
// Copyright © GalaSoft Laurent Bugnion 2009-2016
|
|
// </copyright>
|
|
// ****************************************************************************
|
|
// <author>Laurent Bugnion</author>
|
|
// <email>laurent@galasoft.ch</email>
|
|
// <date>22.4.2009</date>
|
|
// <project>GalaSoft.MvvmLight</project>
|
|
// <web>http://www.mvvmlight.net</web>
|
|
// <license>
|
|
// See license.txt in this project or http://www.galasoft.ch/license_MIT.txt
|
|
// </license>
|
|
// ****************************************************************************
|
|
// <credits>This class was developed by Josh Smith (http://joshsmithonwpf.wordpress.com) and
|
|
// slightly modified with his permission.</credits>
|
|
// ****************************************************************************
|
|
|
|
using System;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace Microsoft.Toolkit.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 class RelayCommand : IRelayCommand
|
|
{
|
|
/// <summary>
|
|
/// The <see cref="Action"/> to invoke when <see cref="Execute"/> is used.
|
|
/// </summary>
|
|
private readonly Action execute;
|
|
|
|
/// <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>
|
|
public RelayCommand(Action execute)
|
|
{
|
|
this.execute = 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>
|
|
public RelayCommand(Action execute, Func<bool> canExecute)
|
|
{
|
|
this.execute = 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;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void Execute(object parameter)
|
|
{
|
|
if (CanExecute(parameter))
|
|
{
|
|
this.execute();
|
|
}
|
|
}
|
|
}
|
|
} |