O'Reilly logo

Programming Reactive Extensions and LINQ by Paul Betts, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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(_ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required