如何从 ContentPage 获取应用版本?
How to get the app version from ContentPage?
我试过这个: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; }
}
}
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();
}
}
}
编辑: 列出了不正确的 nuget 包,下面进行了更改。
从理论上讲,您应该能够在 OnStart() 中使用如下所示的内容;您的表单项目中 App.cs 的方法。
Context context = this.ApplicationContext;
SupportFunctions.Version = context.PackageManager.GetPackageInfo(context.PackageName, 0).VersionName;
但是我们使用由 Mark Trinder 创建的名为 "Xam.Plugin.Version" 的插件,可以在 nuget1 和 GitHub2 上找到。一旦它被安装到您的表单和本机项目中,它就被简单地称为:
using Version.Plugin;
private void SomeMethod()
{
MyLabel.Text = CrossVersion.Current.Version;
}
1 nuget 包 Here
2 Github Here :
您可以通过实施依赖服务来做到这一点。首先,您在共享代码中定义一个接口:
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;
如果不想使用依赖服务,可以直接使用class VersionTracking
.
属性 VersionTracking.CurrentVersion
将为您提供可以在 Android properties
和 iOS info.plist
中设置的版本。
这个class由Xamarin.Essentials
提供,可以给你很多信息。请查看文档 here 以获取更多信息。
我试过这个: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; }
}
}
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();
}
}
}
编辑: 列出了不正确的 nuget 包,下面进行了更改。
从理论上讲,您应该能够在 OnStart() 中使用如下所示的内容;您的表单项目中 App.cs 的方法。
Context context = this.ApplicationContext;
SupportFunctions.Version = context.PackageManager.GetPackageInfo(context.PackageName, 0).VersionName;
但是我们使用由 Mark Trinder 创建的名为 "Xam.Plugin.Version" 的插件,可以在 nuget1 和 GitHub2 上找到。一旦它被安装到您的表单和本机项目中,它就被简单地称为:
using Version.Plugin;
private void SomeMethod()
{
MyLabel.Text = CrossVersion.Current.Version;
}
1 nuget 包 Here
2 Github Here :
您可以通过实施依赖服务来做到这一点。首先,您在共享代码中定义一个接口:
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;
如果不想使用依赖服务,可以直接使用class VersionTracking
.
属性 VersionTracking.CurrentVersion
将为您提供可以在 Android properties
和 iOS info.plist
中设置的版本。
这个class由Xamarin.Essentials
提供,可以给你很多信息。请查看文档 here 以获取更多信息。