Configuring XAML with platform-specific values

Every platform as we know has its own screen metric system, and while Xamarin.Forms does a great job of creating a cross-platform UI and following the platform specifics, we often might need some platform tweaking.

Fortunately, Xamarin.Forms has an out-of-the-box feature that we can use in XAML and in code.

How to do it…

  1. In Visual Studio, create a Xamarin Forms (Portable) project; give it the name XamFormsOnPlatform.
  2. Right-click and Add | New Item… in the PCL core project. Create a new Forms Xaml Page and name it MainPage.
  3. Open the MainPage.xaml file, remove the default label, and add the following in the ContentPage tag:
    <ContentPage.BackgroundColor> <OnPlatform x:TypeArguments="Color"> <OnPlatform.iOS>#FF0000</OnPlatform.iOS> ...

Get Xamarin: Cross-Platform Mobile Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.