Get "Version Name" [Xamarin.Forms]

Asked

Viewed 246 times

2

I have a screen About and I wish I could take the Version Name, how to do this?

inserir a descrição da imagem aqui

2 answers

1


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

First we will create and define our Interface which will contain our return version name method:

public interface IDevice
{
    string ConsultarVersao();
}

Now we will create a class in the project Android 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’ll do the same for the 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’ve helped.

0

You can use Versiontrack.

using Xamarin.Essentials;

VersionTracking.Track();

After starting Track, you can read the version information.

Down with 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

  • 1

    I tried to use and it did not work, but thank you, with the other answer I got, thank you.

  • Okay, I’m still sorry.

  • No, I appreciate your interest in helping.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.