获取应用程序版本并构建

Dan*_*Dan 0 c# xamarin.ios xamarin xamarin.forms

我正在开发 Xamarin.Forms 应用程序,但 iOS 版本似乎无法正确显示。

我将其设置info.plist为版本:0.0 和构建:9,但在应用程序中它显示为版本 1.5.174 和构建 674

[assembly: Xamarin.Forms.Dependency(typeof(SocialNetwork.iOS.Version_iOS))]
namespace SocialNetwork.iOS
{
public class Version_iOS : IAppVersion
{
    public string GetVersion()
    {
        return NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();
    }
    public int GetBuild()
    {
        return int.Parse(NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString());
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

IAppVersion 很简单

public interface IAppVersion
{
    string GetVersion();
    int GetBuild();
}
Run Code Online (Sandbox Code Playgroud)

我使用得到的值

public static string AppVersion = DependencyService.Get<IAppVersion>().GetVersion();
    public static int AppBuild = DependencyService.Get<IAppVersion>().GetBuild();
Run Code Online (Sandbox Code Playgroud)

Nic*_*sky 7

共享

宣言:

public interface IYourName
{
    string GetAppVersion();
}
Run Code Online (Sandbox Code Playgroud)

用法:

 var AppVersion = DependencyService.Get<IYourName>().GetAppVersion();
Run Code Online (Sandbox Code Playgroud)

IOS

    //****************************************************
    class YourNameHelpers : IYourName
    //****************************************************
    {
        //-------------------------------------------------------------
        public string GetAppVersion()
        //-------------------------------------------------------------
        {    
            return NSBundle.MainBundle.InfoDictionary[new NSString("CFBundleVersion")].ToString();    
        }
    }
Run Code Online (Sandbox Code Playgroud)

安卓

    //****************************************************
    class YourNameHelpers : IYourName
    //****************************************************
    {
        //-------------------------------------------------------------
        public string GetAppVersion()
        //-------------------------------------------------------------
        {
            Context context = Forms.Context;
            PackageManager manager = context.PackageManager;
            PackageInfo info = manager.GetPackageInfo(context.PackageName, 0);
            return info.VersionName;
        }
    }
Run Code Online (Sandbox Code Playgroud)