Summary
- Namespace
- ReactiveUI
- Base Types
-
- object
Syntax
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.
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
Extension Methods
Name | Value | Summary |
---|---|---|
Invoke |
object |
This is a thing I lifted from Prism.
|