Xamarin Forms(Android):OnCreate bundle 为 null

4

我有一个Android项目,希望将API密钥存储在我的Android清单文件中。要读取它们,可以在MainActivity类的OnCreate重写中读取传递的捆绑包。如下:

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。

1个回答

0

不必尝试使用代码中的参数,可以按照以下方式检索元数据:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接