获取MvxApplication的版本号/名称

时间:2014-09-08 15:14:47

标签: mvvmcross

有没有办法在MvxApplication中自动获取版本号和版本名称,或者我们必须为每个平台编写代码?

2 个答案:

答案 0 :(得分:4)

我没有看到用于获取跨平台应用版本号的现有插件,但它不应该难以编写。

以下是如何获取每个平台的当前版本号。我确定你可以编写类似的代码来获取内部版本号和应用名称。

Windows Phone

public static string GetVersion()
{
    var versionAttribute = Assembly.GetExecutingAssembly()
        .GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true)
        .FirstOrDefault() as AssemblyFileVersionAttribute;

    return versionAttribute != null ? versionAttribute.Version : "";
}

<强>的Android

public static string GetVersion()
{
    var context = Application.Context;
    var info = context.PackageManager.GetPackageInfo(context.PackageName, 0);
    return info.VersionName;
}

<强>的iOS

public static string GetVersion()
{
    NSObject version = NSBundle.MainBundle.InfoDictionary["CFBundleShortVersionString"];
    return version.ToString();
}

答案 1 :(得分:1)

更详细的android信息:

public class InfoService : IInfoService
{
    public string DeviceId
    {
        get
        {                
            return Settings.Secure.GetString(Application.Context.ContentResolver, Settings.Secure.AndroidId);
        }
    }

    public string PackageName
    {
        get
        {
            return Application.Context.PackageName;
        }
    }

    public string AppVersionName
    {
        get
        {
            var context = Application.Context;
            var info = context.PackageManager.GetPackageInfo(context.PackageName, 0);

            return info.VersionName;
        }
    }

    public int AppVersionCode
    {
        get
        {
            var context = Application.Context;
            var info = context.PackageManager.GetPackageInfo(context.PackageName, 0);

            return info.VersionCode;
        }
    }

    public double DeviceScreenWidth
    {
        get
        {
            var context = Application.Context;
            var displayMetrics = context.Resources.DisplayMetrics;

            return displayMetrics.WidthPixels / displayMetrics.Density;
        }
    }

    public double DeviceScreenHeight
    {
        get
        {
            var context = Application.Context;
            var displayMetrics = context.Resources.DisplayMetrics;

            return displayMetrics.HeightPixels / displayMetrics.Density;
        }
    }
}