ReactiveCommand

ReactiveCommand is an ICommand implementation that is simultaneously a simple ICommand implementation and some extra bits that are pretty motivating. First, we can treat this as a non-Rx enabled ICommand, using the Create static method, as follows:

var cmd = ReactiveCommand.Create(x => true, x => Console.WriteLine(x)); cmd.CanExecute(null); >> true cmd.Execute("Hello"); >> "Hello"

Here's where it gets interesting—we can also provide IObservable as our CanExecute. For example, the following is a command that can only run when the mouse is up:

var mouseIsUp = Observable.Merge(     Observable.FromEvent<MouseButtonEventArgs>(window, "MouseDown").Select(_ => false),     Observable.FromEvent<MouseButtonEventArgs>(window, "MouseUp").Select(_ ...

Get Programming Reactive Extensions and LINQ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.