Xamarin.Forms is gaining momentum in the Xamarin world. It allows you to build beautiful, performant native apps for iOS, Android, and Windows. You create pages and views code once and have them rendered natively on all targeted platforms.

If you have dealt with Xamarin and iOS before, you are certainly aware of the feature that was introduce beginning iOS7: A single-page application shares the screen with the status bar. That means that the top of the displayed page will occupy the same space at the status bar, unless the application specifically compensate for it with extra padding. This extra padding at the top is however only required for iOS since Android and Windows Phone do not have this issue.

Instead of defining a Shared Asset Project with conditional compilation (very ugly in my opinion, please never use it!), Xamarin.Forms provides two methods on the static class Device named OnPlatform which allows you to execute code specific to a particular platform from your shared code! How amazing is that?

Padding = new Thickness(0, Device.OnPlatform(iOS: 20, Android: 0, WinPhone: 0), 0, 0);
Device.OnPlatform(iOS: () =>
{
 Padding = new Thickness(0, 20, 0, 0);
});

In this variation, we are only executing an action if the target platform is iOS.

The Device.OnPlatform method is a very neat tool to tweak the UI depending on the target platform. However it does not have access to platform specific APIs. For that, the use of dependency injection is the way to go!