This article can be considered a part 2 of Async Command A Modern Implementation of ICommand in which we were addressing the async problem in ICommand by blocking the CanExecuteStatus to false in case the previous task was not yet completed.
In this article, we will address the same problem using a different way we will use CancellationToken to cancel the pending task and execute the new one, imagine you have a news app where the user should click on one title in a listview the app will download the news article and will show it, what happens when the user clicks on a title and before the download complete he clicks on another one.
And you can use it in your ViewModel like this
CommandAsync<Uri> commandAsync = new CommandAsync<Uri>(async (uri,cancellationToken)=>await client.GetAsync(uri,cancellationToken));
or can be simplified to this
CommandAsync<Uri> commandAsync = new CommandAsync<Uri>(httpclient.GetAsync);
Top comments (0)