xamarin forms xaml OnPlatform not working on bindings

安稳与你 提交于 2021-02-07 21:54:22

问题


    <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.


回答1:


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.



来源:https://stackoverflow.com/questions/41760510/xamarin-forms-xaml-onplatform-not-working-on-bindings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!