Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xamarin forms xaml OnPlatform not working on bindings

    <cv:BindableComboBox ItemsSource="{Binding Themes}" SelectedValue="{Binding SelectedTheme}">
      <cv:BindableComboBox.Title>
        <OnPlatform x:TypeArguments="x:String"
            iOS="{Binding ThemePickerTitle}"
            Android="{Binding ThemePickerTitle}"/>
      </cv:BindableComboBox.Title>
    </cv:BindableComboBox>

so I have picker and I want to bind its Title to ThemePickerTitle property. It works well if I write it as inline attrubute. But I dont want to have title on windows. So I write OnPlatform to have it only on Ios and Droid, but now it does compile.

like image 601
mister_giga Avatar asked Jan 20 '17 09:01

mister_giga


1 Answers

The Xaml you pasted suppose that the OnPlatform object is the target of the Binding to string, and then the OnPlatform returns the right string depending on the Platform.

Unfortunately, that doesn't work because OnPlatform is not a BindableObject and can't be the target of a Binding.

What make sense, is using this Xaml:

<cv:BindableComboBox ItemsSource="{Binding Themes}" SelectedValue="{Binding SelectedTheme}">
  <cv:BindableComboBox.Title>
    <OnPlatform x:TypeArguments="BindingBase"
        iOS="{Binding ThemePickerTitle}"
        Android="{Binding ThemePickerTitle}"/>
  </cv:BindableComboBox.Title>
</cv:BindableComboBox>

It means that the OnPlatform returns the right Binding (without evaluating it) depending on the platform. As of today (2017-01-24), this does not work in Xamarin.Forms, but the patch to fix that is ready and will land in one of the next version: https://github.com/xamarin/Xamarin.Forms/pull/709.

like image 170
Stephane Delcroix Avatar answered Nov 01 '22 08:11

Stephane Delcroix