2017-04-07 107 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