Computer Magic Logo
Different XAML code for each platform

Tuesday, April 12, 2016

Published by Aristotelis Pitaridis

We can have different XAML code used depending on the platform that our application is executed. The following example demonstrates how to do it by displaying a different Labal for each platform.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Sample.Page1">
  <OnPlatform x:TypeArguments="View">         
    <OnPlatform.iOS>             
      <Label Text="iOS device" />         
    </OnPlatform.iOS> 
    <OnPlatform.Android>             
      <Label Text="Android device" />         
    </OnPlatform.Android> 
    <OnPlatform.WinPhone>             
      <Label Text="Windows device" />         
    </OnPlatform.WinPhone>     
  </OnPlatform>
</ContentPage>