HarmonyOS UIContext获取

在入口中获取UIContext上下文时,在启时崩溃(偶发)。是用法不对吗?

HarmonyOS UIContext获取-鸿蒙开发者社区

HarmonyOS
2024-12-18 16:47:02
746浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

loadcontent和getMainWindow两个接口是异步的,getUIContext是同步接口。loadcontent和getMainWindow会同时执行但是回调时序不保证,就会有先getMainWindow拿到结果,但此时content没有完成加载的情况。loadcontent和getMainWindow两个接口是异步的,最好是再Loadcontent的回调中调用接口。保证时序。推荐示例:

onWindowStageCreate(windowStage: window.WindowStage) {
  // Main window is created, set main page for this ability
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

  windowStage.loadContent('pages/Index', (err, data) => {
    if (err.code) {
      hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
      return;
    }

    windowStage.getMainWindow((err: BusinessError, data) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error('Failed to obtain the main window.Cause:' + JSON.stringify(err))
      }
      let uiContext = data.getUIContext();
      console.log(JSON.stringify(uiContext))
    })
    hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
  });
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
分享
微博
QQ
微信
回复
2024-12-18 18:11:08


相关问题
获取UIContext报错1300002如何解决
3344浏览 • 1回复 待解决
HarmonyOS UIContext()问题
665浏览 • 1回复 待解决
HarmonyOS获取签名信息获取失败
952浏览 • 1回复 待解决
HarmonyOS 获取时区
555浏览 • 1回复 待解决