使用DataBinding Xamarin表单旋转标签 [英] Rotating Label with DataBinding Xamarin Forms
问题描述
I am trying to duplicate the following example to understand binding in Xamarin.
一旦我运行以下脚本,我的第一个标签就不会随着滑块一起旋转.
Once I run the following script, my first label is not rotating along with slider.
<?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>
推荐答案
XAML 页面上过去一直要求使用 {x:Reference} 在引用之前将要引用的控件显示在标记中.
It used to be a requirement in XAML pages that any use of {x:Reference} had to have the control that was being referenced appear in the markup prior to it being referenced.
即
在 Xamarin.Forms SliderBindings 示例中,我们最初具有以下标记,该标记可用于 Xamarin.Forms 1.22x :-
In the Xamarin.Forms SliderBindings example we had the following markup originally that works with 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} 中放置控件.文档,如其更新示例所示:-
However, now in the latest version of Xamarin.Forms v1.2.3.6257 you no longer have to place controls prior to them being {x:Referenced} in a document, as can be seen by their updated example:-
<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:Reference} 控件,而无需订购控制很重要.可能还会有某些限制,但是它似乎比以前的版本灵活得多.
So if you update your project to the latest Xamarin.Forms v1.2.3.6257, you will then be able to {x:Reference} controls without ordering of controls being important. There may be certain restrtictions still, however it seems much more flexible than in previous versions.
这篇关于使用DataBinding Xamarin表单旋转标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!