Getting Device Info In Xamarin.Forms Application Using Xamarin Essentials For Android And UWP

The DeviceInfo class is available at Xamarin.Essentials API. It provides information about the device the application is running on. Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C# leveraging Xamarin.

Xamarin.Essentials provide a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.

Reading this article, you will learn about getting device information including Device Name, DeviceType, Platform, Version, Idiom, Model, and Manufacturer in Xamarin Forms applications using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in cross-platform application development.

The following important tools are required for developing Xamarin Forms Apps,

  1. Windows 10 (Recommended)
  2. Visual Studio 2017
  3. Android API 19 or higher and UWP 10.0.16299.0 or higher.

Now, we can discuss step by step App development.

Step 1

Open Visual Studio 2017 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms)-> Give the Suitable Name for your App (XamFormDevInfo) ->OK

 

Step 2

Select the Cross-Platform template as a Blank APP ->Set Platform as Android and UWP and code sharing strategy as .NET standard, and afterwards, Visual Studio creates 3 projects (Portable, Droid, UWP).

 

Step 3

For adding a reference:

RightClick Your solution (XamFormDevInfo) and select Manage NuGet Packages.

For adding Xamarin.Essentials Reference, choose Browse and Search Xamarin.Essentials, select the package and select all the projects (portable, Android, UWP) and install it.

 

Step 4

Add the label controls in Mainpage.Xaml for displaying Title and Device Name, DeviceType, Platform, Version, Idiom, Model and Manufacturer.

  1. <StackLayout>  
  2.     <Label FontAttributes="Bold" Text="Getting Deviceinfo in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />  
  3.     <Label HorizontalOptions="Center" x:Name="lblDevName"></Label>  
  4.     <Label HorizontalOptions="Center" x:Name="lblDevType"></Label>  
  5.     <Label HorizontalOptions="Center" x:Name="lblPlt"></Label>  
  6.     <Label HorizontalOptions="Center" x:Name="lblIdiom"></Label>  
  7.     <Label HorizontalOptions="Center" x:Name="lblMdl"></Label>  
  8.     <Label HorizontalOptions="Center" x:Name="lblMfg"></Label>  
  9.     <Label HorizontalOptions="Center" x:Name="lblVer"></Label>  
  10. </StackLayout> 

Step 5

Add the following code in the MainActivity.cs in XamFormAppInfo.Android project,

In onCreate() method,

  1. Xamarin.Essentials.Platform.Init(this, savedInstanceState); 

Also add the below method,

  1. publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {  
  2.     Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  3.     base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  4. }  

Step 6

Add the following namespace and code in Mainpage.Xaml.cs

  1. using Xamarin.Essentials;  
  2. lblDevName.Text = "Device Name :" + DeviceInfo.Name;  
  3. lblDevType.Text = "DeviceType :" + DeviceInfo.DeviceType.ToString();  
  4. lblPlt.Text = "Platform : " + DeviceInfo.Platform;  
  5. lblVer.Text = "Version :" + DeviceInfo.Version;  
  6. lblIdiom.Text = "Idiom :" + DeviceInfo.Idiom;  
  7. lblMdl.Text = "Model :" + DeviceInfo.Model;  
  8. lblMfg.Text = "Manufacturer :" + DeviceInfo.Manufacturer;  

Note

Automatically the following code will be generated in XAML code view, while we are finished in the design view.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamFormDevInfo" x:Class="XamFormDevInfo.MainPage">  
  3.     <StackLayout>  
  4.         <Label FontAttributes="Bold" Text="Getting Deviceinfo in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />  
  5.         <Label HorizontalOptions="Center" x:Name="lblDevName"></Label>  
  6.         <Label HorizontalOptions="Center" x:Name="lblDevType"></Label>  
  7.         <Label HorizontalOptions="Center" x:Name="lblPlt"></Label>  
  8.         <Label HorizontalOptions="Center" x:Name="lblIdiom"></Label>  
  9.         <Label HorizontalOptions="Center" x:Name="lblMdl"></Label>  
  10.         <Label HorizontalOptions="Center" x:Name="lblMfg"></Label>  
  11.         <Label HorizontalOptions="Center" x:Name="lblVer"></Label>  
  12.     </StackLayout>  
  13. </ContentPage>  

Step 7

We will test Android and UWP. So, we can set the Multiple Startup Projects as XamFormDevInfo.Droid and XamFormDevInfo.UWP (Universal Windows).

 

Step 8

Deploy your app in UWP and Android. The output of the XamFormDevInfo App is,

 

Summary

Now, you have successfully retrieved the Device information in a Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.


Similar Articles