1

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.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>
casillas
  • 16,351
  • 19
  • 115
  • 215
  • 1
    try Rotation="{Binding Value}" – Jason Oct 09 '14 at 20:09
  • @Jason, no it did not. – casillas Oct 09 '14 at 20:13
  • 1
    I've just run the example on all 3 platforms at it works fine, even on iOS. Are you trying to replicate this in your own new project perhaps?, or are you just running it as part of the existing examples solution? – Pete Oct 09 '14 at 20:44
  • Interesting! I have run exactly the code that I posted, slider does not affect rotation. – casillas Oct 09 '14 at 21:15
  • However, if you put slider on the top of other views (labels), then it works. – casillas Oct 09 '14 at 21:16
  • 1
    What version of Xamarin.Forms are you using? – Pete Oct 09 '14 at 21:19
  • 1
    I think you may be using and older version as it used to be a requirement that any referenced controls had to appear in the markup prior to the reference being used. It looks like on the latest version *1.2.3.6257* they have now fixed xaml pages so controls can reference any where now. I will write this up as an answer. – Pete Oct 09 '14 at 21:28
  • I am using Xamarin 5.5 version and 1.2.3.6257 version of Xamarin Form – casillas Oct 09 '14 at 21:38
  • 1
    Thats strange as it should work then the same as my examples version, especially as we are using the same markup? Can you send the project on email and I will take a look? The experience you had would indicate a previous version as it always used to work that way. – Pete Oct 09 '14 at 21:46

1 Answers1

4

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.

i.e.

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>

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>

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.

Pete
  • 4,746
  • 2
  • 15
  • 21
  • I have copied exactly your latest code, but still have no luck. – casillas Oct 09 '14 at 22:01
  • I still think you maybe using a previous version of Xamarin.Forms. Can you send your example and I will investigate? – Pete Oct 09 '14 at 22:01
  • Sure I am sending it via email. – casillas Oct 09 '14 at 22:02
  • Just sent it to your email, please let me know if you receive it. – casillas Oct 09 '14 at 22:04
  • 1
    Yes - received. Your Xamarin.Forms.Platform.iOS is not updated to the latest version of v.1.2.3 (your file is currently at version 1.0.5256.2017). So although your Xamarin.Forms.Core and Xamarin.Forms.Xaml is version 1.2.3, your don't really have the full latest version as your packages are out of sync slightly. If you get the latest version from NuGet, you should be all ok. – Pete Oct 09 '14 at 22:16
  • Hello Pete, do you have any idea about the following question http://stackoverflow.com/questions/26303074/parsing-data-in-xamarin-forms – casillas Oct 10 '14 at 15:39