【问题标题】:Binding to a non class variable fails in ReactiveUI在 ReactiveUI 中绑定到非类变量失败
【发布时间】:2014-12-22 18:02:48
【问题描述】:

给定一个示例 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

【问题讨论】:

    标签: c# xamarin.forms reactiveui


    【解决方案1】:

    这是设计使然,您必须使用属性进行双向绑定。

    【讨论】:

    • 好的。我正在研究一个场景,其中传递给页面的数据实际上是它创建各种 ui 元素的对象列表。由于这些 ui 元素(标签、条目等)是在外观中创建的,因此它们不能成为类字段或属性的一部分。有什么办法可以解决这个问题,否则我将不得不重新考虑我的设计:)?
    • 如果你想要双向绑定,你必须重新考虑它。但是,我声称您可能只希望从 TextView 绑定到 VM,因此您可以使用 WhenAny().BindTo() 与任意值一起使用
    • 谢谢。我目前正在玩 xamarin 和 ReactiveUI 所以决定检查源代码。我在修改了一些 9 个类后让它工作了:)。我的控件不必是主类的一部分。我将继续对其进行改进和测试,但会坚持不平凡的应用程序的默认设计。
    猜你喜欢
    • 2017-03-05
    • 2020-07-08
    • 2016-07-17
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多