Created
November 17, 2014 10:25
-
-
Save rossdargan/55436d20e9e845196fce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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