Skip to content
This repository was archived by the owner on Aug 27, 2020. It is now read-only.

AsyncDelegateCommand

Mark Smith edited this page Aug 29, 2016 · 1 revision

AsyncDelegateCommand

The AsyncDelegateCommand class is an implementation of IAsyncDelegateCommand and provides an ICommand implementation that can be executed asynchronously.

It comes in two variations: AsyncDelegateCommand which uses an object as the parameter type, and AsyncDelegateCommand<T> which casts the parameter to a T type.

This is a standard ICommand implementation that utilizes delegates similar to the built-in Xamarin.Forms Command class. However, this supports async'/await` and properly pushes exceptions back to the UI thread. In addition, it has a few additional methods you can use:

Tip: you should expose this from a ViewModel as an IAsyncDelegateCommand so it can be replaced for testing.

Methods

  • RaiseCanExecuteChanged : raises the CanExecuteChanged event.
  • ExecuteAsync : provides access to the underlying Task which is running for the command.

Example

public class MyViewModel : SimpleViewModel
{
   public IAsyncDelegateCommand DoSomething { get; private set; }

   public MyViewModel()
   {
      DoSomething = new AsyncDelegateCommand<string>(OnDoSomethingAsync);
   }

   private async Task DoSomethingAsync(string parameter)
   {
       await SomeAsyncOperationHere ...
   }
}