Warm tip: This article is reproduced from stackoverflow.com, please click
xamarin xamarin.forms xaml

How to only set OnPlatform for a specific platform?

发布于 2020-04-03 23:49:09

I am programming with Xamarin.Forms. I would like know how to only set the height request for a single platform, using OnPlatform in XAML? I want to keep the default value of -1 if the platform is not referred.

I currently have the following code:

<ListView.HeightRequest>
    <OnPlatform x:TypeArguments="x:Double">
        <On Platform="WPF" Value="153" />
        <On Platform="Android" Value="-1" />
        <On Platform="iOS" Value="-1" />
        <On Platform="UWP" Value="-1" />
    </OnPlatform>
</ListView.HeightRequest>

I wish I only had to write something like:

<ListView.HeightRequest>
    <OnPlatform x:TypeArguments="x:Double">
        <On Platform="WPF" Value="153" />
    </OnPlatform>
</ListView.HeightRequest>

This doesn't work, is it possible?

Questioner
zhichaoxiang
Viewed
60
Jack Hua - MSFT 2020-02-05 09:59

You can write the platforms with same value in one line:

<ListView.HeightRequest>
    <OnPlatform x:TypeArguments="x:Double">
        <On Platform="WPF" Value="153" />
        <On Platform="Android,iOS,UWP" Value="-1" />
    </OnPlatform>
</ListView.HeightRequest>

Refer: Providing platform-specific values