using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Input;
namespace XLIMS.MVVM.Base
{
///
/// Асинхронная команда без параметра.
///
public class AsyncRelayCommand : ICommand
{
private readonly Func _execute;
private readonly Func? _canExecute;
private bool _isExecuting;
public AsyncRelayCommand(Func execute, Func? canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object? parameter)
{
if (_isExecuting) return false;
return _canExecute == null || _canExecute();
}
public async void Execute(object? parameter)
{
if (!CanExecute(parameter)) return;
try
{
_isExecuting = true;
CommandManager.InvalidateRequerySuggested();
await _execute();
}
finally
{
_isExecuting = false;
CommandManager.InvalidateRequerySuggested();
}
}
public event EventHandler? CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
// Удобный статический метод для создания из метода с параметром (если вдруг понадобится)
public static AsyncRelayCommand FromAsync(Func