1

I would like to observe a property path: MainViewModel.Project.SelectedDocument.Align

  1. Align is regular property of type ReactiveCommand<Unit, Unit>.
  2. Project and SelectedDocument are regular properties.

I'm using this to create the observable (from MainViewModel):

var commandObs = this
    .WhenAnyObservable(x => x.Project.SelectedDocument.WhenAnyValue(y => y.Align));

I'm getting an exception on this line with the following message:

System.NotSupportedException: 'Index expressions are only supported with constants.'

What's wrong?

Since the WhenAnyObservable method requires an observable property at the end of the property path, I'm creating it with the inner WhenAnyValue. Is that the problem? Should the property expression a simple access expression instead of a method call?

In any case, I took the code from this answer: ReactiveUI How to use WhenAnyObservable properly

It supposedly works :) but not for me in this case.

SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • It would be awesome if you could provide a [mcve]. – mjwills Nov 10 '19 at 12:05
  • This could be an issue/protecetion from an issue with Lambda Variable Capture: https://blogs.msdn.microsoft.com/matt/2008/03/01/understanding-variable-capturing-in-c/ | Similar to how we added the CrossThreadException back in .NET 2.0. So people would finally use invoke (and avoid the issues it is there to solve): https://stackoverflow.com/a/14703806/3346583 – Christopher Nov 10 '19 at 12:15

1 Answers1

3

Expressions in the WhenAny must point towards a property or field. That's why reactiveui is throwing the exception. You would need to expose an IObservable property or you could use a Select and Switch statements.

Eg

var commandObs = this
    .WhenAnyValue(x => x.Project.SelectedDocument)
    .Select(x => x.WhenAnyValue(y => y.Align))
    .Switch()
John Cummings
  • 1,949
  • 3
  • 22
  • 38
Glenn Watson
  • 2,758
  • 1
  • 20
  • 30