c# - 如何从 ContentPage 获取应用版本?

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

我试过这个:iPhone MonoTouch - Get Version of Bundle

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

但这没有用。因为找不到 NSBundle

如何从 ContentPage 获取应用程序版本(iOS 和 Android)?

我最终得到的代码(感谢 Steven Thewissen):

PCL(共享代码)

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

安卓

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();
        }
    }
}

最佳答案

您可以通过实现依赖服务来做到这一点。首先,您在共享代码中定义一个接口(interface):

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

然后在每个平台项目中实现接口(interface)。

iOS

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

安卓

[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;

关于c# - 如何从 ContentPage 获取应用版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45506496/

相关文章:

ios - 将图片存储在设备上

android - 带有 AndroidSDK API 19 及更低版本的 Xamarin.Android 的放大 float 操作按钮的阴影填充错误

c# - 使用 services.AddHttpClient 时,HttpClient 是在哪里创建的?

c# - 来自 subview 的 MainWindow ViewModel 引用

ios - uitableview 返回 2 cellIdentifier

c# - 如何在 C# 中使用 Xamarin 中的 CHECK_VOICE_DATA_PASS?

android - Xamarin.Android 安卓 :versionCode android:versionName - From Jenkins Build Server

c# - 网络核心 : how to make the browser show file as being downloaded before it's sent

c# - 在 C# 应用程序之间流式传输数据

ios - 是否有与 Java 的 GlyphVector 或 .NET 的 GraphicsPath 等效的 Xamarin.iOS?