Skip to content

ReactiveCommand class

Defined in

Namespace: ReactiveUI.Reactive Assembly: ReactiveUI.Reactive.dll Full name: ReactiveUI.Reactive.ReactiveCommand Modifiers: public static

Summary

View source

Encapsulates a user action behind a reactive interface.

Applies to

net10.0, net10.0-maccatalyst26.0, net10.0-desktop1.0, net10.0-browserwasm1.0, net10.0-tvos26.0, net10.0-windows10.0.19041, net10.0-macos26.0, net10.0-ios26.0, net10.0-android36.0, net9.0, net9.0-tvos18.0, net9.0-macos15.0, net9.0-maccatalyst18.0, net9.0-windows10.0.19041, net9.0-ios18.0, net9.0-desktop1.0, net8.0, net8.0-windows10.0.19041, net8.0-ios18.0, net8.0-maccatalyst18.0, net8.0-macos15.0, net8.0-tvos18.0, net8.0-ios17.5, net8.0-maccatalyst17.5, net8.0-macos14.5, netstandard2.1, net481, net462, net471

Remarks

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

ReactiveCommand 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 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:

<![CDATA[
 // 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:

<![CDATA[
 // 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

NameSummary
static CreateCreates a parameterless reactive command with synchronous execution logic.
static CreateRunInBackgroundCreates a parameterless reactive command with asynchronous background execution logic.
static CreateCombinedCreates a combined reactive command that composes all the provided child commands.
static CreateFromObservableCreates a parameterless reactive command with asynchronous observable execution logic.
static CreateFromTaskCreates a parameterless reactive command with asynchronous task-based execution logic returning TResult.
Inherited members