Files
DaSaSo/DaSaSo.ViewModel/Commands/AsyncCommandBase.cs
2021-09-28 10:46:16 +02:00

46 lines
1.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace DaSaSo.ViewModel.Commands
{
public abstract class AsyncCommandBase : ICommand
{
bool _isExecuting = false;
public event EventHandler? CanExecuteChanged;
public bool IsExecuting
{
get => _isExecuting;
set
{
_isExecuting = value;
CanExecuteChanged?.Invoke(this, new EventArgs());
}
}
protected void OnCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, new EventArgs());
}
public virtual bool CanExecute(object? parameter)
{
return !IsExecuting;
}
public async void Execute(object? parameter)
{
IsExecuting = true;
await ExecuteAsync(parameter);
IsExecuting = false;
}
public abstract Task ExecuteAsync(object? parameter);
}
}