WinRT specific properties in Xamarin Forms XAML

Xamarin Forms originally supported iOS, Android and Windows Phone Silverlight applications. The OnPlatform<T> class provides a mechanism for putting values directly into your XAML which are dependent on the host platform. This is often necessary to cope with different screen sizes and scaling behaviour across platforms. When Windows 8.1 and Windows Phone 8.1 were later added the OnPlatform class was not updated. This means that if you use OnPlatform it can’t provide a value for WinRT platforms. Without resorting to changing properties in the code-behind we wanted a way to set platform specific property values from XAML. Luckily the solution was quite simple – just 55 lines of code including doc comments and I’ve published the code here:-

To use in your XAML you just need to add an XML namespace declaration to your root node (e.g. ContentPage)


Then use the class from your XAML. It works with any type which can be represented as a string in XAML. e.g.

   <forms:OnPlatform2 x:TypeArguments="Thickness" iOS="5" Android="5" Windows="0,10,0,5"/>