Quick Start Tutorial: Creating Universal Apps Via Xamarin: Device Class - Part Four

In this article, you will learn how to create universal apps via Xamarin device class.

Read the previous part of the series here,

  1. Quick Start Tutorial: Creating Universal Apps Via Xamarin - Part One
  2. Quick Start Tutorial: Creating Universal Apps Via Xamarin - Part Two
  3. Quick Start Tutorial: Creating Universal Apps via Xamarin: Label and Button - Part Three

This article explains about the device class of Xamarin SDK.

Device class

This class is used to identify the platform (Windows Phone, UWP, iOS, Android) and the device (Mobile, Tablet, Desktop) and it supports other functionalities.

Device

Device.OS- This option is used to identify platform and is TargetPlatform enumeration type,

  • TargetPlatform. Android
  • TargetPlatform.WinPhone
  • TargetPlatform. Windows
  • TargetPlatform.iOS
  • TargetPlatform.Other

Each type is clearly understandable. What is the difference between WinPhone and Windows?

Windows- It is used to identity the Application running in the Universal Windows Platform (Windows 8.1).
WinPhone- It is used to identity the Windows 8 device.

OS is running in the different devices like mobile, tablet, and desktop. Device.Idiom class is used to identity the device.  

Device.Idiom- This option is used to identity the device,

  • TargetIdiom.Desktop
  • TargetIdiom.Phone
  • TargetIdiom.Tablet
  • TargetIdiom.Unsupported

TargetIdiom.Desktop- This is used to identify the Application running in the desktop. Windows 10 currently is a preview version.

Example Set the different font sizes in different platforms,

  1. var targetPlatform = Device.OS.ToString();  
  2. var targetdevice = Device.Idiom.ToString();  
  3.   
  4.           switch (Device.OS)  
  5.           {  
  6.               case TargetPlatform.Other:  
  7.                   break;  
  8.               case TargetPlatform.iOS:  
  9.                   platformVal = 20;  
  10.                   break;  
  11.               case TargetPlatform.Android:  
  12.                   platformVal = 30;  
  13.                   break;  
  14.               case TargetPlatform.WinPhone:  
  15.                   platformVal = 35;  
  16.                   break;  
  17.               case TargetPlatform.Windows:  
  18.                   platformVal = 35;  
  19.                   break;  
  20.           }  
  21.   
  22. d If different size set the Phone, Tablet and Desktop   
  23.          switch (Device.Idiom)  
  24.           {  
  25.               case TargetIdiom.Unsupported:  
  26.                   break;  
  27.               case TargetIdiom.Phone:  
  28.                   break;  
  29.               case TargetIdiom.Tablet:  
  30.                   platformVal = 50;  
  31.                   break;  
  32.               case TargetIdiom.Desktop:  
  33.                   platformVal = 60;  
  34.                   break;  
  35.           }  
Output

Output

Device.OnPlatform

Xamarin provides OnPlatform method (to avoid above big switch statement), this method is executing the statement and is based on the platform.

OnPlatform- This is a method which is generic, all arguments are Action delegate and all are optional.

Device

First two arguments: iOS and Android platform

If you pass the default arguments, it will set all the platforms and this function does not support UWP. (In the future, Xamarin may add the UWP).

Example

Set different font sizes in each platform. Each argument must be set as iOS, Android, or WinPhone.
Font Size

Font Size

Default arguments

If a platform has specified; it will take those values, otherwise the default value is taken. Android sets the font size to 20 and on the rest of the platform, sets it as 30,
arguments
arguments

OnPlatform in Xaml

We can define the OnPlatform in Xaml also. Additionally, we need to pass the argument type.

X: TypeArguments: It specifies the datatype.

Xaml

Device.GetNamedSize

This is the function that returns platform-specific control font sizes.

function

First argument NamedSize, this enum specifies the predefined font, other arguments help to control the object.

object

Example

Label LblUpdate;

object

Function returns platform-specific font size for the label control.

The
output