mirror of
https://github.com/CommunityToolkit/dotnet.git
synced 2026-10-20 14:16:34 +08:00
92 lines
3.1 KiB
C#
92 lines
3.1 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.
|
|
|
|
using System;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Toolkit.Mvvm.ComponentModel;
|
|
|
|
namespace Microsoft.Toolkit.Mvvm.Input
|
|
{
|
|
/// <summary>
|
|
/// A command that mirrors the functionality of <see cref="RelayCommand"/>, with the addition of
|
|
/// accepting a <see cref="Func{TResult}"/> returning a <see cref="Task"/> as the execute
|
|
/// action, and providing an <see cref="ExecutionTask"/> property that notifies changes when
|
|
/// <see cref="ExecuteAsync"/> is invoked and when the returned <see cref="Task"/> completes.
|
|
/// </summary>
|
|
public sealed class AsyncRelayCommand : ObservableObject, IAsyncRelayCommand
|
|
{
|
|
/// <summary>
|
|
/// The <see cref="Func{TResult}"/> to invoke when <see cref="Execute"/> is used.
|
|
/// </summary>
|
|
private readonly Func<Task> 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="AsyncRelayCommand"/> class that can always execute.
|
|
/// </summary>
|
|
/// <param name="execute">The execution logic.</param>
|
|
public AsyncRelayCommand(Func<Task> execute)
|
|
{
|
|
this.execute = execute;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="AsyncRelayCommand"/> class that can always execute.
|
|
/// </summary>
|
|
/// <param name="execute">The execution logic.</param>
|
|
/// <param name="canExecute">The execution status logic.</param>
|
|
public AsyncRelayCommand(Func<Task> execute, Func<bool> canExecute)
|
|
{
|
|
this.execute = execute;
|
|
this.canExecute = canExecute;
|
|
}
|
|
|
|
private Task? executionTask;
|
|
|
|
/// <inheritdoc/>
|
|
public Task? ExecutionTask
|
|
{
|
|
get => this.executionTask;
|
|
private set => SetAndNotifyOnCompletion(ref executionTask, () => executionTask, value);
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
ExecuteAsync(parameter);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public Task ExecuteAsync(object parameter)
|
|
{
|
|
if (CanExecute(parameter))
|
|
{
|
|
return ExecutionTask = this.execute();
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
} |