2014-10-09 60 views
1

我試圖複製following example以瞭解Xamarin中的綁定。使用DataBinding旋轉標籤Xamarin Forms

一旦我運行下面的腳本,我的第一個標籤不隨着滑塊一起旋轉。 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

嘗試旋轉=「{綁定值}」 – Jason 2014-10-09 20:09:01

+0

@Jason,不,它沒有。 – casillas 2014-10-09 20:13:42

+1

我剛剛在所有3個平臺上運行示例,即使在iOS上也能正常運行。你是否試圖在你自己的新項目中複製它?或者你只是將它作爲現有示例解決方案的一部分運行? – Pete 2014-10-09 20:44:13

回答

4

它曾經是在XAML頁一個要求,即任何使用{X:參考}的必須有正被引用出現在它之前所引用的標記的控件。

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:引用},正如它們可以看到的更新例如: -

<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:參考}沒有控制的控制可被訂購重要。可能有一些restrtictions,但它似乎比以前的版本更靈活。

+0

我已經複製了您的最新代碼,但仍然沒有運氣。 – casillas 2014-10-09 22:01:03

+0

我仍然認爲你可能會使用以前版本的Xamarin.Forms。你可以發送你的例子,我會調查? – Pete 2014-10-09 22:01:56

+0

確定我通過電子郵件發送。 – casillas 2014-10-09 22:02:36