Get "Version Name" [Xamarin.Forms]

1

I have a About screen and would like to get the Version Name , how do I do this?

    
asked by anonymous 06.09.2018 / 15:46

2 answers

1

We need to use DependencyService to implement code for Android and iOS .

First we will create and define our Interface which will contain our method of returning the version name:

public interface IDevice
{
    string ConsultarVersao();
}

Now we will create a class in the Android project and implement our interface which in this case is called IDevice :

[assembly: Xamarin.Forms.Dependency(typeof(MeuApp.Droid.DeviceDroid))]
namespace MeuApp.Droid
{
    public class DeviceDroid : IDevice
    {
        public string ConsultarVersao()
        {
            return Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionName;
        }
    }
}

Now we will do the same for iOS , we will create a class in the project iOS and implement our interface IDevice :

[assembly: Xamarin.Forms.Dependency(typeof(MeuApp.iOS.DeviceIOS))]
namespace MeuApp.iOS
{
    public class DeviceIOS : IDevice
    {
        public string ConsultarVersao()
        {
            return NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();
        }
    }
}
Okay, it's all done. Now just use the method as follows that will work for both platforms:

DependencyService.Get<IDevice>().ConsultarVersao();

I hope I have helped.

    
06.09.2018 / 15:57
0

You can use VersionTrack.

using Xamarin.Essentials;

VersionTracking.Track();
  

After starting the Track, you can read the version information.

Below the methods:

// First time ever launched application
var firstLaunch = VersionTracking.IsFirstLaunchEver;

// First time launching current version
var firstLaunchCurrent = VersionTracking.IsFirstLaunchForCurrentVersion;

// First time launching current build
var firstLaunchBuild = VersionTracking.IsFirstLaunchForCurrentBuild;

// Current app version (2.0.0)
var currentVersion = VersionTracking.CurrentVersion;

// Current build (2)
var currentBuild = VersionTracking.CurrentBuild;

// Previous app version (1.0.0)
var previousVersion = VersionTracking.PreviousVersion;

// Previous app build (1)
var previousBuild = VersionTracking.PreviousBuild;

// First version of app installed (1.0.0)
var firstVersion = VersionTracking.FirstInstalledVersion;

// First build of app installed (1)
var firstBuild = VersionTracking.FirstInstalledBuild;

// List of versions installed (1.0.0, 2.0.0)
var versionHistory = VersionTracking.VersionHistory;

// List of builds installed (1, 2)
var buildHistory = VersionTracking.BuildHistory;

Source: Xamarin.Essentials: Version Control

    
06.09.2018 / 15:55