Forum Xamarin.Forms

How use Device.RuntimePlatform in XAML??

SteveShaw.5557SteveShaw.5557 USMember ✭✭✭
edited May 2018 in Xamarin.Forms

Device.OnPlatform is marked Obsolete:

Docs claim the replacement is Device.RuntimePlatform. So I try to find how to use it. ALL I find is C# code. Huh?

For example, on
@AdamPedley shows XAML using OnPlatform:

    <OnPlatform x:TypeArguments="Thickness">
        <On Platform="Android, UWP">0</On>
        <On Platform="iOS">0,20,0,0</On>

And then the replacement which is in C#:

case Device.iOS:
    this.Padding = new Thickness(0,20,0,0);
case Device.Android:
case Device.UWP:   
case Device.macOS:
    // This is just an example. You wouldn't actually need to do this, since Padding is already 0 by default.
    this.Padding = new Thickness(0);

??? That isn't a replacement for a feature that works in XAML - that's C# code.

Explanation please. Is this useable in XAML? Will be in the future?

Best Answer


  • SirAndriySirAndriy USMember ✭✭✭

    This works for me in xaml, so it's useable

            <OnPlatform x:TypeArguments="Thickness">
                <On Platform="iOS">0,20,0,0</On>
                <On Platform="Android">0,10,0,0</On>
  • SteveShaw.5557SteveShaw.5557 USMember ✭✭✭
    edited May 2018

    Yes, I am using that. My question is about it being marked as Obsolete in docs, with Device.RuntimePlatform as the suggested replacement - but I have not found how to use this supposed replacement in XAML.

    Perhaps if I was more familiar with XAML, it would be obvious what the XAML equivalent is of that C# code, and I would just try it. (I'm just starting to migrate from Xamarin.iOS and Xamarin.Android to X Forms with XAML.)

Sign In or Register to comment.