Xamarin Community Toolkit AsyncValueCommand

Ermöglicht die sichere Verwendung des ValueTask Typs mit einem ICommand.

Weitere Informationen zum Typ finden Sie unter Grundlegendes zu den ValueTaskGründen, Whats und Wann von ValueTask .

Syntax

AsyncValueCommand<TExecute, TCanExecute> : IAsyncValueCommand<TExecute, TCanExecute>

public AsyncValueCommand(
    Func<TExecute, ValueTask> execute,
    Func<TCanExecute, bool> canExecute = null,
    Action<Exception> onException = null,
    bool continueOnCapturedContext = false,
    bool allowsMultipleExecutions = true)

AsyncValueCommand<T> : IAsyncValueCommand<T>

public AsyncValueCommand(
    Func<T, ValueTask> execute,
    Func<object, bool> canExecute = null,
    Action<Exception> onException = null,
    bool continueOnCapturedContext = false,
    bool allowsMultipleExecutions = true)
public AsyncValueCommand(
    Func<T, ValueTask> execute,
    Func<bool> canExecute,
    Action<Exception> onException = null,
    bool continueOnCapturedContext = false,
    bool allowsMultipleExecutions = true)
    : this(execute, _ => canExecute(), onException, continueOnCapturedContext, allowsMultipleExecutions)

AsyncValueCommand : IAsyncValueCommand

public AsyncValueCommand(
    Func<Task> execute,
    Func<object, bool> canExecute = null,
    Action<Exception> onException = null,
    bool continueOnCapturedContext = false,
    bool allowsMultipleExecutions = true)
public AsyncValueCommand(
    Func<Task> execute,
    Func<bool> canExecute,
    Action<Exception> onException = null,
    bool continueOnCapturedContext = false,
    bool allowsMultipleExecutions = true)
    : this(execute, _ => canExecute(), onException, continueOnCapturedContext, allowsMultipleExecutions)

IAsyncValueCommand<TExecute, TCanExecute>

interface IAsyncValueCommand<TExecute, TCanExecute> : IAsyncValueCommand<TExecute>

IAsyncValueCommand<T>

interface IAsyncValueCommand<T> : ICommand

IAsyncValueCommand

interface IAsyncValueCommand : ICommand

Methoden

Methoden Rückgabetyp BESCHREIBUNG
ExecuteAsync() ValueTask Führt den Befehl als ValueTask aus.
ExecuteAsync(T) ValueTask Führt den Befehl als ValueTask aus.
ExecuteAsync(TExecute) ValueTask Führt den Befehl als ValueTask aus.

Beispiele

AsyncValueCommand

class MyViewModel
{
    bool _isBusy;

    public MyViewModel()
    {
        ButtonCommand = new AsyncValueCommand(() => ExecuteButtonCommand(), _ => !IsBusy);
    }

    public IAsyncValueCommand ButtonCommand { get; }

    public bool IsBusy
    {
        get => _isBusy;
        set
        {
            if(_isBusy != value)
            {
                _isBusy = value;
                ButtonCommand.RaiseCanExecuteChanged();
            }
        }
    }    

    async ValueTask ExecuteButtonCommand()
    {
        // ...
    }
}

AsyncValueCommand<T>

class MyViewModel
{
    bool _isBusy;

    public MyViewModel()
    {
        ButtonCommand = new AsyncValueCommand<int>(buttonClicks => ExecuteButtonCommand(buttonClicks), _ => !IsBusy);
    }

    public IAsyncValueCommand<int> ButtonCommand { get; }

    public bool IsBusy
    {
        get => _isBusy;
        set
        {
            if(_isBusy != value)
            {
                _isBusy = value;
                ButtonCommand.RaiseCanExecuteChanged();
            }
        }
    }   
    

    async ValueTask ExecuteButtonCommand(int buttonClicks)
    {
        // ...
    }
}

AsyncValueCommand<TExecute, TCanExecute>

class MyViewModel
{
    public MyViewModel()
    {
        ButtonCommand = new AsyncValueCommand<int, bool>(buttonClicks => ExecuteButtonCommand(buttonClicks), isBusy => !isBusy);
    }

    public IAsyncValueCommand<int, bool> ButtonCommand { get; } 
    

    async ValueTask ExecuteButtonCommand(int buttonClicks)
    {
        // ...
    }
}

Beispielprojekt

Sie können dieses Element in Aktion in der Xamarin-Community-Toolkit-Beispiel-App sehen.

API