在ReactiveUI中绑定到非类变量失败

时间:2014-12-22 01:39:21

标签: c# xamarin.forms reactiveui

给出示例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);
    }

以上不会编译。它给出了下面的例外情况。当input2是类的属性时,它运行得很好。此外,OneWayBind甚至根本不会编译。

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:
  • 这是设计还是我在这里做错了什么?
  • 虽然它没有运行,但为什么代码会使用Bind而不是OneWayBind进行编译?

1 个答案:

答案 0 :(得分:2)

这是按照设计,你必须使用属性进行双向绑定。