Binding Commands
This commit is contained in:
parent
d3327089ff
commit
22f7143d6e
4 changed files with 156 additions and 16 deletions
52
imagecatalog/Commands/AsyncCommand.cs
Normal file
52
imagecatalog/Commands/AsyncCommand.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace ImageCatalog_2.Commands
|
||||
{
|
||||
public class AsyncCommand : ICommand
|
||||
{
|
||||
private readonly Func<Task> _execute;
|
||||
private readonly Func<bool> _canExecute;
|
||||
private bool _isExecuting;
|
||||
|
||||
public event EventHandler CanExecuteChanged;
|
||||
|
||||
public AsyncCommand(Func<Task> execute, Func<bool> canExecute = null)
|
||||
{
|
||||
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
return (_canExecute?.Invoke() ?? true) && !_isExecuting;
|
||||
}
|
||||
|
||||
public async void Execute(object parameter)
|
||||
{
|
||||
if (CanExecute(parameter))
|
||||
{
|
||||
try
|
||||
{
|
||||
_isExecuting = true;
|
||||
RaiseCanExecuteChanged();
|
||||
await _execute();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isExecuting = false;
|
||||
RaiseCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RaiseCanExecuteChanged()
|
||||
{
|
||||
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue