| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using System.Windows.Input;
- namespace AGEHAB.RelatorioSeadIpasgo.Presentation.Commands;
- 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;
- _canExecute = canExecute;
- }
- public event EventHandler? CanExecuteChanged;
- public bool CanExecute(object? parameter)
- {
- return !_isExecuting && (_canExecute?.Invoke() ?? true);
- }
- public async void Execute(object? parameter)
- {
- if (!CanExecute(parameter))
- {
- return;
- }
- _isExecuting = true;
- RaiseCanExecuteChanged();
- try
- {
- await _execute();
- }
- finally
- {
- _isExecuting = false;
- RaiseCanExecuteChanged();
- }
- }
- public void RaiseCanExecuteChanged()
- {
- CanExecuteChanged?.Invoke(this, EventArgs.Empty);
- }
- }
|