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,
- Windows 10 (Recommended)
- Visual Studio 2017
- 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.
- <StackLayout>
- <Label FontAttributes="Bold" Text="Getting Deviceinfo in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />
- <Label HorizontalOptions="Center" x:Name="lblDevName"></Label>
- <Label HorizontalOptions="Center" x:Name="lblDevType"></Label>
- <Label HorizontalOptions="Center" x:Name="lblPlt"></Label>
- <Label HorizontalOptions="Center" x:Name="lblIdiom"></Label>
- <Label HorizontalOptions="Center" x:Name="lblMdl"></Label>
- <Label HorizontalOptions="Center" x:Name="lblMfg"></Label>
- <Label HorizontalOptions="Center" x:Name="lblVer"></Label>
- </StackLayout>
Step 5
Add the following code in the MainActivity.cs in XamFormAppInfo.Android project,
In onCreate() method,
- Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Also add the below method,
- publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {
- Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- }
Step 6
Add the following namespace and code in Mainpage.Xaml.cs
- using Xamarin.Essentials;
- lblDevName.Text = "Device Name :" + DeviceInfo.Name;
- lblDevType.Text = "DeviceType :" + DeviceInfo.DeviceType.ToString();
- lblPlt.Text = "Platform : " + DeviceInfo.Platform;
- lblVer.Text = "Version :" + DeviceInfo.Version;
- lblIdiom.Text = "Idiom :" + DeviceInfo.Idiom;
- lblMdl.Text = "Model :" + DeviceInfo.Model;
- 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.
- <?xml version="1.0" encoding="utf-8" ?>
- <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">
- <StackLayout>
- <Label FontAttributes="Bold" Text="Getting Deviceinfo in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />
- <Label HorizontalOptions="Center" x:Name="lblDevName"></Label>
- <Label HorizontalOptions="Center" x:Name="lblDevType"></Label>
- <Label HorizontalOptions="Center" x:Name="lblPlt"></Label>
- <Label HorizontalOptions="Center" x:Name="lblIdiom"></Label>
- <Label HorizontalOptions="Center" x:Name="lblMdl"></Label>
- <Label HorizontalOptions="Center" x:Name="lblMfg"></Label>
- <Label HorizontalOptions="Center" x:Name="lblVer"></Label>
- </StackLayout>
- </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.