1

我嘗試這樣做:iPhone MonoTouch - Get Version of Bundle如何從ContentPage獲取應用程序版本?

NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString(); 

但這並沒有工作。由於找不到NSBundle

如何從ContentPage獲取應用程序版本(iOS和Android)?

其中我結束了(由於史蒂芬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(); 
     } 
    } 
} 

回答

6

你可以做到這一點實施依賴服務。首先在共享代碼中定義一個接口:

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; 
+0

像魅力一樣工作我不得不使用構造函數而不是屬性不知道爲什麼它不能與屬性一起工作(我將它添加到了我的問題)。謝謝!:) – JedatKinports

3

編輯:列出了不正確的nuget軟件包,更改如下。

你理論上應該能夠在OnStart()中使用類似下面的東西;您的表單項目中的App.cs方法。

Context context = this.ApplicationContext; 
    SupportFunctions.Version = context.PackageManager.GetPackageInfo(context.PackageName, 0).VersionName; 

但是我們用馬克的Trinder創建一個插件叫做 「Xam.Plugin.Version」,它可以在的NuGet 並在GitHub上被發現。一旦安裝到您的形式&本地項目它簡稱爲這樣:

using Version.Plugin; 

private void SomeMethod() 
{ 
    MyLabel.Text = CrossVersion.Current.Version; 
} 

NuGet包Here

Github上Here

+0

我不相信的NuGet有i中的任何此類功能噸。如果是這樣,我很樂意看到它的一段代碼:) –

+1

這不適合我。 「'錯誤:應用程序。OnCreate()':沒有找到合適的方法來覆蓋「 – JedatKinports

+0

@JedatKinports道歉我衝過去發佈該答案並交叉引用了一些不正確的細節,已編輯 – Digitalsa1nt

相關問題