ReactiveCommand Class

Summary

Encapsulates a user action behind a reactive interface.
Namespace
ReactiveUI
Base Types
  • object
graph BT Type-->Base0["object"] Type["ReactiveCommand"] class Type type-node

Syntax

[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleType", Justification = "Same class just generic.")]
public static class 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.
 var command = ReactiveCommand.Create<int, Unit>(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);

Attributes

Type Description
SuppressMessageAttribute

Methods

Name Value Summary
Create(Action, IObservable<bool>, IScheduler) ReactiveCommand<Unit, Unit>
Creates a parameterless ReactiveCommand<TParam, TResult> with synchronous execution logic.
static
Create<TParam, TResult>(Func<TParam, TResult>, IObservable<bool>, IScheduler) ReactiveCommand<TParam, TResult>
Creates a ReactiveCommand<TParam, TResult> with synchronous execution logic that takes a parameter of type TParam and returns a value of type TResult.
static
Create<TParam>(Action<TParam>, IObservable<bool>, IScheduler) ReactiveCommand<TParam, Unit>
Creates a ReactiveCommand<TParam, TResult> with synchronous execution logic that takes a parameter of type TParam.
static
Create<TResult>(Func<TResult>, IObservable<bool>, IScheduler) ReactiveCommand<Unit, TResult>
Creates a parameterless ReactiveCommand<TParam, TResult> with synchronous execution logic that returns a value of type TResult.
static
CreateCombined<TParam, TResult>(IEnumerable<ReactiveCommandBase<TParam, TResult>>, IObservable<bool>, IScheduler) CombinedReactiveCommand<TParam, TResult>
Creates a CombinedReactiveCommand<TParam, TResult> that composes all the provided child commands.
static
CreateFromObservable<TParam, TResult>(Func<TParam, IObservable<TResult>>, IObservable<bool>, IScheduler) ReactiveCommand<TParam, TResult>
Creates a ReactiveCommand<TParam, TResult> with asynchronous execution logic that takes a parameter of type TParam.
static
CreateFromObservable<TResult>(Func<IObservable<TResult>>, IObservable<bool>, IScheduler) ReactiveCommand<Unit, TResult>
Creates a parameterless ReactiveCommand<TParam, TResult> with asynchronous execution logic.
static
CreateFromTask(Func<CancellationToken, Task>, IObservable<bool>, IScheduler) ReactiveCommand<Unit, Unit>
Creates a parameterless, cancellable ReactiveCommand<TParam, TResult> with asynchronous execution logic.
static
CreateFromTask(Func<Task>, IObservable<bool>, IScheduler) ReactiveCommand<Unit, Unit>
Creates a parameterless ReactiveCommand<TParam, TResult> with asynchronous execution logic.
static
CreateFromTask<TParam, TResult>(Func<TParam, CancellationToken, Task<TResult>>, IObservable<bool>, IScheduler) ReactiveCommand<TParam, TResult>
Creates a ReactiveCommand<TParam, TResult> with asynchronous, cancellable execution logic that takes a parameter of type TParam.
static
CreateFromTask<TParam, TResult>(Func<TParam, Task<TResult>>, IObservable<bool>, IScheduler) ReactiveCommand<TParam, TResult>
Creates a ReactiveCommand<TParam, TResult> with asynchronous execution logic that takes a parameter of type TParam.
static
CreateFromTask<TParam>(Func<TParam, CancellationToken, Task>, IObservable<bool>, IScheduler) ReactiveCommand<TParam, Unit>
Creates a ReactiveCommand<TParam, TResult> with asynchronous, cancellable execution logic that takes a parameter of type TParam.
static
CreateFromTask<TParam>(Func<TParam, Task>, IObservable<bool>, IScheduler) ReactiveCommand<TParam, Unit>
Creates a ReactiveCommand<TParam, TResult> with asynchronous execution logic that takes a parameter of type TParam.
static
CreateFromTask<TResult>(Func<CancellationToken, Task<TResult>>, IObservable<bool>, IScheduler) ReactiveCommand<Unit, TResult>
Creates a parameterless, cancellable ReactiveCommand<TParam, TResult> with asynchronous execution logic.
static
CreateFromTask<TResult>(Func<Task<TResult>>, IObservable<bool>, IScheduler) ReactiveCommand<Unit, TResult>
Creates a parameterless ReactiveCommand<TParam, TResult> with asynchronous execution logic.
static