Skip to content

Instantly share code, notes, and snippets.

@rossdargan
Created November 17, 2014 10:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rossdargan/55436d20e9e845196fce to your computer and use it in GitHub Desktop.
Save rossdargan/55436d20e9e845196fce to your computer and use it in GitHub Desktop.
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