Table of Contents

Class ReactiveCommand<TParam, TResult>

Namespace
ReactiveUI
Assembly
ReactiveUI.dll

Encapsulates a user interaction behind a reactive interface.

public class ReactiveCommand<TParam, TResult> : ReactiveCommandBase<TParam, TResult>, IReactiveCommand<TParam, TResult>, IObservable<TResult>, IReactiveCommand, IDisposable, IHandleObservableErrors, ICommand

Type Parameters

TParam

The type of parameter values passed in during command execution.

TResult

The type of the values that are the result of command execution.

Inheritance
ReactiveCommandBase<TParam, TResult>
ReactiveCommand<TParam, TResult>
Implements
IReactiveCommand<TParam, TResult>
IObservable<TResult>
Inherited Members
Extension Methods
WhenAnyMixin.WhenAny<TSender, TRet, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(TSender?, Expression<Func<TSender, T1>>, Expression<Func<TSender, T2>>, Expression<Func<TSender, T3>>, Expression<Func<TSender, T4>>, Expression<Func<TSender, T5>>, Expression<Func<TSender, T6>>, Expression<Func<TSender, T7>>, Expression<Func<TSender, T8>>, Expression<Func<TSender, T9>>, Expression<Func<TSender, T10>>, Expression<Func<TSender, T11>>, Expression<Func<TSender, T12>>, Func<IObservedChange<TSender, T1>, IObservedChange<TSender, T2>, IObservedChange<TSender, T3>, IObservedChange<TSender, T4>, IObservedChange<TSender, T5>, IObservedChange<TSender, T6>, IObservedChange<TSender, T7>, IObservedChange<TSender, T8>, IObservedChange<TSender, T9>, IObservedChange<TSender, T10>, IObservedChange<TSender, T11>, IObservedChange<TSender, T12>, TRet>)
WhenAnyMixin.WhenAny<TSender, TRet, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(TSender?, Expression<Func<TSender, T1>>, Expression<Func<TSender, T2>>, Expression<Func<TSender, T3>>, Expression<Func<TSender, T4>>, Expression<Func<TSender, T5>>, Expression<Func<TSender, T6>>, Expression<Func<TSender, T7>>, Expression<Func<TSender, T8>>, Expression<Func<TSender, T9>>, Expression<Func<TSender, T10>>, Expression<Func<TSender, T11>>, Expression<Func<TSender, T12>>, Func<IObservedChange<TSender, T1>, IObservedChange<TSender, T2>, IObservedChange<TSender, T3>, IObservedChange<TSender, T4>, IObservedChange<TSender, T5>, IObservedChange<TSender, T6>, IObservedChange<TSender, T7>, IObservedChange<TSender, T8>, IObservedChange<TSender, T9>, IObservedChange<TSender, T10>, IObservedChange<TSender, T11>, IObservedChange<TSender, T12>, TRet>)
WhenAnyMixin.WhenAny<TSender, TRet, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(TSender?, Expression<Func<TSender, T1>>, Expression<Func<TSender, T2>>, Expression<Func<TSender, T3>>, Expression<Func<TSender, T4>>, Expression<Func<TSender, T5>>, Expression<Func<TSender, T6>>, Expression<Func<TSender, T7>>, Expression<Func<TSender, T8>>, Expression<Func<TSender, T9>>, Expression<Func<TSender, T10>>, Expression<Func<TSender, T11>>, Expression<Func<TSender, T12>>, Func<IObservedChange<TSender, T1>, IObservedChange<TSender, T2>, IObservedChange<TSender, T3>, IObservedChange<TSender, T4>, IObservedChange<TSender, T5>, IObservedChange<TSender, T6>, IObservedChange<TSender, T7>, IObservedChange<TSender, T8>, IObservedChange<TSender, T9>, IObservedChange<TSender, T10>, IObservedChange<TSender, T11>, IObservedChange<TSender, T12>, TRet>)
WhenAnyMixin.WhenAny<TSender, TRet, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(TSender?, Expression<Func<TSender, T1>>, Expression<Func<TSender, T2>>, Expression<Func<TSender, T3>>, Expression<Func<TSender, T4>>, Expression<Func<TSender, T5>>, Expression<Func<TSender, T6>>, Expression<Func<TSender, T7>>, Expression<Func<TSender, T8>>, Expression<Func<TSender, T9>>, Expression<Func<TSender, T10>>, Expression<Func<TSender, T11>>, Expression<Func<TSender, T12>>, Func<IObservedChange<TSender, T1>, IObservedChange<TSender, T2>, IObservedChange<TSender, T3>, IObservedChange<TSender, T4>, IObservedChange<TSender, T5>, IObservedChange<TSender, T6>, IObservedChange<TSender, T7>, IObservedChange<TSender, T8>, IObservedChange<TSender, T9>, IObservedChange<TSender, T10>, IObservedChange<TSender, T11>, IObservedChange<TSender, T12>, TRet>)

Remarks

This class provides the bulk of the actual implementation for reactive commands. You should not create instances of this class directly, but rather via the static creation methods on the non-generic ReactiveCommand class.

Constructors

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

Initializes a new instance of the ReactiveCommand<TParam, TResult> class for work that signals cancellation through a separate callback (as opposed to cancelling by unsubscribing).

protected ReactiveCommand(Func<TParam, IObservable<(IObservable<TResult>, Action)>> execute, IObservable<bool>? canExecute, IScheduler? outputScheduler)

Parameters

execute Func<TParam, IObservable<(IObservable<TResult>, Action)>>

The Func to perform when the command is executed.

canExecute IObservable<bool>

A observable which has a value if the command can execute.

outputScheduler IScheduler

The scheduler where to send output after the main execution.

Exceptions

ArgumentNullException

execute.

ArgumentNullException

Thrown if any dependent parameters are null.

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

Initializes a new instance of the ReactiveCommand<TParam, TResult> class.

protected ReactiveCommand(Func<TParam, IObservable<TResult>> execute, IObservable<bool>? canExecute, IScheduler? outputScheduler)

Parameters

execute Func<TParam, IObservable<TResult>>

The Func to perform when the command is executed.

canExecute IObservable<bool>

A observable which has a value if the command can execute.

outputScheduler IScheduler

The scheduler where to send output after the main execution.

Exceptions

ArgumentNullException

execute.

ArgumentNullException

Thrown if any dependent parameters are null.

Properties

CanExecute

Gets an observable whose value indicates whether the command can currently execute.

public override IObservable<bool> CanExecute { get; }

Property Value

IObservable<bool>

The can execute.

Remarks

The value provided by this observable is governed both by any canExecute observable provided during command creation, as well as the current execution status of the command. A command that is currently executing will always yield false from this observable, even if the canExecute pipeline is currently true.

IsExecuting

Gets an observable whose value indicates whether the command is currently executing.

public override IObservable<bool> IsExecuting { get; }

Property Value

IObservable<bool>

The is executing.

Remarks

This observable can be particularly useful for updating UI, such as showing an activity indicator whilst a command is executing.

ThrownExceptions

Gets a observable which will fire whenever an exception would normally terminate ReactiveUI internal state.

public override IObservable<Exception> ThrownExceptions { get; }

Property Value

IObservable<Exception>

Methods

Dispose(bool)

Disposes of the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

If its getting called by the Dispose() method.

Execute()

Gets an observable that, when subscribed, executes this command.

public override IObservable<TResult> Execute()

Returns

IObservable<TResult>

An observable that will tick the single result value if and when it becomes available.

Remarks

Invoking this method will return a cold (lazy) observable that, when subscribed, will execute the logic encapsulated by the command. It is worth restating that the returned observable is lazy. Nothing will happen if you call Execute and neglect to subscribe (directly or indirectly) to the returned observable.

If no parameter value is provided, a default value of type TParam will be passed into the execution logic.

Any number of subscribers can subscribe to a given execution observable and the execution logic will only run once. That is, the result is broadcast to those subscribers.

In those cases where execution fails, there will be no result value. Instead, the failure will tick through the ThrownExceptions observable.

Execute(TParam)

Gets an observable that, when subscribed, executes this command.

public override IObservable<TResult> Execute(TParam parameter)

Parameters

parameter TParam

The parameter to pass into command execution.

Returns

IObservable<TResult>

An observable that will tick the single result value if and when it becomes available.

Remarks

Invoking this method will return a cold (lazy) observable that, when subscribed, will execute the logic encapsulated by the command. It is worth restating that the returned observable is lazy. Nothing will happen if you call Execute and neglect to subscribe (directly or indirectly) to the returned observable.

If no parameter value is provided, a default value of type TParam will be passed into the execution logic.

Any number of subscribers can subscribe to a given execution observable and the execution logic will only run once. That is, the result is broadcast to those subscribers.

In those cases where execution fails, there will be no result value. Instead, the failure will tick through the ThrownExceptions observable.

Subscribe(IObserver<TResult>)

Subscribes to execution results from this command.

public override IDisposable Subscribe(IObserver<TResult> observer)

Parameters

observer IObserver<TResult>

The observer.

Returns

IDisposable

An IDisposable that, when disposed, will unsubscribe the observer.