HarmonyOS 创建子窗口时,如何向子窗口传递页面参数

HarmonyOS
8h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

参考demo:

EntryAbility.ets

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

  windowStage.loadContent('pages/Index', (err) => {
  if (err.code) {
  hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
  return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
// 给Index页面传递windowStage
AppStorage.setOrCreate('windowStage', windowStage);
});
}

Index.ets

import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
let windowStage_: window.WindowStage | undefined = undefined;
let sub_windowClass: window.Window | undefined = undefined;
@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  storage: LocalStorage = new LocalStorage();

  private CreateSubWindow(){
    // 获取windowStage
    windowStage_ = AppStorage.get('windowStage');
    // 1.创建应用子窗口。
    if (windowStage_ == null) {
      console.error('Failed to create the subwindow. Cause: windowStage_ is null');
    }
    else {
      windowStage_.createSubWindow("mySubWindow", (err: BusinessError, data) => {
        let errCode: number = err.code;
        if (errCode) {
          console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err));
          return;
        }
        sub_windowClass = data;
        console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
        // 2.子窗口创建成功后,设置子窗口的位置、大小及相关属性等。
        sub_windowClass.moveWindowTo(300, 300, (err: BusinessError) => {
          let errCode: number = err.code;
          if (errCode) {
            console.error('Failed to move the window. Cause:' + JSON.stringify(err));
            return;
          }
          console.info('Succeeded in moving the window.');
        });
        sub_windowClass.resize(500, 500, (err: BusinessError) => {
          let errCode: number = err.code;
          if (errCode) {
            console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
            return;
          }
          console.info('Succeeded in changing the window size.');
        });
        // 3.为子窗口加载对应的目标页面。
        this.storage.setOrCreate('storageSimpleProp', 121);
        sub_windowClass.loadContent("pages/SubWindow", this.storage, (err: BusinessError) => {
          let errCode: number = err.code;
          if (errCode) {
            console.error('Failed to load the content. Cause:' + JSON.stringify(err));
            return;
          }
          console.info('Succeeded in loading the content.');
          // 3.显示子窗口。
          (sub_windowClass as window.Window).showWindow((err: BusinessError) => {
            let errCode: number = err.code;
            if (errCode) {
              console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
              return;
            }
            console.info('Succeeded in showing the window.');
          });
        });
      })
    }
  }
  private destroySubWindow(){
    // 4.销毁子窗口。当不再需要子窗口时,可根据具体实现逻辑,使用destroy对其进行销毁。
    (sub_windowClass as window.Window).destroyWindow((err: BusinessError) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err));
        return;
      }
      console.info('Succeeded in destroying the window.');
    });
  }
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button(){
          Text('CreateSubWindow')
            .fontSize(24)
            .fontWeight(FontWeight.Normal)
        }.width(220).height(68)
        .margin({left:10, top:60})
        .onClick(() => {
          this.CreateSubWindow()
        })
        Button(){
          Text('destroySubWindow')
            .fontSize(24)
            .fontWeight(FontWeight.Normal)
        }.width(220).height(68)
        .margin({left:10, top:60})
        .onClick(() => {
          this.destroySubWindow()
        })
        Image($rawfile('[library2].mmm.png'))
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
6h前
相关问题
窗口加载的页面是否可以带参数
420浏览 • 1回复 待解决
HarmonyOS 窗口跳转页面的返回问题
254浏览 • 1回复 待解决
如何实现一个页面显示窗口
675浏览 • 1回复 待解决
HarmonyOS 窗口路由切换问题
130浏览 • 1回复 待解决
arkTS无法创建窗口有了解的吗?
2245浏览 • 0回复 待解决
如何通过代码关闭窗口
411浏览 • 1回复 待解决
求告知窗口如何添加动画
418浏览 • 1回复 待解决
HarmonyOS 窗口是否可手势移动
0浏览 • 0回复 待解决
如何设置窗口的背景颜色?
375浏览 • 1回复 待解决
HarmonyOS window.findWindow获取窗口错误
416浏览 • 1回复 待解决
HarmonyOS api10如何窗口设置圆角
396浏览 • 1回复 待解决
设置窗口透明度未生效
1714浏览 • 1回复 待解决
应用窗口的开发流程是什么
1922浏览 • 1回复 待解决
如何生成一个可以交互的移动窗口
916浏览 • 1回复 待解决