获取应用版本和构建

时间:2018-03-23 02:59:28

标签: c# xamarin xamarin.forms xamarin.ios

我正在开发一个Xamarin.Forms应用,但iOS版本似乎没有正确显示。

我将info.plist设置为Version:0.0和Build:9,但在应用程序中显示为版本1.5.174和Build 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());
    }
  }
}

IAppVersion就是

public interface IAppVersion
{
    string GetVersion();
    int GetBuild();
}

我使用

获取值
public static string AppVersion = DependencyService.Get<IAppVersion>().GetVersion();
    public static int AppBuild = DependencyService.Get<IAppVersion>().GetBuild();

1 个答案:

答案 0 :(得分:3)

<强> SHARED

声明:

public interface IYourName
{
    string GetAppVersion();
}

用法:

 var AppVersion = DependencyService.Get<IYourName>().GetAppVersion();

<强> IOS

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

<强>的Android

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