Two Xamarin.Essentials classes can provide much needed info about mobile devices: DeviceInfo and DeviceDisplay.
As mobile developers, sometimes we need to get information from mobile devices to be able to perform some determined actions using the data obtained. That’s why in this article we will learn how to get it through Xamarin Essentials. The topics will be divided in two groups: Device information and device display information.
โ Xamarin Essentials: Is a cross-platform library that gives us access to useful information and native libraries from the device’s platform in order to simplify our lives when developing a mobile application. Some examples of the types of information we can obtain include: Device information, device sensor information, motion detectors, phone dialer, among many other interesting features to apply in our device in Xamarin Forms. ๐
โ Device information: Gives us access to the information of the device that is running our application.
โ Device display information: Gives us access to the information about the device’s screen metrics our application is running on.
To start, let’s add Xamarin Essentials references to our class:
using Xamarin.Essentials;
Once we’ve added the references, let’s explore the API’s properties:
๐น Model: Returns a string value and provides us the device model. (For example: x86_64 X.)
string Model = DeviceInfo.Model;
๐น Manufacturer: Returns a string value and provides us the device manufacturer. (For example: Apple.)
string Manufacturer = DeviceInfo.Manufacturer;
๐น Name: Returns a string value and provides us the device name. (For example: iPhone X.)
string Name = DeviceInfo.Name;
๐น Version: Returns a string value and provides us the device version. (For example: 12.2.)
string Version = DeviceInfo.VersionString;
๐น Platform: Returns a DevicePlatform value and provides us the device platform. Values can be: iOS, Android, UWP, Unknown. (For example: iOS.)
var Platforms = DeviceInfo.Platform;
๐น Idiom: Returns a DeviceIdiom value and provides us the device type that is running our application. Values can be: Phone, Desktop, TV, Tablet, Watch, Unknown. (For example: Phone.)
var Idiom = DeviceInfo.Idiom;
๐น DeviceType: Returns a DeviceType value and identifies if we are running the application on an emulator or physical device. (For example: Virtual.)
var DeviceType = DeviceInfo.DeviceType;
To get access, you just have to add the following code line the property that you need to use: DeviceDisplay.MainDisplayInfo
For example:
double _density = DeviceDisplay.MainDisplayInfo.Density;
Now let’s learn the other properties:
Thanks for reading my article! ๐
References:
Leomaris Reyes is a Software Engineer from the Dominican Republic, with more than 5 years of experience. A Xamarin Certified Mobile Developer, she is also the founder of Stemelle, an entity that works with software developers, training and mentoring with a main goal of including women in Tech. Leomaris really loves learning new things! ๐๐ You can follow her: Twitter, LinkedIn , AskXammy and Medium.