Version Tracking In Xamarin.Forms Application Using Xamarin.Essentials For Android And UWP

The VersionTracking Class is available in Xamarin.Essentials API. It is used to check the application's version and build numbers along with seeing additional information, such as if it is the first time ever the application is launched for the current version, get the previous build information, and more. Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all 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.

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

Version Tracking in Xamarin Forms application using Xamarin Essentials for Android and UWP 

Step 2

Select the cross-platform template as a Blank App ->Set Platform as Android and UWP, and code sharing strategy as .NET standard. Afterward, Visual Studio creates 3 projects (Portable, Android, UWP).

Version Tracking in Xamarin Forms application using Xamarin Essentials for Android and UWP 

Step 3

For adding the reference,

Right-click your solution (XamFromVerTrack) and select "Manage NuGet Packages".

For adding Xamarin.Essentials reference, choose Browse and search for Xamarin.Essentials. Select the package and select all the projects (portable, Android, UWP) and install it.

Version Tracking in Xamarin Forms application using Xamarin Essentials for Android and UWP 

Step 4

Add the label controls in Mainpage.Xaml for displaying Title, display version check information.

  1. < <Label FontAttributes="Bold" Text="Version Tracking in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />  
  2. <Label HorizontalOptions="Center" x:Name="lblfirstLaunch"></Label>  
  3. <Label HorizontalOptions="Center" x:Name="lblfirstLaunchCurrent"></Label>  
  4. <Label HorizontalOptions="Center" x:Name="lblfirstLaunchBuild"></Label>  
  5. <Label HorizontalOptions="Center" x:Name="lblcurrentVersion"></Label>  
  6. <Label HorizontalOptions="Center" x:Name="lblcurrentBuild"></Label>  
  7. <Label HorizontalOptions="Center" x:Name="lblpreviousVersion"></Label>  
  8. <Label HorizontalOptions="Center" x:Name="lblpreviousBuild"></Label>  
  9. <Label HorizontalOptions="Center" x:Name="lblfirstVersion"></Label>  
  10. <Label HorizontalOptions="Center" x:Name="lblfirstBuild"></Label>  
  11. <Label HorizontalOptions="Center" x:Name="lblversionHistory"></Label>  
  12. <Label HorizontalOptions="Center" x:Name="lblbuildHistory"></Label>  

Step 5

Add the following code in the MainActivity.cs in XamFromVerTrack.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. public MainPage() {  
  3.     InitializeComponent();  
  4.     VersionTracking.Track();  
  5.     lblfirstLaunch.Text = "IsFirstLaunchEver : " + VersionTracking.IsFirstLaunchEver.ToString();  
  6.     lblfirstLaunchCurrent.Text = "IsFirstLaunchForCurrentVersion : " + VersionTracking.IsFirstLaunchForCurrentVersion.ToString();  
  7.     lblfirstLaunchBuild.Text = "IsFirstLaunchForCurrentBuild : " + VersionTracking.IsFirstLaunchForCurrentBuild.ToString();  
  8.     lblcurrentVersion.Text = "CurrentVersion : " + VersionTracking.CurrentVersion.ToString();  
  9.     lblcurrentBuild.Text = "CurrentBuild : " + VersionTracking.CurrentBuild.ToString();  
  10.     if (VersionTracking.PreviousVersion != null) lblpreviousVersion.Text = "PreviousVersion : " + VersionTracking.PreviousVersion.ToString();  
  11.     if (VersionTracking.PreviousBuild != null) lblpreviousBuild.Text = "PreviousBuild : " + VersionTracking.PreviousBuild.ToString();  
  12.     lblfirstVersion.Text = "FirstInstalledVersion : " + VersionTracking.FirstInstalledVersion.ToString();  
  13.     lblfirstBuild.Text = "FirstInstalledBuild : " + VersionTracking.FirstInstalledBuild.ToString();  
  14.     foreach(string item in VersionTracking.VersionHistory)  
  15.     lblversionHistory.Text = "VersionHistory : " + item.ToString();  
  16.     foreach(string item in VersionTracking.VersionHistory)  
  17.     lblbuildHistory.Text = "BuildHistory : " + item.ToString();  
  18. }  

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:XamFromVerTrack" x:Class="XamFromVerTrack.MainPage">  
  3.     <StackLayout>  
  4.         <Label FontAttributes="Bold" Text="Version Tracking in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />  
  5.         <Label HorizontalOptions="Center" x:Name="lblfirstLaunch"></Label>  
  6.         <Label HorizontalOptions="Center" x:Name="lblfirstLaunchCurrent"></Label>  
  7.         <Label HorizontalOptions="Center" x:Name="lblfirstLaunchBuild"></Label>  
  8.         <Label HorizontalOptions="Center" x:Name="lblcurrentVersion"></Label>  
  9.         <Label HorizontalOptions="Center" x:Name="lblcurrentBuild"></Label>  
  10.         <Label HorizontalOptions="Center" x:Name="lblpreviousVersion"></Label>  
  11.         <Label HorizontalOptions="Center" x:Name="lblpreviousBuild"></Label>  
  12.         <Label HorizontalOptions="Center" x:Name="lblfirstVersion"></Label>  
  13.         <Label HorizontalOptions="Center" x:Name="lblfirstBuild"></Label>  
  14.         <Label HorizontalOptions="Center" x:Name="lblversionHistory"></Label>  
  15.         <Label HorizontalOptions="Center" x:Name="lblbuildHistory"></Label>  
  16.     </StackLayout>  
  17. </ContentPage>  

Step 7

Deploy your App in UWP and Android. The output of the XamFromVerTrack App is shown below.

Initial version app details -
 
Version Tracking in Xamarin Forms application using Xamarin Essentials for Android and UWP 
 
Rerun the App version app details,  
 
Version Tracking in Xamarin Forms application using Xamarin Essentials for Android and UWP 
 
Version Tracking in Xamarin Forms application using Xamarin Essentials for Android and UWP 
 
Change the version in package.appxmanifest in UWP.
 
Version Tracking in Xamarin Forms application using Xamarin Essentials for Android and UWP 
 
After changing the version details in UWP.
 
Version Tracking in Xamarin Forms application using Xamarin Essentials for Android and UWP 
 
Change the version in Androidmanifest.xml in Android.
 
Version Tracking in Xamarin Forms application using Xamarin Essentials for Android and UWP 
 
After changing the version details in Android.
 
Version Tracking in Xamarin Forms application using Xamarin Essentials for Android and UWP 
 
Summary

Now, you have successfully checked the version details in Xamarin.Forms application using Xamarin.Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.


Similar Articles