HarmonyOS window.findWindow获取子窗口错误

1. 创建子窗口1 winStage.createSubWindow(‘window1’)。

2. 创建子窗2 winStage.createSubWindow(‘window2’)。

3. 这个时候let windows = await winStage.getSubWindow()能查看到以上两个subwindow,但是问题在于:windows?.find(() => window.findWindow(‘window1’)),返回window2,预期是window1。

HarmonyOS
2024-10-15 12:20:56
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

以下是本地测试demo,可供参考:

//src/main/ets/entryability/EntryAbility.ets  
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';  
import { hilog } from '@kit.PerformanceAnalysisKit';  
import { window } from '@kit.ArkUI';  
import { BusinessError } from '@kit.BasicServicesKit';  
let windowStage_: window.WindowStage | null = null;  
let sub_windowClass: window.Window | null = null;  
export default class EntryAbility extends UIAbility {  
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {  
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');  
  }  
  onDestroy(): void {  
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');  
  }  
  showSubWindow() {  
    // 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.为子窗口加载对应的目标页面。  
        sub_windowClass.setUIContent("pages/subPage", (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.');  
          });  
        });  
      })  
    }  
  }  
  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.');  
    });  
  }  
  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, data) => {  
      if (err.code) {  
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');  
        return;  
      }  
      AppStorage.setOrCreate("windowStage", windowStage);  
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');  
    });  
  }  
  onWindowStageDestroy(): void {  
    // Main window is destroyed, release UI related resources  
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');  
  }  
  onForeground(): void {  
    // Ability has brought to foreground  
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');  
  }  
  onBackground(): void {  
    // Ability has back to background  
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');  
  }  
}
//index.ets  
import { window } from '@kit.ArkUI';  
@Entry  
@Component  
struct Index {  
  @State message: string = '获取子窗口2';  
  @State message1: string = '创建子窗口1';  
  @State message2: string = '创建子窗口2';  
  @State windowStage: window.WindowStage = AppStorage.get("windowStage") as window.WindowStage  
  build() {  
    Row() {  
      Column() {  
        Text(this.message1)  
          .fontSize(50)  
          .fontWeight(FontWeight.Bold)  
          .onClick(() => {  
            this.windowStage.createSubWindow("window1", (err, windowClass) => {  
              let subWindowID: number = windowClass.getWindowProperties().id  
              console.log('window1',subWindowID)  
              if (err.code > 0) {  
                console.error("failed to create subWindow Cause:" + err.message)  
                return;  
              }  
              // 设置子窗口加载页  
              try {  
                windowClass.setUIContent("pages/subPage", () => {  
                  windowClass.setWindowBackgroundColor("#00000000")  
                });  
                // 设置子窗口左上角坐标  
                windowClass.moveWindowTo(0, 200)  
                // 设置子窗口大小  
                windowClass.resize(vp2px(300), vp2px(300))  
                // 展示子窗口  
                windowClass.showWindow();  
                // 设置子窗口全屏化布局不避让安全区  
                windowClass.setWindowLayoutFullScreen(true);  
              } catch (err) {  
                console.error("failed to create subWindow Cause:" + err)  
              }  
            })  
          })  
        Text(this.message2)  
          .fontSize(50)  
          .fontWeight(FontWeight.Bold)  
          .onClick(() => {  
            this.windowStage.createSubWindow("window2", (err, windowClass) => {  
              let subWindowID: number = windowClass.getWindowProperties().id  
              console.log('window2',subWindowID)  
              if (err.code > 0) {  
                console.error("failed to create subWindow Cause:" + err.message)  
                return;  
              }  
              // 设置子窗口加载页  
              try {  
                windowClass.setUIContent("pages/subPageTwo", () => {  
                  windowClass.setWindowBackgroundColor("#00000000")  
                });  
                // 设置子窗口左上角坐标  
                windowClass.moveWindowTo(0, 500)  
                // 设置子窗口大小  
                windowClass.resize(vp2px(300), vp2px(300))  
                // 展示子窗口  
                windowClass.showWindow();  
                // 设置子窗口全屏化布局不避让安全区  
                windowClass.setWindowLayoutFullScreen(true);  
              } catch (err) {  
                console.error("failed to create subWindow Cause:" + err)  
              }  
            })  
          })  
        Text(this.message)  
          .fontSize(50)  
          .fontWeight(FontWeight.Bold)  
          .onClick(() => {  
            let subWindowID: number = window.findWindow("window2").getWindowProperties().id  
            console.log('window2',subWindowID)  
          })  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}
//SubPage.ets  
@Entry  
@Component  
struct SubPage {  
  @State message: string = 'SUBPAGE';  
  build() {  
    Row() {  
      Column() {  
        Text(this.message)  
          .fontSize(20)  
          .fontWeight(FontWeight.Bold)  
      }  
      .width('100%')  
    }  
    .height('100%')  
    .backgroundColor(Color.Red)  
  }  
}
//SubPageTwo.ets  
@Entry  
@Component  
struct SubPageTwo{  
  @State message: string = 'SubPageTwo';  
  
  build() {  
    Row() {  
      Column() {  
        Text(this.message)  
          .fontSize(50)  
          .fontWeight(FontWeight.Bold)

上述demo中创建两个子窗口并获得对应id,点击获取子窗口2获得的id与创建window2的id相同,window.findWindow("window2")改成window1获得的id与创建window1的id相同。

分享
微博
QQ
微信
回复
2024-10-15 17:32:59
相关问题
HarmonyOS window.findWindow("test")问题
14浏览 • 1回复 待解决
如何获取窗口window的宽度
2016浏览 • 1回复 待解决
如何获取当前window窗口方向
465浏览 • 1回复 待解决
Window窗口的生命周期问题
347浏览 • 1回复 待解决
HarmonyOS 如何拖拽窗口
10浏览 • 1回复 待解决
HarmonyOS 窗口路由切换问题
166浏览 • 1回复 待解决
HarmonyOS 窗口弹出popup问题
3浏览 • 0回复 待解决
HarmonyOS 底部横条和window路由问题
70浏览 • 1回复 待解决
HarmonyOS 窗口是否可手势移动
91浏览 • 1回复 待解决
如何通过代码关闭窗口
433浏览 • 1回复 待解决
HarmonyOS 窗口页面返回事件无效
45浏览 • 1回复 待解决
HarmonyOS窗口获取
17浏览 • 1回复 待解决