Xamarin Community Toolkit AsyncCommand
Ermöglicht die sichere Verwendung des Task
Typs mit einer 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
Methoden
Methoden | Rückgabetyp | BESCHREIBUNG |
---|---|---|
ExecuteAsync() | Aufgabe | Führt den Befehl als Aufgabe aus. |
ExecuteAsync(T) | Aufgabe | Führt den Befehl als Aufgabe aus. |
ExecuteAsync(TExecute) | Aufgabe | Führt den Befehl als Aufgabe aus. |
Beispiele
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)
{
// ...
}
}
Beispielprojekt
Dieses Element wird in der Beispiel-App des Xamarin-Community-Toolkits angezeigt.