如何在HSP模块切换根视图

如何在HSP模块切换根视图,是否需要在HSP模块拿到程序入口的WindowStage进行切换,具体怎么实现?

HarmonyOS
2024-07-21 18:54:11
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
QW_MA

在HSP页面中,通过windowStage对象的getMainWindowSync接口获取主窗口,然后使用pushUrl跳转到跟页面,在跟页面通过router.pushNamedRoute切换到HSP页面。具体请参考示例代码:

HspPage页面Index代码:

import { window } from '@kit.ArkUI'; 
 
@Entry({ routeName: "HspPage" }) 
@Component 
struct Index { 
  @State message: string = 'This is Hsp Page'; 
  @State windowStage: window.WindowStage | undefined = AppStorage.get<window.WindowStage>("windowStage") 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            if (this.windowStage) { 
              this.windowStage.getMainWindowSync() 
                .getUIContext() 
                .getRouter() 
                .pushUrl({ 
                  url: "pages/Page" 
                }) 
            } 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}

根页面page1

import { router } from '@kit.ArkUI'; 
 
import("lib1/src/main/ets/pages/Index") 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
 
  build() { 
    RelativeContainer() { 
      Text(this.message) 
        .id('HelloWorld') 
        .fontSize(50) 
        .fontWeight(FontWeight.Bold) 
        .alignRules({ 
          center: { anchor: 'container', align: VerticalAlign.Center }, 
          middle: { anchor: 'container', align: HorizontalAlign.Center } 
        }) 
        .onClick(() => { 
          console.log("clicked") 
          router.pushNamedRoute({ 
            name: "HspPage" 
          }) 
        }) 
    } 
    .height('100%') 
    .width('100%') 
  } 
}

根页面page2

import { router } from '@kit.ArkUI'; 
 
import("lib1/src/main/ets/pages/Index") 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
 
  build() { 
    RelativeContainer() { 
      Text(this.message) 
        .id('HelloWorld') 
        .fontSize(50) 
        .fontWeight(FontWeight.Bold) 
        .alignRules({ 
          center: { anchor: 'container', align: VerticalAlign.Center }, 
          middle: { anchor: 'container', align: HorizontalAlign.Center } 
        }) 
        .onClick(() => { 
          console.log("clicked") 
          router.pushNamedRoute({ 
            name: "HspPage" 
          }) 
        }) 
    } 
    .height('100%') 
    .width('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-07-22 11:01:41
相关问题
HSP如何感知前后台切换
1777浏览 • 1回复 待解决
hsp模块如何加载悬浮窗页面
1617浏览 • 1回复 待解决
如何在组件中监听App前后台切换
36浏览 • 0回复 待解决
无法读取到hsp模块中的rawfile文件
1627浏览 • 1回复 待解决
请问如何获取到鸿蒙的布局
6490浏览 • 1回复 待解决