Async Command A Modern Implementation of ICommand

Ahmed Fouad
1 min readSep 2, 2019

--

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.

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));

--

--

Ahmed Fouad

Hello, I’m Ahmed. I’m a software engineer living in Vienna, Austria. I am a fan of technology, web development, and programming. I’m also interested in xamarin