How use Device.RuntimePlatform in XAML??

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

Device.OnPlatform is marked Obsolete: https://docs.microsoft.com/en-us/dotnet/api/Xamarin.Forms.Device.OnPlatform?view=xamarin-forms#Xamarin_Forms_Device_OnPlatform_System_Action_System_Action_System_Action_System_Action_

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 https://xamarinhelp.com/xamarin-forms-onplatform-runtimeplatform/
@AdamPedley shows XAML using OnPlatform:

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

And then the replacement which is in C#:

switch(Device.RuntimePlatform)
{
case Device.iOS:
    this.Padding = new Thickness(0,20,0,0);
    break;
case Device.Android:
case Device.UWP:   
case Device.macOS:
default:
    // 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);
    break;
}

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

Answers

  • SirAndriySirAndriy USMember ✭✭✭

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

    <Grid>
        <Grid.Padding>
            <OnPlatform x:TypeArguments="Thickness">
                <On Platform="iOS">0,20,0,0</On>
                <On Platform="Android">0,10,0,0</On>
            </OnPlatform>
        </Grid.Padding>
    </Grid>
    
  • 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.