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.
- 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 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.
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).
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.
Step 4
Add the label controls to Mainpage.Xaml for displaying the title and app info like Appname, Packagename, Version, and Buildnumber.
- <Label Text="Retrieving Appinfo in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>
- <Label HorizontalOptions="Center" x:Name="lblAppName"></Label>
- <Label HorizontalOptions="Center" x:Name="lblPName"></Label>
- <Label HorizontalOptions="Center" x:Name="lblVer"></Label>
- <Label HorizontalOptions="Center" x:Name="lblBNumber"></Label>
Step 5
Add the following code to 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 to Mainpage.Xaml.cs.
- using Xamarin.Essentials;
- lblAppName.Text = "App Name :" + AppInfo.Name;
- lblPName.Text = "App Package Name :" + AppInfo.PackageName;
- lblVer.Text = "App Version :" + AppInfo.VersionString;
- 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.
- <?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:XamFormAppInfo" x:Class="XamFormAppInfo.MainPage">
- <StackLayout>
- <Label FontAttributes="Bold" Text="Retrieving Appinfo in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />
- <Label HorizontalOptions="Center" x:Name="lblAppName"></Label>
- <Label HorizontalOptions="Center" x:Name="lblPName"></Label>
- <Label HorizontalOptions="Center" x:Name="lblVer"></Label>
- <Label HorizontalOptions="Center" x:Name="lblBNumber"></Label>
- </StackLayout>
- </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).
Step 8
Deploy your app to UWP and Android. The output of the XamFormAppInfo app is shown below.
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.