-
-
Save NickStrupat/939cf839b32fb0e36eba 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
using System; | |
using System.Threading.Tasks; | |
using System.Windows.Input; | |
class AsyncCommand : ICommand { | |
public Boolean CanExecute(Object parameter = null) => !IsRunning; | |
public event EventHandler CanExecuteChanged = delegate { }; | |
public async void Execute(Object parameter = null) { | |
IsRunning = true; | |
try { | |
await asyncAction(); | |
} | |
finally { | |
IsRunning = false; | |
} | |
} | |
private readonly Func<Task> asyncAction; | |
public AsyncCommand(Func<Task> asyncAction) { | |
this.asyncAction = asyncAction; | |
} | |
private Boolean isRunning; | |
private Boolean IsRunning { | |
get { | |
return isRunning; | |
} | |
set { | |
isRunning = value; | |
CanExecuteChanged.Invoke(this, EventArgs.Empty); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment