first edit
This commit is contained in:
112
XLIMS.MVVM/Base/AsyncRelayCommand.cs
Normal file
112
XLIMS.MVVM/Base/AsyncRelayCommand.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace XLIMS.MVVM.Base
|
||||
{
|
||||
/// <summary>
|
||||
/// Асинхронная команда без параметра.
|
||||
/// </summary>
|
||||
public class AsyncRelayCommand : ICommand
|
||||
{
|
||||
private readonly Func<Task> _execute;
|
||||
private readonly Func<bool>? _canExecute;
|
||||
|
||||
private bool _isExecuting;
|
||||
|
||||
public AsyncRelayCommand(Func<Task> execute, Func<bool>? 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<object?, Task> execute, Func<object?, bool>? canExecute = null)
|
||||
{
|
||||
return new AsyncRelayCommand(() => execute(null), canExecute == null ? null : () => canExecute(null));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Асинхронная команда с параметром (типизированная).
|
||||
/// </summary>
|
||||
public class AsyncRelayCommand<T> : ICommand
|
||||
{
|
||||
private readonly Func<T?, Task> _execute;
|
||||
private readonly Predicate<T?>? _canExecute;
|
||||
|
||||
private bool _isExecuting;
|
||||
|
||||
public AsyncRelayCommand(Func<T?, Task> execute, Predicate<T?>? canExecute = null)
|
||||
{
|
||||
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public bool CanExecute(object? parameter)
|
||||
{
|
||||
if (_isExecuting) return false;
|
||||
if (_canExecute == null) return true;
|
||||
|
||||
// Если параметр неправильного типа — считаем, что нельзя выполнить
|
||||
return parameter is T typedParam && _canExecute(typedParam);
|
||||
}
|
||||
|
||||
public async void Execute(object? parameter)
|
||||
{
|
||||
if (!CanExecute(parameter)) return;
|
||||
|
||||
T? typedParam = parameter is T p ? p : default;
|
||||
|
||||
try
|
||||
{
|
||||
_isExecuting = true;
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
|
||||
await _execute(typedParam);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isExecuting = false;
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler? CanExecuteChanged
|
||||
{
|
||||
add { CommandManager.RequerySuggested += value; }
|
||||
remove { CommandManager.RequerySuggested -= value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user