Xamarin Community Toolkit AsyncCommand
Enables the Task type to safely be used asynchronously with an ICommand.
Syntax
AsyncCommand<TExecute, TCanExecute> : IAsyncCommand<TExecute, TCanExecute>
public AsyncCommand(
Func<TExecute, Task> execute,
Func<TCanExecute, bool> canExecute = null,
Action<Exception> onException = null,
bool continueOnCapturedContext = false,
bool allowsMultipleExecutions = true)
AsyncCommand<T> : IAsyncCommand<T>
public AsyncCommand(
Func<T, Task> execute,
Func<object, bool> canExecute = null,
Action<Exception> onException = null,
bool continueOnCapturedContext = false,
bool allowsMultipleExecutions = true)
public AsyncCommand(
Func<T, Task> execute,
Func<bool> canExecute,
Action<Exception> onException = null,
bool continueOnCapturedContext = false,
bool allowsMultipleExecutions = true)
: this(execute, _ => canExecute(), onException, continueOnCapturedContext, allowsMultipleExecutions)
AsyncCommand : IAsyncCommand
public AsyncCommand(
Func<Task> execute,
Func<object, bool> canExecute = null,
Action<Exception> onException = null,
bool continueOnCapturedContext = false,
bool allowsMultipleExecutions = true)
public AsyncCommand(
Func<Task> execute,
Func<bool> canExecute,
Action<Exception> onException = null,
bool continueOnCapturedContext = false,
bool allowsMultipleExecutions = true)
: this(execute, _ => canExecute(), onException, continueOnCapturedContext, allowsMultipleExecutions)
IAsyncCommand<TExecute, TCanExecute>
interface IAsyncCommand<TExecute, TCanExecute> : IAsyncCommand<TExecute>
IAsyncCommand<T>
interface IAsyncCommand<T> : ICommand
IAsyncCommand
interface IAsyncCommand : ICommand
Methods
| Methods | Return Type | Description |
|---|---|---|
| ExecuteAsync() | Task | Executes the Command as a Task. |
| ExecuteAsync(T) | Task | Executes the Command as a Task. |
| ExecuteAsync(TExecute) | Task | Executes the Command as a Task. |
Examples
AsyncCommand
class MyViewModel
{
bool _isBusy;
public MyViewModel()
{
ButtonCommand = new AsyncCommand(() => ExecuteButtonCommand(), _ => !IsBusy);
}
public IAsyncCommand ButtonCommand { get; }
public bool IsBusy
{
get => _isBusy;
set
{
if(_isBusy != value)
{
_isBusy = value;
ButtonCommand.RaiseCanExecuteChanged();
}
}
}
async Task ExecuteButtonCommand()
{
// ...
}
}
AsyncCommand<T>
class MyViewModel
{
bool _isBusy;
public MyViewModel()
{
ButtonCommand = new AsyncCommand<int>(buttonClicks => ExecuteButtonCommand(buttonClicks), _ => !IsBusy);
}
public IAsyncCommand<int> ButtonCommand { get; }
public bool IsBusy
{
get => _isBusy;
set
{
if(_isBusy != value)
{
_isBusy = value;
ButtonCommand.RaiseCanExecuteChanged();
}
}
}
async Task ExecuteButtonCommand(int buttonClicks)
{
// ...
}
}
AsyncCommand<TExecute, TCanExecute>
class MyViewModel
{
public MyViewModel()
{
ButtonCommand = new AsyncCommand<int, bool>(buttonClicks => ExecuteButtonCommand(buttonClicks), isBusy => !isBusy);
}
public IAsyncCommand<int, bool> ButtonCommand { get; }
async Task ExecuteButtonCommand(int buttonClicks)
{
// ...
}
}
Sample project
You can see this element in action in the Xamarin community toolkit sample app.