Click here to Skip to main content
14,279,378 members

Async Command A Modern Implementation of ICommand

Rate this:
0.00 (No votes)
Please Sign up or sign in to vote.
0.00 (No votes)
1 Sep 2019CPOL
ICommand play a very important role in MVVM and it is usually an important feature in all the popular frameworks (MVVMLight , Prism , …) and even the default xamarin forms implementation.. The main problem of the populate ICommands implementations is the lake of async support..
Image 1

ICommand play a very important role in MVVM and it is usually an important feature in all the popular frameworks (MVVMLight , Prism , …) and even the default xamarin forms implementation.

The main problem of the populate ICommands implementations is the lake of async support.

In this article we will discuss an Async implementation for ICommand that prevent concurrent execution by setting canExecute to false while the target is being executed and reset it to true after the task compensations.

<a href="https://medium.com/media/4b3d157a1b18e8741112da009c2c3d3b/href">https://medium.com/media/4b3d157a1b18e8741112da009c2c3d3b/href</a>

This Asyncommand implementation constructor take an async execution delegate and a can execute predicate.

public async void Execute(object parameter)                                                                         {                                                                             try                                                                             {                                                                            _looked = true;                                                                                 CanExecuteChanged?.Invoke(this, new CommandExecuteChangedArgs("Command Looked for async execution"));                                                                                 await _executeTask.Invoke((T) parameter);                                                                                                                                               }                                                                             finally                                                                             {                                                                                 _looked = false;                                                                                 CanExecuteChanged?.Invoke(this, new CommandExecuteChangedArgs("Command Unlooked for async execution terminated"));                                                                             }                                                                                                                                           }

1- Before the task execution ,set the _looked flag to true

2- Raise the CanExecuteChanged

3- Wait for the task completion

4- set _looked flag to false

5- Raise the CanExecuteChanged

public bool CanExecute(object parameter)                                                                         {                                                                             return !_looked && _canExecute.Invoke(parameter);                                                                         }

the canExecute method return false if the _looked flag is true else check _canExecute.

The Usage

The CommandAsync can be used the same way as the normal RelayCommands

AsyncCommand=new CommandAsync<object>((obj)=>Task.Delay(10000));
Image 2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DotnetShtien
Austria Austria
I am a software engineer at PlanRadar currently living in Vienna, Austria. My interests range from technology to web development. I am also interested in programming, xamarin, and mobile development.

Comments and Discussions

 
SuggestionTip Pin
Michael Haephrati9hrs 50mins ago
mvpMichael Haephrati9hrs 50mins ago 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Technical Blog
Posted 1 Sep 2019

Tagged as

Stats

153 views