0

How do I bind to the latest version of the ReactiveCommand from my XAML page?

In ReactiveUI (6.5), The following command property declaration is no longer supported:

public ReactiveCommand ClickMe { get; private set; }

As a result, can a button declared within XAML still invoke a command on a view-model using the "Button.Command" property?

I tried the following:

public class InstructionsViewModel : ReactiveObject
{
    public InstructionsViewModel()
    {
         Accept = ReactiveCommand.Create(x => Debug.WriteLine("Hello World"));
    }

    public ReactiveCommand<object> Accept { get; }
}

Cannot convert lambda expression to type 'IObservable' because it is not a delegate type

Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118

2 Answers2

1

In ReactiveUI 6.5, ReactiveCommand is now generic: ReactiveCommand<T> (so you need to update the type of your property accordingly, probably ReactiveCommand<object> or ReactiveCommand<Unit>, as the non-generic ReactiveCommand is now a static helper class with factory methods)

But it still implements ICommand and you can still bind it in your XAML.

Gluck
  • 2,933
  • 16
  • 28
  • What error/behavior are you having with your new code ? You're missing a private setter for the property, but other than that the binding should work (it won't support dynamic updates to the command property though, but it doesn't seem you need it) – Gluck Dec 16 '15 at 12:20
  • Cannot convert lambda expression to type 'IObservable' because it is not a delegate type – Scott Nimrod Dec 16 '15 at 12:24
  • You should check the [params](https://github.com/reactiveui/ReactiveUI/blob/master/ReactiveUI/ReactiveCommand.cs) to the helper methods, none match the lambda you gave. Try `CreateAsync(async _ => Debug.WriteLine("Hello World"))`, samples are [there](http://reactiveui.readthedocs.org/en/latest/commands/reactive-command-async/). – Gluck Dec 16 '15 at 12:33
  • I'm starting to think that this isn't supported for a PCL targeting UWP (i.e. Windows Phone 10). – Scott Nimrod Dec 17 '15 at 10:33
1

ReactiveCommand.Create method will create the reactive Command for you. the parameter you pass to the create method is IObservable that tells if the command can be executed. if you want to react to that command you have to subscribe.

Accept = ReactiveCommand.Create();
Accept.Subscribe(_ => Debug.WriteLine("Hello World"));

ReactiveCommand implements ICommand so you can bind it in the XAML. The CommandParameter value will be sent to your Subscribe method.

Rajan
  • 121
  • 7