1、首先,定义一个方法类,写展示方法与关闭方法

        方法类写法参考官网管理应用窗口-窗口-基础能力开发-元服务开发指导-HarmonyOS元服务 - 华为HarmonyOS开发者https://developer.huawei.com/consumer/cn/doc/atomic-guides/atomic-application-window#%E5%BC%80%E5%8F%91%E6%AD%A5%E9%AA%A4-1

import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';


class MyWindows {
  public  windowStage_: window.WindowStage | null = null;
  public  sub_windowClass: window.Window | null = null;


  showSubWindow() {
    // 1.创建应用子窗口。
    if (this.windowStage_ == null) {
      console.error('Failed to create the subwindow. Cause: windowStage_ is null');
    } else {
      this.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;
        }
        this.sub_windowClass = data;
        console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
        // 2.子窗口创建成功后,设置子窗口的位置、大小及相关属性等。
        this.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.');
        });
        this.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.sub_windowClass.setUIContent("pages/small_page", (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.显示子窗口。
          (this.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对其进行销毁。
    (this.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.');
    });
  }
}

export const MyWindow = new MyWindows()

2、使用前在【EntryAbility】中初始化

export default class EntryAbility extends UIAbility {
  ......
  onWindowStageCreate(windowStage: window.WindowStage): void {
    ......
    MyWindow.windowStage_ = windowStage
  }
  ......
}

3、调用

      Button('点击打开小窗')
        .onClick(()=>{
          MyWindow.showSubWindow()
        })
=============================================
      Button('点击关闭小窗')
        .onClick(()=>{
          MyWindow.destroySubWindow()
        })

4、实现点击小窗口,跳转主页面

      Button('主窗口跳转')
        .onClick(() => {
          MyWindow.windowStage_?.getMainWindowSync()
            .getUIContext()
            .getRouter()
            .pushUrl({ url: "pages/routerPage" })
        })
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐