#鸿蒙学习大百科#如何通过WindowStage创建一个子窗口?

如何通过WindowStage创建一个子窗口?

HarmonyOS
2024-10-22 09:17:01
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
努力向前进
// 创建子窗口
private createSubWindow(windowStage: window.WindowStage | null) {
  try {
    if (!windowStage) {
      return;
    }
    windowStage.createSubWindow('mySubWindow', (err: BusinessError, data) => {
      if (err.code) {
        console.error("Failed to create the subwindow, Cause: " + JSON.stringify(err));
        return;
      }
      this.subWindow = (data as window.Window);
      if (this.subWindow) {
        // 设置子窗口蒙层颜色
        this.initColor();
        // 设置子窗口可触
        this.subWindow.setWindowTouchable(true);
        // 设置窗口UI
        this.loadContent(entryName);
        // 展示子窗口
        this.showSubWindow();
      }
    });
  } catch (exception) {
    console.error("Failed to create the window, Cause: " + JSON.stringify(exception));
  }
}
/**
 * 设置子窗口蒙层颜色
 */
initColor() {
  try {
    // 获取指定resource对象对应的字符串
    this.maskColor = context.resourceManager.getStringSync($r('app.string.custom_dialog_mask_color').id);
    console.log("getStringSync: " + this.maskColor)
  } catch (err) {
    console.error(`getStringSync failed, code is ${err.code}, message is ${err.message}`);
  }
}
// 为当前WindowStage加载命名路由页面
private loadContent(path: string) {
  if (this.subWindow) {
    // TODO: 知识点: 用loadContentByName为当前窗口加载命名路由页面,通过LocalStorage传递状态属性给加载的页面
    this.subWindow.loadContentByName(path, this.Storage, (err: BusinessError) => {
      if (this.subWindow) {
        try {
          this.subWindow.setWindowBackgroundColor(this.maskColor);
        } catch (exception) {
          console.error('Failed to set the background color. Cause: ' + JSON.stringify(exception));
        };
      }
      if (err.code) {
        console.error("Failed to load the content. Cause:" + JSON.stringify(err));
        return;
      }
    });
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
分享
微博
QQ
微信
回复
2024-10-22 22:10:59
相关问题
销毁一个子窗口的方法
913浏览 • 1回复 待解决