【问题标题】:BindingContext is not working in Xamarin formsBindingContext 在 Xamarin 表单中不起作用
【发布时间】:2017-09-19 23:39:35
【问题描述】:

我使用以下代码将数据绑定到标签以更改滑块值,并且效果很好。

 <StackLayout VerticalOptions="Center" HorizontalOptions="Center" >
    <Label Text="{Binding Source={x:Reference slider1}, Path=Value, StringFormat='Value is {0:F2}'}"
           Opacity="{Binding Source={x:Reference slider1}, Path=Value}"
           x:Name="label1"/>
    <Slider x:Name="slider1"></Slider>
 </StackLayout>

在 xaml.cs 页面中,我已将滑块值初始化为 0.5

   public MainPage()
   {
       InitializeComponent();
       slider1.Value=0.5;
   }

但是,当我想使用 BindingContext 绑定数据时,它无法正确显示输出。

<StackLayout VerticalOptions="Center" HorizontalOptions="Center" >
    <Label BindingContext="x:Reference slider1" 
           Text="{Binding Value, StringFormat='Value is {0:F2}'}"
           Opacity="{Binding Value}"
           x:Name="label1"/>
    <Slider x:Name="slider1"></Slider>
 </StackLayout>

我的第二个 xaml 有什么问题?

【问题讨论】:

    标签: c# xaml xamarin xamarin.android xamarin.forms


    【解决方案1】:

    我认为,您错过了 {}。试试看

    <Label BindingContext="{x:Reference slider1}"
    

    而不是

    <Label BindingContext="x:Reference slider1"  
    

    【讨论】:

      【解决方案2】:

      如果你使用BindingContext,那么对象应该实现INotifyPropertyChanged。

      Slider 没有实现这个接口。一般来说,你应该将BindableProperty分配给BindingContext。

      https://developer.xamarin.com/guides/xamarin-forms/xaml/bindable-properties/

      【讨论】:

        猜你喜欢
        • 2014-12-26
        • 2018-12-15
        • 1970-01-01
        • 2019-10-12
        • 2018-11-11
        • 2017-12-27
        • 1970-01-01
        • 1970-01-01
        • 2020-06-22
        相关资源
        最近更新 更多