ritcoder
ritcoder

Reputation: 3304

Binding to a non class variable fails in ReactiveUI

Given a sample ContentPage

public class MyPage : ContentPage, IViewFor<MyPageModel> //ReactiveContentPage<MyPageModel>
{
    Entry input;
    //Entry input2;
    Label displayLabel;
    Slider _slider;
    Button _button;
    ListView _listView;
    public MyPage() : base()
    {
        ViewModel = new MyPageModel();

        input = new Entry();
        var input2 = new Entry();
        displayLabel = new Label();
        _slider = new Slider();
        _button = new Button { Text = "Save" };
        _listView = new ListView();
        var cell = new DataTemplate(typeof(TextCell));
        cell.SetBinding(TextCell.TextProperty, "name");
        cell.SetBinding(TextCell.DetailProperty, "location");
        _listView.ItemTemplate = cell;

        //_listView.ItemsSource = ViewModel.monkeys;
        Content = new StackLayout
        {
            Children = {
                new Label { Text = "Hello ContentPage" },
                input, input2,
                displayLabel,
                _slider,
                _button,
                _listView
            }
        };

        this.Bind(ViewModel, x => x.username, x => x.input.Text);
        this.Bind(ViewModel, x => x.username, x => input2.Text); //error line 
        this.Bind(ViewModel, x => x.value, x => x._slider.Value);
        this.OneWayBind(ViewModel, x => x.monkeys, x => x._listView.ItemsSource);
        this.OneWayBind(ViewModel, x => x.save, x => x._button.Command);
        this.OneWayBind(ViewModel, x => x.value, x => x.displayLabel.Text);
    }

The above will not compile. It gives the exception below. When input2 is a property of the class, it runs just fine. Also, OneWayBind will not even compile at all.

System.NotSupportedException was unhandled by user code
  HResult=-2146233067
  Message=Unsupported expression type: 'Constant'
  Source=ReactiveUI
  StackTrace:
       at ReactiveUI.ExpressionMixins.GetExpressionChain(Expression This)
       at ReactiveUI.ReactiveNotifyPropertyChangedMixin.SubscribeToExpressionChain[TSender,TValue](TSender source, Expression expression, Boolean beforeChange, Boolean skipInitial)
       at ReactiveUI.WhenAnyMixin.WhenAnyDynamic[TSender,TRet](TSender This, Expression property1, Func`2 selector)
       at ReactiveUI.PropertyBinderImplementation.Bind[TViewModel,TView,TVMProp,TVProp,TDontCare](TViewModel viewModel, TView view, Expression`1 vmProperty, Expression`1 viewProperty, IObservable`1 signalViewUpdate, Object conversionHint, IBindingTypeConverter vmToViewConverterOverride, IBindingTypeConverter viewToVMConverterOverride)
       at ReactiveUI.BindingMixins.Bind[TViewModel,TView,TVMProp,TVProp](TView view, TViewModel viewModel, Expression`1 vmProperty, Expression`1 viewProperty, Object conversionHint, IBindingTypeConverter vmToViewConverterOverride, IBindingTypeConverter viewToVMConverterOverride)
       at App5.MyPage..ctor()
       at App5.App..ctor()
       at App5.WinPhone.MainPage..ctor()
  InnerException:

Upvotes: 0

Views: 1884

Answers (1)

Ana Betts
Ana Betts

Reputation: 74654

This is by-design, you'll have to use a property for two-way binds.

Upvotes: 2

Related Questions