在HarmonyOS NEXT开发中创建子窗口时,如何向子窗口传递页面参数?

在HarmonyOS NEXT开发中创建子窗口时,如何向子窗口传递页面参数?

阅读 1.2k
1 个回答

参考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%') 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进