I am using the @Stephen-Cleary AsyncCommand implementation in WPF (.NET 4.0) and now I am trying to find out how to specify the CanExecute handler during command definition.
Usually I create the command like this:
SaveCommandAsync = AsyncCommand.Create(async token =>
{
//async code
});
I don't see any Create overload so I can specify CanExecute logic.
Thank you,
Igor
Use Stephen Cleary's Nito.Mvvm.Async project to achieve what you need.
Add nuget reference to the package:
<package id="Nito.Mvvm.Async" version="1.0.0-eta-05" targetFramework="net45" />
Create Xaml binding:
<Button Content="Toggle" Command="{Binding MyAsyncCommand}"></Button>
Create CustomAsyncCommand, specifying CanExecute function
MyAsyncCommand = new CustomAsyncCommand(AsyncAction, x=> !_isWorking);
Do some async work in AsyncAction
private async Task AsyncAction(object obj) {
_isWorking = true;
MyAsyncCommand.OnCanExecuteChanged();
await Task.Delay(2000);
_isWorking = false;
MyAsyncCommand.OnCanExecuteChanged();
}
And finally: enjoy.