This repository was archived by the owner on Aug 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
AsyncDelegateCommand
Mark Smith edited this page Aug 29, 2016
·
1 revision
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.
-
RaiseCanExecuteChanged
: raises theCanExecuteChanged
event. -
ExecuteAsync
: provides access to the underlyingTask
which is running for the command.
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 ...
}
}