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
646浏览
收藏 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)
  • 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.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.

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

分享
微博
QQ
微信
回复
2024-10-15 17:32:59


相关问题
HarmonyOS window.findWindow("test")问题
623浏览 • 1回复 待解决
如何获取窗口window的宽度
2998浏览 • 1回复 待解决
如何获取当前window窗口方向
1107浏览 • 1回复 待解决
Window窗口的生命周期问题
1141浏览 • 1回复 待解决
HarmonyOS 如何拖拽窗口
568浏览 • 1回复 待解决
HarmonyOS 窗口路由切换问题
743浏览 • 1回复 待解决
HarmonyOS 窗口弹出popup问题
599浏览 • 1回复 待解决
HarmonyOS 底部横条和window路由问题
561浏览 • 1回复 待解决
HarmonyOS 窗口是否可手势移动
628浏览 • 1回复 待解决
HarmonyOS 创建窗口后相关问题
504浏览 • 1回复 待解决
HarmonyOS 创建window后横竖屏切换问题
1019浏览 • 1回复 待解决
HarmonyOS NativeXComponent获取window问题
947浏览 • 1回复 待解决