2017-04-07 93 views
2

我有一個Android項目,我希望將我的API密鑰存儲在我的Android Manifest文件中。讀他們,我可以讀,在OnCreate覆蓋來通過我的MainActivity類的包:即Xamarin Forms(Android):OnCreate bundle爲null

type MainActivity() = 
    inherit FormsApplicationActivity() 
    static let [<Literal>] MyApiKey = "my-api-key" 
    override this.OnCreate (bundle) = 
     base.OnCreate(bundle) 
     let createViewModel() = new PhotoSetViewModel() :> IRoutableViewModel 
     AppDomain.CurrentDomain.UnhandledException.AddHandler(UnhandledExceptionEventHandler(AppExceptions.processException)) 
     Forms.Init(this, bundle) 
     Xamarin.FormsMaps.Init(this, bundle) 
     // Read the API key that I've stored in AndroidManifest.xml 
     let platform = new DroidPlatform(bundle.GetString(MyApiKey)) :> ICustomPlatform 
     let app = new App<ICustomPlatform>(platform, new UiContext(this), createViewModel) 
     app.Init() 
     base.LoadApplication app 

有一個小問題。 bundle始終爲空。在我的所有項目中,C#和F#實現都發生這種情況。

我正在使用Xamarin.Forms NuGet包,版本2.3.4.224。

回答

0

,而不是試圖用bundle參數,它可以檢索元數據如下:

let metaData = PackageManager.GetApplicationInfo(PackageName, PackageInfoFlags.MetaData).MetaData 
let platform = new DroidPlatform(metaData.GetString(MyApiKey)) :> ICustomPlatform