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
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> Result, Action Cancel)>>, 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> Result, Action Cancel)>> execute, IObservable<bool>? canExecute, IScheduler? outputScheduler)
Parameters
execute
Func<TParam, IObservable<(IObservable<TResult> Result, Action Cancel)>>The Func to perform when the command is executed.
canExecute
IObservable<bool>A observable which has a value if the command can execute.
outputScheduler
ISchedulerThe 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
ISchedulerThe 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
Methods
Dispose(bool)
Disposes of the managed resources.
protected override void Dispose(bool disposing)
Parameters
disposing
boolIf 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
TParamThe 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.