Read the previous part of the series here,
- Quick Start Tutorial: Creating Universal Apps Via Xamarin - Part One
- Quick Start Tutorial: Creating Universal Apps Via Xamarin - Part Two
- 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.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,
- var targetPlatform = Device.OS.ToString();
- var targetdevice = Device.Idiom.ToString();
-
- switch (Device.OS)
- {
- case TargetPlatform.Other:
- break;
- case TargetPlatform.iOS:
- platformVal = 20;
- break;
- case TargetPlatform.Android:
- platformVal = 30;
- break;
- case TargetPlatform.WinPhone:
- platformVal = 35;
- break;
- case TargetPlatform.Windows:
- platformVal = 35;
- break;
- }
-
- d If different size set the Phone, Tablet and Desktop
- switch (Device.Idiom)
- {
- case TargetIdiom.Unsupported:
- break;
- case TargetIdiom.Phone:
- break;
- case TargetIdiom.Tablet:
- platformVal = 50;
- break;
- case TargetIdiom.Desktop:
- platformVal = 60;
- break;
- }
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.
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.
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,
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.
Device.GetNamedSize
This is the function that returns platform-specific control font sizes.
First argument NamedSize, this enum specifies the predefined font, other arguments help to control the object.
Example
Label LblUpdate;
Function returns platform-specific font size for the label control.
The