Adapting for Device.OS going obsolete in Xamarin.Forms 2.3.4

Those of you who may have updated to Xamarin.Forms 2.3.4 may have noticed something peculiar with your IntelliSense and warnings in Visual Studio, Xamarin Studio and Visual Studio for Mac recently when working with your Xamairn Forms projects. Once of the things they have been discussing is changing how the OnPlatform mechanism works to provide better support for more platforms like Tizen or something other then the standard Android, iOS, & Windows. This update means that the way OnPlatform is coded for needs to be updated as well in our C# and XAML code.

Updating your OnPlatform in XAML

Before 2.3.4 our OnPlatform in XAML looked something like this

<OnPlatform x:TypeArguments="Color" Android="#ECECEC" iOS="#C8C7CC" WinPhone="#C8C7CC" x:Key="Divider"/>
<OnPlatform x:TypeArguments="Thickness" Android="8,16" iOS="0,0,0,16" WinPhone="0,0,0,16" x:Key="CardPadding"/>
<OnPlatform x:TypeArguments="x:Double" Android="16" iOS="0" WinPhone="0" x:Key="CardSpacing"/>
<OnPlatform x:TypeArguments="Font" Android="SemiBold" iOS="SemiBold" WinPhone="SemiBold" x:Key="DemoSemiBoldTextSize" />
<OnPlatform x:TypeArguments="x:String" Android="sans-serif-light" x:Key="SectionLabelFontFamily" />

however this has changed and the above code samples now must be formatted a little differently, each platform must be nested inside the OnPlatform tag with an <On Platform> tag to define the values. Below is a example of the XAML updated to match the new style.

<OnPlatform x:TypeArguments="Color" x:Key="WindowBackgroundTable">
 <On Platform="Android" Value="#F5F5F5" />
 <On Platform="iOS" Value="#EFEFF4" />
 <On Platform="WinPhone" Value="#EFEFF4" />
 </OnPlatform>
<OnPlatform x:TypeArguments="Thickness" x:Key="CardPadding">
 <On Platform="Android" Value="8,16" />
 <On Platform="iOS" Value="0,0,0,16" />
 <On Platform="WinPhone" Value="0,0,0,16" />
 </OnPlatform>
<OnPlatform x:TypeArguments="Thickness" x:Key="CardSpacing">
 <On Platform="Android">16</On>
 <On Platform="iOS">0</On>
 <On Platform="WinPhone">0</On>
 </OnPlatform>
<OnPlatform x:TypeArguments="Font" x:Key="DemoSemiBoldTextSize">
 <On Platform="Android">SemiBold</On>
 <On Platform="iOS">SemiBold</On>
 <On Platform="WinPhone">SemiBold</On>
</OnPlatform>
<OnPlatform x:TypeArguments="x:String" x:Key="SectionLabelFontFamily">
 <On Platform="Android">sans-serif-light</On>
</OnPlatform>

As you can see it is not too much different but the update is enough to throw people off if you are newer to forms.

Updating OnPlatform from your code behind

Again there is a bit of a change we have to do in our code to adjust for this update. If you are doing something like:

if (Device.OS == TargetPlatform.iOS)
 {
 HasShadow = false;
 OutlineColor = Color.Transparent;
 BackgroundColor = Color.Transparent;
 }

it now means you must rework it to:

if (Device.RuntimePlatform == Device.iOS)
 {
 HasShadow = false;
 OutlineColor = Color.Transparent;
 BackgroundColor = Color.Transparent;
 }

as you can see the biggest difference we are no longer using Device.OS but instead are switching to Device.RuntimePlatform and using Device.<platform> constant.

IF you want more info check out the Pull Request where this feature was added or this helpful Forum Post about the Redesign. If you are wondering about OnIdom, that has not changed.

Advertisements

Author: Mitch Muenster

Microsoft & Xamarin MVP, Xamarin Certified Developer, VR/AR Enthusiast, Biohacker, & Public Speaker

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s