Retrieving Appinfo In Xamarin.Forms Application Using Xamarin Essentials For Android And UWP

The AppInfo class is available at Xamarin.Essentials API. It provides information about the application. Android, iOS, and UWP offer unique operating systems and platform APIs that developers have access to in C# leveraging Xamarin. Xamarin.Essentials provides 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 retrieving Appinfo in Xamarin.Forms applications using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in a cross-platform application development process.

The following important tools are required for developing a Xamarin.Forms App.

  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 and go to Start -> New Project. Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms). Give a suitable name to your app (XamFormAppInfo) and click OK.

Retrieving Appinfo In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 2

Select the cross-platform template as a blank app, set the platform to Android and UWP, and code sharing strategy as .NET Standard. Afterward, Visual Studio creates 3 projects (Portable, Droid, and UWP).

Retrieving Appinfo In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 3

For adding Xamarin.Essentials Reference, right-click your solution (XamFormAppInfo) and select "Manage NuGet Packages". Choose Browse and search for Xamarin.Essentials. Select the package and select all the projects (portable, Android, UWP) and install it.

Retrieving Appinfo In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 4

Add the label controls to Mainpage.Xaml for displaying the title and app info like Appname, Packagename, Version, and Buildnumber.

  1. <Label Text="Retrieving Appinfo in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>  
  2. <Label HorizontalOptions="Center" x:Name="lblAppName"></Label>  
  3. <Label HorizontalOptions="Center" x:Name="lblPName"></Label>  
  4. <Label HorizontalOptions="Center" x:Name="lblVer"></Label>  
  5. <Label HorizontalOptions="Center" x:Name="lblBNumber"></Label>  

Step 5

Add the following code to 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 to Mainpage.Xaml.cs.

  1. using Xamarin.Essentials;  
  2. lblAppName.Text = "App Name :" + AppInfo.Name;  
  3. lblPName.Text = "App Package Name :" + AppInfo.PackageName;  
  4. lblVer.Text = "App Version :" + AppInfo.VersionString;  
  5. lblBNumber.Text = "App Build Number :" + AppInfo.BuildString; 

Note
Automatically, the following code will be generated in XAML Code View while we are done 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:XamFormAppInfo" x:Class="XamFormAppInfo.MainPage">  
  3.     <StackLayout>  
  4.         <Label FontAttributes="Bold" Text="Retrieving Appinfo in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />  
  5.         <Label HorizontalOptions="Center" x:Name="lblAppName"></Label>  
  6.         <Label HorizontalOptions="Center" x:Name="lblPName"></Label>  
  7.         <Label HorizontalOptions="Center" x:Name="lblVer"></Label>  
  8.         <Label HorizontalOptions="Center" x:Name="lblBNumber"></Label>  
  9.     </StackLayout>  
  10. </ContentPage> 

Step 7

We will test Android and UWP. So, we can set the "Multiple startup projects" as XamFormAppInfo.Droid and XamFormAppInfo.UWP (universal Windows).

Retrieving Appinfo In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 8

Deploy your app to UWP and Android. The output of the XamFormAppInfo app is shown below.

Retrieving Appinfo In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Summary

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


Similar Articles