Xamarin.forms: OnPlatform/OnIdiom markup extensions

Created on 4 May 2018  Â·  9Comments  Â·  Source: xamarin/Xamarin.Forms

Description

Sometimes changing a single property value per platform is quite convenient, yet the "full" syntax of OnPlatform is overkill for scalar values in attributes.

For example:

<Label Text="Hello World">
    <Label.FontSize>
        <OnPlatform x:TypeArguments="x:Double">
            <On Platform="iOS" Value="20" />
            <On Platform="Android" Value="24" />
        </OnPlatform>
    </Label.FontSize>
</Label>

vs a markup extension version that could provide:

<Label Text="Hello World" FontSize="{OnPlatform Android=24, iOS=20}" />

Same thing applies for OnIdiom, so I think both would make XAML much more concise.

Xaml </> enhancement âž•

Most helpful comment

@asafgo that's not the markup extension form that this issue refers to. The markup extension format for your page would be:

<ContentPage Padding='{OnPlatform Android="10,5,10,0", iOS="10,20,10,0"}'>

Which is arguably immensely more concise ;)

All 9 comments

This would be legit cool. As long as it is properly documented etc. Definitely helps reduce complexity, verbosity and improves readability.

If we had blend on OSX and it supported Xamarin Forms then I could argue it's not needed but we don't.

I'm into this!

This is a great idea!! It will really simplify the verbosity of the XAML which is a pretty big complaint I hear often about using the OnPlatform feature in XAML

I got this working locally, can someone once over my change before I submit the PR? I wasn't able to test this working from the Xamarin.Forms solution but I was able to create this exact same code inside a Xamarin.Forms project and it worked. See the commit in my fork above

While I have this code working as a Custom markup extension I am unable to get it working in the Xamarin.Forms platform. I'm not going to be able to submit a PR with what I have because it doesn't work.

I would love some help to get us over this blocker so we can get a PR submitted to add this new markup extension

I am using Xamarin.Forms version 3.2.0.809874-pre3 I am facing a problem that with this version I am getting the error "The property 'Default' is set more than once" when using OnPlatform at XAML. Here is my code:

<ContentPage.Padding> <OnPlatform x:TypeArguments="Thickness"> <On Platform="Android">10,5,10,0</On> <On Platform="iOS">10,20,10,0</On> </OnPlatform> </ContentPage.Padding>

@asafgo that's not the markup extension form that this issue refers to. The markup extension format for your page would be:

<ContentPage Padding='{OnPlatform Android="10,5,10,0", iOS="10,20,10,0"}'>

Which is arguably immensely more concise ;)

I try to bind IsVisible to the Visible Property. But only for UWP. How does this work? I cannot get it running with the old layout, nor with the new.

Current solution, enabled for all Platforms:

<StackLayout IsVisible="{Binding Visible}">

Attempt with the old style

<StackLayout>
 <StackLayout.IsVisible>
     <OnPlatform x:TypeArguments="x:Boolean">
           <On Platform="WinPhone" Value="{Binding Visible}"></On>
     </OnPlatform>
</StackLayout.IsVisible>

Attempt with the new stype:

<StackLayout IsVisible="{x:OnPlatform UWP={Binding Visible}}">

Could anyone please also tell me if WinPhone is the correct wording or UWP? Is there also a placeholder for "Anything else"/* (in this case ios and android)?

@AtosNicoS both OnPlatform syntax expect values, not bindings

Was this page helpful?
0 / 5 - 0 ratings