【问题标题】:Xamarin.Forms - View-To-View Bindings In C# Code?Xamarin.Forms - C# 代码中的视图到视图绑定?
【发布时间】:2016-08-31 07:14:04
【问题描述】:

Xamarin 网站有一个 section on View-To-View Binding,但示例是在 XAML 中,使用 x:Reference 标记扩展。如何在 C# 代码中做同样的事情?

【问题讨论】:

    标签: c# mvvm data-binding xamarin xamarin.forms


    【解决方案1】:

    只需将视图的 BindingContext 设置为另一个视图,您就可以像 XAML 示例一样有效地执行此操作。

    例子:

    var stepper = new Stepper();
    
    var label = new Label {
        BindingContext = stepper
    };
    label.SetBinding (Label.TextProperty, new Binding ("Value", stringFormat: "{0:F0}"));
    

    或者,您可以设置 Binding.Source:

    var stepper = new Stepper();
    
    var label = new Label();
    label.SetBinding (Label.TextProperty, new Binding ("Value", stringFormat: "{0:F0}", source: stepper));
    

    Source

    【讨论】:

      【解决方案2】:

      x:Reference 只是将一个对象的引用设置为另一个对象的BindingContext

      这基本上是:

      myLabel.BindingContext = mySlider;
      

      【讨论】:

        猜你喜欢
        • 2015-03-19
        • 2017-02-22
        • 2020-07-20
        • 2015-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-14
        • 1970-01-01
        相关资源
        最近更新 更多