How to get the app version from ContentPage?

时间:2017-08-04 12:10:22

标签: c# xamarin.ios xamarin.android xamarin.forms portable-class-library

I tried this: iPhone MonoTouch - Get Version of Bundle

NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString();

But this didn't work. As NSBundle can't be found.

How can I get the app version (iOS and Android) from ContentPage?

The code which i ended up with (thanks to Steven Thewissen):

PCL (shared code)

using System;
namespace MyApp.Interfaces
{
    public interface IApplicationVersion
    {
        string ApplicationsPublicVersion { get; set; }
        string ApplicationsPrivateVersion { get; set; }
    }
}

Android

using System;
using MyApp.Droid.Helpers;
using MyApp.Interfaces;
using Xamarin.Forms;

[assembly: Dependency(typeof(ApplicationVersion))]
namespace MyApp.Droid.Helpers
{
    public class ApplicationVersion : IApplicationVersion
    {
        public string ApplicationsPublicVersion { get; set; }
        public string ApplicationsPrivateVersion { get; set; }

        public ApplicationVersion()
        {
            var context = Android.App.Application.Context;
            var info = context.PackageManager.GetPackageInfo(context.PackageName, 0);

            ApplicationsPublicVersion = info.VersionName;
            ApplicationsPrivateVersion = info.VersionCode.ToString();
        }
    }
}

iOS

using System;
using MyApp.Interfaces;
using MyApp.iOS.Helpers;
using Foundation;
using Xamarin.Forms;

[assembly: Dependency(typeof(ApplicationVersion))]
namespace MyApp.iOS.Helpers
{
    public class ApplicationVersion : IApplicationVersion
    {
        public string ApplicationsPublicVersion { get; set; }
        public string ApplicationsPrivateVersion { get; set; }

        public ApplicationVersion()
        {
            ApplicationsPublicVersion = NSBundle.MainBundle.InfoDictionary[new NSString("CFBundleShortVersionString")].ToString();
            ApplicationsPrivateVersion = NSBundle.MainBundle.InfoDictionary[new NSString("CFBundleVersion")].ToString();
        }
    }
}

3 个答案:

答案 0 :(得分:13)

您可以通过实施依赖服务来完成此操作。首先,在共享代码中定义一个接口:

namespace MyApp
{
    public interface IAppVersionProvider
    {
        string AppVersion { get; }
    }
}

然后在每个平台项目中实现界面。

<强>的iOS

[assembly: Dependency(typeof(AppVersionProvider))]
namespace MyApp.iOS
{
    public class AppVersionProvider : IAppVersionProvider
    {
        public string AppVersion => NSBundle.MainBundle.InfoDictionary[new NSString("CFBundleVersion")].ToString();
    }
}

<强>的Android

[assembly: Dependency(typeof(AppVersionProvider))]
namespace MyApp.Droid
{
    public class AppVersionProvider : IAppVersionProvider
    {
        public string AppVersion
        {
            get
            {
                var context = Android.App.Application.Context;
                var info = context.PackageManager.GetPackageInfo(context.PackageName, 0);

                return $"{info.VersionName}.{info.VersionCode.ToString()}";
            }
        }
    }
}

然后,您可以通过以下方式从共享代码中检索版本号:

var version = DependencyService.Get<IAppVersionProvider>();
var versionString = version.AppVersion;

答案 1 :(得分:4)

Edit: listed incorrect nuget package, changes made below.

You should in theory be able to use something like the below inside the OnStart(); method of your App.cs in your forms project.

    Context context = this.ApplicationContext;
    SupportFunctions.Version = context.PackageManager.GetPackageInfo(context.PackageName, 0).VersionName;

However we use a plugin created by Mark Trinder called "Xam.Plugin.Version" which can be found on nuget1 and on GitHub2. Once it's installed into your forms & native projects it's simply called as so:

using Version.Plugin;

private void SomeMethod()
{
     MyLabel.Text = CrossVersion.Current.Version;
}

1 nuget package Here

2 Github Here :

答案 2 :(得分:0)

如果您不想使用依赖项服务,则可以使用类VersionTracking

属性VersionTracking.CurrentVersion将为您提供可以在Android properties和iOS info.plist中设置的版本。

该课程由Xamarin.Essentials提供,可以为您提供很多信息。请查看文档here,以了解更多信息。