Table of Contents

Class ReactiveCommand

Namespace
ReactiveUI
Assembly
ReactiveUI.dll

Encapsulates a user action behind a reactive interface.

public static class ReactiveCommand : Object
Inheritance
ReactiveCommand

Remarks

This non-generic base class defines the creation behavior of the ReactiveCommand's.

ReactiveCommand<TParam, TResult> adds the concept of Input and Output generic types. The Input is often passed in by the View and it's type is captured as TInput, and the Output is the result of executing the command which type is captured as TOutput.

ReactiveCommand<TParam, TResult> is IObservable which can be used like any other IObservable. For example, you can Subscribe() to it like any other observable, and add the output to a List on your view model. The Unit type is a functional programming construct analogous to void and can be used in cases where you don't care about either the input and/or output value.

Creating synchronous reactive commands:

// A synchronous command taking a parameter and returning nothing.
ReactiveCommand<int, Unit> command = ReactiveCommand.Create<int>(x => Console.WriteLine(x));

// This outputs 42 to console. command.Execute(42).Subscribe();

// A better approach is to invoke a command in response to an Observable<T>. // InvokeCommand operator respects the command's executability. That is, if // the command's CanExecute method returns false, InvokeCommand will not // execute the command when the source observable ticks. Observable.Return(42).InvokeCommand(command);

Creating asynchronous reactive commands:

// An asynchronous command that waits 2 seconds and returns 42.
var command = ReactiveCommand.CreateFromObservable<Unit, int>(
     _ => Observable.Return(42).Delay(TimeSpan.FromSeconds(2))
);

// Calling the asynchronous reactive command: // Observable.Return(Unit.Default).InvokeCommand(command);

// Subscribing to values emitted by the command: command.Subscribe(Console.WriteLine);

Methods

Create(Action, IObservable<bool>?, IScheduler?)

Creates a parameterless ReactiveCommand<TParam, TResult> with synchronous execution logic.

public static ReactiveCommand<Unit, Unit> Create(Action execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Action

The action to execute whenever the command is executed.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<Unit, Unit>

The ReactiveCommand instance.

Exceptions

ArgumentNullException

execute.

CreateCombined<TParam, TResult>(IEnumerable<ReactiveCommandBase<TParam, TResult>>, IObservable<bool>?, IScheduler?)

Creates a CombinedReactiveCommand<TParam, TResult> that composes all the provided child commands.

public static CombinedReactiveCommand<TParam, TResult> CreateCombined<TParam, TResult>(IEnumerable<ReactiveCommandBase<TParam, TResult>> childCommands, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

childCommands IEnumerable<ReactiveCommandBase<TParam, TResult>>

The child commands that the combined command will compose.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution (in addition to the availability specified by each individual child command).

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

CombinedReactiveCommand<TParam, TResult>

The CombinedReactiveCommand instance.

Type Parameters

TParam

The type of the parameter passed through to command execution.

TResult

The type of the command's result.

CreateFromObservable<TResult>(Func<IObservable<TResult>>, IObservable<bool>?, IScheduler?)

Creates a parameterless ReactiveCommand<TParam, TResult> with asynchronous execution logic.

public static ReactiveCommand<Unit, TResult> CreateFromObservable<TResult>(Func<IObservable<TResult>> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Func<IObservable<TResult>>

Provides an observable representing the command's asynchronous execution logic.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<Unit, TResult>

The ReactiveCommand instance.

Type Parameters

TResult

The type of the command's result.

CreateFromObservable<TParam, TResult>(Func<TParam, IObservable<TResult>>, IObservable<bool>?, IScheduler?)

Creates a ReactiveCommand<TParam, TResult> with asynchronous execution logic that takes a parameter of type TParam.

public static ReactiveCommand<TParam, TResult> CreateFromObservable<TParam, TResult>(Func<TParam, IObservable<TResult>> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Func<TParam, IObservable<TResult>>

Provides an observable representing the command's asynchronous execution logic.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<TParam, TResult>

The ReactiveCommand instance.

Type Parameters

TParam

The type of the parameter passed through to command execution.

TResult

The type of the command's result.

CreateFromTask(Func<CancellationToken, Task>, IObservable<bool>?, IScheduler?)

Creates a parameterless, cancellable ReactiveCommand<TParam, TResult> with asynchronous execution logic.

public static ReactiveCommand<Unit, Unit> CreateFromTask(Func<CancellationToken, Task> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Func<CancellationToken, Task>

Provides a Task representing the command's asynchronous execution logic.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<Unit, Unit>

The ReactiveCommand instance.

CreateFromTask(Func<Task>, IObservable<bool>?, IScheduler?)

Creates a parameterless ReactiveCommand<TParam, TResult> with asynchronous execution logic.

public static ReactiveCommand<Unit, Unit> CreateFromTask(Func<Task> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Func<Task>

Provides a Task representing the command's asynchronous execution logic.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<Unit, Unit>

The ReactiveCommand instance.

CreateFromTask<TResult>(Func<CancellationToken, Task<TResult>>, IObservable<bool>?, IScheduler?)

Creates a parameterless, cancellable ReactiveCommand<TParam, TResult> with asynchronous execution logic.

public static ReactiveCommand<Unit, TResult> CreateFromTask<TResult>(Func<CancellationToken, Task<TResult>> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Func<CancellationToken, Task<TResult>>

Provides a Task representing the command's asynchronous execution logic.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<Unit, TResult>

The ReactiveCommand instance.

Type Parameters

TResult

The type of the command's result.

CreateFromTask<TResult>(Func<Task<TResult>>, IObservable<bool>?, IScheduler?)

Creates a parameterless ReactiveCommand<TParam, TResult> with asynchronous execution logic.

public static ReactiveCommand<Unit, TResult> CreateFromTask<TResult>(Func<Task<TResult>> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Func<Task<TResult>>

Provides a Task representing the command's asynchronous execution logic.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<Unit, TResult>

The ReactiveCommand instance.

Type Parameters

TResult

The type of the command's result.

CreateFromTask<TParam>(Func<TParam, CancellationToken, Task>, IObservable<bool>?, IScheduler?)

Creates a ReactiveCommand<TParam, TResult> with asynchronous, cancellable execution logic that takes a parameter of type TParam.

public static ReactiveCommand<TParam, Unit> CreateFromTask<TParam>(Func<TParam, CancellationToken, Task> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Func<TParam, CancellationToken, Task>

Provides a Task representing the command's asynchronous execution logic.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<TParam, Unit>

The ReactiveCommand instance.

Type Parameters

TParam

The type of the parameter passed through to command execution.

CreateFromTask<TParam>(Func<TParam, Task>, IObservable<bool>?, IScheduler?)

Creates a ReactiveCommand<TParam, TResult> with asynchronous execution logic that takes a parameter of type TParam.

public static ReactiveCommand<TParam, Unit> CreateFromTask<TParam>(Func<TParam, Task> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Func<TParam, Task>

Provides a Task representing the command's asynchronous execution logic.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<TParam, Unit>

The ReactiveCommand instance.

Type Parameters

TParam

The type of the parameter passed through to command execution.

CreateFromTask<TParam, TResult>(Func<TParam, CancellationToken, Task<TResult>>, IObservable<bool>?, IScheduler?)

Creates a ReactiveCommand<TParam, TResult> with asynchronous, cancellable execution logic that takes a parameter of type TParam.

public static ReactiveCommand<TParam, TResult> CreateFromTask<TParam, TResult>(Func<TParam, CancellationToken, Task<TResult>> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Func<TParam, CancellationToken, Task<TResult>>

Provides a Task representing the command's asynchronous execution logic.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<TParam, TResult>

The ReactiveCommand instance.

Type Parameters

TParam

The type of the parameter passed through to command execution.

TResult

The type of the command's result.

CreateFromTask<TParam, TResult>(Func<TParam, Task<TResult>>, IObservable<bool>?, IScheduler?)

Creates a ReactiveCommand<TParam, TResult> with asynchronous execution logic that takes a parameter of type TParam.

public static ReactiveCommand<TParam, TResult> CreateFromTask<TParam, TResult>(Func<TParam, Task<TResult>> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Func<TParam, Task<TResult>>

Provides a Task representing the command's asynchronous execution logic.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<TParam, TResult>

The ReactiveCommand instance.

Type Parameters

TParam

The type of the parameter passed through to command execution.

TResult

The type of the command's result.

CreateRunInBackground(Action, IObservable<bool>?, IScheduler?, IScheduler?)

Creates a parameterless ReactiveCommand<TParam, TResult> with asynchronous execution logic.

public static ReactiveCommand<Unit, Unit> CreateRunInBackground(Action execute, IObservable<bool>? canExecute = null, IScheduler? backgroundScheduler = null, IScheduler? outputScheduler = null)

Parameters

execute Action

The action to execute whenever the command is executed.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

backgroundScheduler IScheduler

The background scheduler.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<Unit, Unit>

The ReactiveCommand instance.

Exceptions

ArgumentNullException

execute.

CreateRunInBackground<TParam>(Action<TParam>, IObservable<bool>?, IScheduler?, IScheduler?)

Creates a ReactiveCommand<TParam, TResult> with asynchronous execution logic that takes a parameter of type TParam.

public static ReactiveCommand<TParam, Unit> CreateRunInBackground<TParam>(Action<TParam> execute, IObservable<bool>? canExecute = null, IScheduler? backgroundScheduler = null, IScheduler? outputScheduler = null)

Parameters

execute Action<TParam>

The action to execute whenever the command is executed.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

backgroundScheduler IScheduler

The background scheduler.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<TParam, Unit>

The ReactiveCommand instance.

Type Parameters

TParam

The type of the parameter passed through to command execution.

Exceptions

ArgumentNullException

execute.

CreateRunInBackground<TResult>(Func<TResult>, IObservable<bool>?, IScheduler?, IScheduler?)

Creates a parameterless ReactiveCommand<TParam, TResult> with asynchronous execution logic that returns a value of type TResult.

public static ReactiveCommand<Unit, TResult> CreateRunInBackground<TResult>(Func<TResult> execute, IObservable<bool>? canExecute = null, IScheduler? backgroundScheduler = null, IScheduler? outputScheduler = null)

Parameters

execute Func<TResult>

The function to execute whenever the command is executed.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

backgroundScheduler IScheduler

The background scheduler.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<Unit, TResult>

The ReactiveCommand instance.

Type Parameters

TResult

The type of value returned by command executions.

Exceptions

ArgumentNullException

execute.

CreateRunInBackground<TParam, TResult>(Func<TParam, TResult>, IObservable<bool>?, IScheduler?, IScheduler?)

Creates a ReactiveCommand<TParam, TResult> with asynchronous execution logic that takes a parameter of type TParam and returns a value of type TResult.

public static ReactiveCommand<TParam, TResult> CreateRunInBackground<TParam, TResult>(Func<TParam, TResult> execute, IObservable<bool>? canExecute = null, IScheduler? backgroundScheduler = null, IScheduler? outputScheduler = null)

Parameters

execute Func<TParam, TResult>

The function to execute whenever the command is executed.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

backgroundScheduler IScheduler

The background scheduler.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<TParam, TResult>

The ReactiveCommand instance.

Type Parameters

TParam

The type of the parameter passed through to command execution.

TResult

The type of value returned by command executions.

Exceptions

ArgumentNullException

execute.

Create<TParam>(Action<TParam>, IObservable<bool>?, IScheduler?)

Creates a ReactiveCommand<TParam, TResult> with synchronous execution logic that takes a parameter of type TParam.

public static ReactiveCommand<TParam, Unit> Create<TParam>(Action<TParam> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Action<TParam>

The action to execute whenever the command is executed.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<TParam, Unit>

The ReactiveCommand instance.

Type Parameters

TParam

The type of the parameter passed through to command execution.

Exceptions

ArgumentNullException

execute.

Create<TResult>(Func<TResult>, IObservable<bool>?, IScheduler?)

Creates a parameterless ReactiveCommand<TParam, TResult> with synchronous execution logic that returns a value of type TResult.

public static ReactiveCommand<Unit, TResult> Create<TResult>(Func<TResult> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Func<TResult>

The function to execute whenever the command is executed.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<Unit, TResult>

The ReactiveCommand instance.

Type Parameters

TResult

The type of value returned by command executions.

Exceptions

ArgumentNullException

execute.

Create<TParam, TResult>(Func<TParam, TResult>, IObservable<bool>?, IScheduler?)

Creates a ReactiveCommand<TParam, TResult> with synchronous execution logic that takes a parameter of type TParam and returns a value of type TResult.

public static ReactiveCommand<TParam, TResult> Create<TParam, TResult>(Func<TParam, TResult> execute, IObservable<bool>? canExecute = null, IScheduler? outputScheduler = null)

Parameters

execute Func<TParam, TResult>

The function to execute whenever the command is executed.

canExecute IObservable<bool>

An optional observable that dictates the availability of the command for execution.

outputScheduler IScheduler

An optional scheduler that is used to surface events. Defaults to RxApp.MainThreadScheduler.

Returns

ReactiveCommand<TParam, TResult>

The ReactiveCommand instance.

Type Parameters

TParam

The type of the parameter passed through to command execution.

TResult

The type of value returned by command executions.

Exceptions

ArgumentNullException

execute.