Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ReactiveUI中绑定到非类变量失败_C#_Xamarin.forms_Reactiveui - Fatal编程技术网

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

C# 在ReactiveUI中绑定到非类变量失败,c#,xamarin.forms,reactiveui,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;

给出一个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);
    }
  • 这是故意的还是我做错了什么
  • 虽然它没有运行,但为什么该代码使用
    Bind
    编译,而不是
    OneWayBind

    • 这是出于设计,您必须使用属性进行双向绑定

      嗯。我在看一个场景,其中传递到页面的数据实际上是一个对象列表,它从中创建各种ui元素。由于这些ui元素(标签、条目等)是在外观中创建的,因此它们不能成为类字段或属性的一部分。有什么办法可以解决这个问题,否则我只能重新考虑我的设计:)?如果你想要双向绑定,你必须重新考虑。但是,我敢说,您可能只是真正想要从TextView绑定到VM,所以您可以使用
      WhenAny().BindTo()
      ,它可以处理任意值的hanks。我目前正在使用xamarin和ReactiveUI,因此决定签出源代码。在修改了9个类之后,我让它工作了:)。我的控件不必是主类的一部分。我将继续改进和测试它,但对于不平凡的应用程序,我将坚持默认设计。
      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: