OnPlatform/OnIdiom markup extensions in xamarin forms 3.2.0

Xamarin.Forms is going toward more stability and adding features day by day. Let’s explore one of the enhancement which is currently available in xamarin.forms 3.2.0-pre1.

This feature includes markup extension for OnPlatform and OnIdiom. Currently we are using On Platform and On Idiom like this.

How we use these Previously:

<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">

<StackLayout.IsVisible>
<OnPlatform x:TypeArguments="x:Boolean" Android="True" iOS="False"></OnPlatform>
</StackLayout.IsVisible>

<StackLayout.BackgroundColor>
<OnIdiom x:TypeArguments="Color" Phone="Black" Desktop="Red"></OnIdiom>
</StackLayout.BackgroundColor>

<Button Text="Welcom to XamarinWeb.com" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"/>

</StackLayout>

The same code from markup extension introduced in xamarin.form 3.2.0-pre1

<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" 
IsVisible="{OnPlatform Android=True,iOS=False}"
BackgroundColor="{OnIdiom Phone=Black,Desktop=Red}">

<Button Text="Welcom to XamarinWeb.com" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"/>

</StackLayout>

 

Now its become more easy to write device and platform specific things using markup extensions.

 

Output of both code snippets is same as shown on android phone:

 

XamarinWeb Founder | MVP – C-SharpCorner | Xamarin Forms Developer

Leave a Reply

Your email address will not be published. Required fields are marked *