【问题标题】:Get App Version and Build获取应用版本和构建
【发布时间】:2018-03-23 14:00:51
【问题描述】:

我正在开发 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());
    }
  }
}

IAppVersion 很简单

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

我使用

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

【问题讨论】:

  • 确保从模拟器/设备中删除调试应用程序并再次执行清理/构建/调试会话。 (注意:CFBundleVersion 应该是点通知(1.2.3)中的 3 个数字
  • @SushiHangover 我正在使用 Xamarin Live Player 在我的 iPhone 上对其进行测试,因此每次都很干净。
  • Xamarin Live Player 将使用自己的 info.plist,在没有实时播放器的模拟器/设备上对其进行调试....

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


【解决方案1】:

共享

声明:

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

安卓

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

【讨论】:

    猜你喜欢
    • 2019-05-09
    • 2020-06-24
    • 2017-02-01
    • 2014-08-21
    • 2019-03-04
    • 1970-01-01
    • 2011-09-29
    • 2018-07-29
    相关资源
    最近更新 更多