Xamarin Community Toolkit CommandFactory

The CommandFactory class provides a unified approach to creating new Command, AsyncCommand, and AsyncValueCommand objects.

Syntax

public static CommandFactory.Create()

Examples

To consume the CommandFactory class, replace new Command, new AsyncCommand and new AsyncValueCommand with the CommandFactory.Create method:

Command command = CommandFactory.Create(() => Debug.WriteLine("Command executed"));
Command<string> commandWithParameter = CommandFactory.Create<string>(p => Debug.WriteLine("Command executed: {0}", p));

IAsyncCommand asyncCommand = CommandFactory.Create(ExecuteCommandAsync)
IAsyncCommand<int> asyncCommandWithParameter = CommandFactory.Create<int>(ExecuteCommandAsync)

async Task ExecuteCommandAsync()
{
    // ...
}

async Task ExecuteCommandAsync(int commandParameter)
{
    // ...
}

Methods

Methods Return Type Description
Create(Action) Command Initializes Xamarin.Forms.Command.
Create(Action, Func<bool>) Command Initializes Xamarin.Forms.Command.
Create(Action<object>) Command Initializes Xamarin.Forms.Command.
Create(Action<object>, Func<object, bool>) Command Initializes Xamarin.Forms.Command.
Create<T>(Action<T>) Command<T> Initializes Xamarin.Forms.Command<T>.
Create<T>(Action<T>, Func<T, bool>) Command<T> Initializes Xamarin.Forms.Command<T>.
Create(Func<Task> execute, Func<object, bool> canExecute = null, Action<Exception> onException = null, bool continueOnCapturedContext = false, bool allowsMultipleExecutions = true) IAsyncCommand Initializes a new instance of IAsyncCommand.
Create(Func<Task> execute, Func<bool> canExecute, Action<Exception> onException = null, bool continueOnCapturedContext = false, bool allowsMultipleExecutions = true) IAsyncCommand Initializes a new instance of IAsyncCommand.
Create<TExecute>(Func<TExecute, Task> execute, Func<object, bool> canExecute = null, Action<Exception> onException = null, bool continueOnCapturedContext = false, bool allowsMultipleExecutions = true) IAsyncCommand<TExecute> Initializes a new instance of IAsyncCommand<TExecute>.
Create<TExecute>(Func<TExecute, Task> execute, Func<bool> canExecute, Action<Exception> onException = null, bool continueOnCapturedContext = false, bool allowsMultipleExecutions = true) IAsyncCommand<TExecute> Initializes a new instance of IAsyncCommand<TExecute>.
Create<TExecute, TCanExecute>(Func<TExecute, Task> execute, Func<TCanExecute, bool> canExecute = null, Action<Exception> onException = null, bool continueOnCapturedContext = false, bool allowsMultipleExecutions = true) IAsyncCommand<TExecute, TCanExecute> Initializes a new instance of IAsyncCommand<TExecute, TCanExecute>.
Create(Func<ValueTask>, Func<object, bool> canExecute = null, Action<Exception> onException = null, bool continueOnCapturedContext = false, bool allowsMultipleExecutions = true) IAsyncValueCommand Initializes a new instance of IAsyncValueCommand.
Create(Func<ValueTask> execute, Func<bool> canExecute, Action<Exception> onException = null, bool continueOnCapturedContext = false, bool allowsMultipleExecutions = true) IAsyncValueCommand Initializes a new instance of IAsyncValueCommand.
Create<TExecute>(Func<TExecute, ValueTask> execute, Func<object, bool> canExecute = null, Action<Exception> onException = null, bool continueOnCapturedContext = false, bool allowsMultipleExecutions = true) IAsyncValueCommand<TExecute> Initializes a new instance of IAsyncValueCommand<TExecute>.
Create<TExecute>(Func<TExecute, ValueTask> execute, Func<bool> canExecute, Action<Exception> onException = null, bool continueOnCapturedContext = false, bool allowsMultipleExecutions = true) IAsyncValueCommand<TExecute> Initializes a new instance of IAsyncValueCommand<TExecute>.
Create<TExecute, TCanExecute>(Func<TExecute, ValueTask> execute, Func<TCanExecute, bool> canExecute = null, Action<Exception> onException = null, bool continueOnCapturedContext = false, bool allowsMultipleExecutions = true) IAsyncValueCommand<TExecute, TCanExecute> Initializes a new instance of IAsyncValueCommand<TExecute, TCanExecute>.

Sample project

You can see this element in action in the Xamarin community toolkit sample app. In the sample app, every command is created using this approach. For more information, see BasePage Source as an example.

API