Instantly share code, notes, and snippets.

Embed
What would you like to do?
class AsyncCommand : ICommand
{
private readonly Func<Task> asyncAction;
private bool isRunning = false;
public bool IsRunning
{
get { return isRunning; }
set
{
isRunning = value;
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
public AsyncCommand(Func<Task> asyncAction)
{
this.asyncAction = asyncAction;
}
public bool CanExecute(object parameter)
{
return !isRunning;
}
public event EventHandler CanExecuteChanged;
public async void Execute(object parameter)
{
IsRunning = true;
await asyncAction();
IsRunning = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment