使用DataBinding Xamarin表单旋转标签

时间:2014-10-09 19:32:57

标签: xaml xamarin xamarin.forms

我正在尝试复制following example以了解Xamarin中的绑定。

运行以下脚本后,我的第一个标签不会随滑块一起旋转。enter image description here

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="DataBinding.MyPage" Title="Slider Bindings Page">
 <StackLayout>
    <Label Text="ROTATION"
           BindingContext="{x:Reference Name=slider}"
           Rotation="{Binding Path=Value}"
           Font="Bold, Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />

    <Slider x:Name="slider"
            Maximum="360"
            VerticalOptions="CenterAndExpand" />

    <Label BindingContext="{x:Reference slider}"
           Text="{Binding Value, 
                          StringFormat='The angle is {0:F0} degrees'}"
           Font="Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
  </StackLayout>


</ContentPage>

1 个答案:

答案 0 :(得分:4)

以前在 XAML 页面中要求任何使用 {x:Reference} 必须让被引用的控件出现在标记之前被引用。

即。

Xamarin.Forms SliderBindings 示例中,我们最初使用以下标记,与 Xamarin.Forms 1.22x 一起使用: -

<StackLayout>
    <Slider x:Name="slider"
            Maximum="360"
            VerticalOptions="CenterAndExpand" />

    <Label Text="ROTATION"
           BindingContext="{x:Reference Name=slider}"
           Rotation="{Binding Path=Value}"
           Font="Bold, Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />

    <Label BindingContext="{x:Reference slider}"
           Text="{Binding Value, 
                          StringFormat='The angle is {0:F0} degrees'}"
           Font="Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
</StackLayout>

但是,现在在 Xamarin.Forms v1.2.3.6257 的最新版本中,您不再需要在 {x:Referenced} 之前放置控件一份文件,可以通过他们更新的例子看出: -

<StackLayout>
    <Label Text="ROTATION"
           BindingContext="{x:Reference Name=slider}"
           Rotation="{Binding Path=Value}"
           Font="Bold, Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />

    <Slider x:Name="slider"
            Maximum="360"
            VerticalOptions="CenterAndExpand" />

    <Label BindingContext="{x:Reference slider}"
           Text="{Binding Value, 
                          StringFormat='The angle is {0:F0} degrees'}"
           Font="Large"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
  </StackLayout>

因此,如果您将项目更新为最新的 Xamarin.Forms v1.2.3.6257 ,那么您将可以 {x:参考} 控件而无需订购控制很重要。可能仍有某些恢复,但它似乎比以前的版本更灵活。