0

The following line throws a runtime exception:

Accept = ReactiveCommand.Create(this.WhenAnyValue(x => x.Canexecute()));

Here's the code:

public class InstructionsViewModel : ReactiveObject
{
    public InstructionsViewModel()
    {
        Accept = ReactiveCommand.Create(this.WhenAnyValue(x => x.CanExecute));
        Accept.Subscribe(x =>
          {
             Debug.Write("Hello World");
          });
    }

        public ReactiveCommand<object> Accept { get; }

    bool _canExecute;
    public bool CanExecute { get { return _canExecute; } set { this.RaiseAndSetIfChanged(ref _canExecute, value); } }
}

Error:

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

I've also tried the following:

    public InstructionsViewModel()
    {
        Accept = ReactiveCommand.Create(this.WhenAnyValue(x => x.Canexecute()));
        Accept.Subscribe(x =>
        {
            Debug.Write("Hello World");
        });
    }

    public ReactiveCommand<object> Accept { get; }


    public bool Canexecute() => true;

I receive the following error:

An exception of type 'System.NotSupportedException' occurred in ReactiveUI.dll but was not handled in user code

Additional information: Index expressions are only supported with constants.

Is this even supported on Windows Phone 10?

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

1 Answers1

1

I guess that your problem is not with ReactiveCommand, but with WhenAnyValue.

WhenAnyValue accepts a property, while you feed it with a method, which causes run time exception (see the sourcecode).

Check if this works (I changed CanExecute to be a property instead of a method):

public InstructionsViewModel()
{
    Accept = ReactiveCommand.Create(this.WhenAnyValue(x => x.CanExecute));
    Accept.Subscribe(x =>
    {
        Debug.Write("Hello World");
    });
}

public ReactiveCommand<object> Accept { get; }

private bool _canExecute;
public bool CanExecute { get { return _canExecute; } set { this.RaiseAndSetIfChanged(ref _canExecute, value); } }

Also, as a general advice - do not nest your calls, this makes debugging harder. You should split creating command into two lines:

var canExecute = this.WhenAnyValue(x => x.CanExecute)
Accept = ReactiveCommand.Create(canExecute);
pmbanka
  • 1,902
  • 17
  • 24
  • I tried your suggestion. However, I receive the following error: Cannot convert lambda expression to type 'IObserver' because it is not a delegate type. – Scott Nimrod Dec 17 '15 at 16:26
  • Is this supported on UWP (i.e. Windows Phone 10)? – Scott Nimrod Dec 17 '15 at 16:26
  • I have to admit that I'm puzzled, this should work also on UWP according to the website. Could you edit your question with new version of your code and details about the error? – pmbanka Dec 17 '15 at 16:38
  • 1
    Ah, I see, now the problem is in subscribe call. Try adding 'using System;' on top of your file. See http://stackoverflow.com/q/10521000/1108916 for details. – pmbanka Dec 17 '15 at 17:28