如何设置应用子窗口,并对其进行属性设置等操作

按需创建应用子窗口,如弹窗等,并对其进行属性设置等操作。


HarmonyOS
2024-05-26 14:02:28
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
e_lion

使用的核心API

窗口管理

核心代码解释

onWindowStageCreate(windowStage: window.WindowStage) { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
  
    // 将自定义数据变量“data”存入AppStorage,api10 
    AppStorage.setOrCreate('data', 5); 
    AppStorage.setOrCreate('window', windowStage); 
  
    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; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 
    }); 
  } 
import window from '@ohos.window'; 
import router from '@ohos.router'; 
  
@Entry 
@Component 
struct Page { 
  @State message: string = 'Hello World' 
  // 使用@StorageLink将"mainData"与AppStorage中的变量"data"进行双向绑定 
  @StorageLink('data') mainData: number = 1; 
  @StorageLink('window') StorageWindow:window.WindowStage | null = null 
  private windowStage_ = this.StorageWindow 
  sub_windowClass: window.Window | null = null; 
  
  build() { 
    Row() { 
      Column() { 
        Text('开启子窗口') 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            this.showSubWindow() 
          }) 
        Text('index里数据:'+this.mainData) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
        Text('关闭子窗口') 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            this.destroySubWindow() 
          }) 
        Button('页面跳转') 
          .onClick(()=>{ 
            router.pushUrl({ 
              url:'pages/Index3' 
            }) 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
  
  showSubWindow() { 
    // 1.创建应用子窗口。 
    if (this.windowStage_ == null) { 
      console.error('Failed to create the subwindow. Cause: windowStage_ is null'); 
    } 
    else { 
      this.windowStage_.createSubWindow("mySubWindow", (err, data) => { 
        let errCode: number = err.code; 
        if (errCode) { 
          console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err)); 
          return; 
        } 
        this.sub_windowClass = data; 
        console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data)); 
        // 2.子窗口创建成功后,设置子窗口的位置、大小及相关属性等。 
        this.sub_windowClass.moveWindowTo(300, 300, (err) => { 
          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.'); 
        }); 
        this.sub_windowClass.resize(500, 500, (err) => { 
          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.sub_windowClass.setUIContent("pages/Index2", (err) => { 
          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.显示子窗口。 
          (this.sub_windowClass as window.Window).showWindow((err) => { 
            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对其进行销毁。 
    (this.sub_windowClass as window.Window).destroyWindow((err) => { 
      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.'); 
    }); 
  } 
} 
@Entry 
@Component 
struct Index2 { 
  @StorageLink('data') mainData: number = 1; 
  
  build() { 
    Row() { 
      Column() { 
        Text('index2里数据:'+this.mainData) 
          .fontSize(20) 
          .fontWeight(FontWeight.Bold) 
        Button('修改') 
          .onClick(()=>{ 
            this.mainData++ 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
  • 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.

实现效果

适配的版本信息

IDE:DevEco Studio 4.0.3.600

SDK:HarmoneyOS 4.0.10.11


分享
微博
QQ
微信
回复
2024-05-27 16:55:41
相关问题
如何设置窗口的背景颜色?
914浏览 • 1回复 待解决
HarmonyOS api10如何窗口设置圆角
1164浏览 • 1回复 待解决
设置窗口透明度未生效
2233浏览 • 1回复 待解决
HarmonyOS 窗口如何设置成横屏
768浏览 • 1回复 待解决
如何设置窗口最小宽度
3087浏览 • 1回复 待解决
基于PhotoViewPicker图片进行操作
1809浏览 • 1回复 待解决
基于CameraKit相机进行操作
1561浏览 • 1回复 待解决
如何设置窗口启动图片
2614浏览 • 1回复 待解决