访问HSP包中ArkUI组件的访问与开发

HSP包中的ArkUI开发

HarmonyOS
2024-05-26 16:16:59
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
jshyb

本文主要介绍访问HSP包中ArkUI组件的访问与开发

使用的核心API

核心代码解释

通过pushUrl页面路由至HSP包中,使用@Builder装饰器修饰组件,通过isShow中的height控制弹出页面高度,transition实现转场效果。

import router from '@ohos.router'; 
import { BusinessError } from '@ohos.base'; 
  
const module = import('library/src/main/ets/pages/Index') 
  
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World' 
  
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
        // 添加按钮,以响应用户点击 
        Button() { 
          Text('click to pay') 
            .fontSize(30) 
            .fontWeight(FontWeight.Bold) 
        } 
        .type(ButtonType.Capsule) 
        .margin({ 
          top: 20 
        }) 
        .backgroundColor('#0D9FFB') 
        .width('40%') 
        .height('5%') 
        // 绑定点击事件 
        .onClick(() => { 
          router.pushUrl({url:"@bundle:com.example.test1109/library/ets/pages/Index" }) 
            .then(() => { 
            console.log("push page success"); 
          }).catch((err: BusinessError) => { 
            console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); 
          }) 
          // router.pushNamedRoute({name:"lib_page"}) 
          //   .then(() => { 
          //     console.log("push page success"); 
          //   }).catch((err: BusinessError) => { 
          //   console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); 
          // }) 
        }) 
        .width('100%') 
      } 
      .height('100%') 
    } 
  } 
} 
import { PayComponent } from '.components/PayComponent'; 
  
@Entry({routeName: 'lib_page'}) 
@Component 
export struct Index { 
  @State isShow: boolean = false 
  @State sheetHeight: number = 500; 
  @State showDragBar: boolean = true; 
  
  @Builder 
  myBuilder() { 
    Column() { 
      PayComponent({isShow: $isShow}); 
    } 
    .backgroundColor(Color.Blue) 
  } 
  
  build() { 
    Column() { 
      Button("打开二维码") 
        .onClick(() => { 
          this.isShow = true 
        }) 
        .fontSize(20) 
        .margin(10) 
        .bindSheet(this.isShow, 
          this.myBuilder(), 
          { 
            height: this.sheetHeight, 
            dragBar: this.showDragBar, 
            backgroundColor: Color.Blue, 
            onAppear: () => { 
              console.log("BindSheet onAppear.") 
            }, 
            onDisappear: () => { 
              console.log("BindSheet onDisappear.") 
            } 
          }) 
    } 
    .padding(100) 
    .backgroundColor(Color.White) 
    .width('100%') 
    .height('100%') 
  } 
} 
import curves from '@ohos.curves'; 
  
@Component 
export struct PayComponent { 
  
  @State isShowFirst: boolean = false; 
  @State firstFloorWidth: string = '100%'; 
  @State firstFloorHeight: string = '70%'; 
  
  private effect: TransitionEffect = 
  TransitionEffect.OPACITY.animation({ curve: curves.springMotion(0.3, 1) }) 
    .combine(TransitionEffect.move(TransitionEdge.BOTTOM)) 
  
  @Link isShow: boolean; 
  
  build() { 
    Row() { 
      Stack({ alignContent: Alignment.Bottom }) { 
        Stack() { 
          Column() { 
            Image($r('app.media.img')) 
              .height(200) 
              .width(200) 
  
            Button('确认') 
              .onClick(() => { 
                this.isShow = false 
              }) 
          } 
        } 
        .height(this.firstFloorHeight) 
        .width(this.firstFloorWidth) 
        .borderRadius({ 
          topLeft: 16, 
          topRight: 16 
        }) 
      } 
      .height('100%') 
      .width('100%') 
    } 
    .width('100%') 
    .height(500) 
  } 
}
  • 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.

实现效果

分享
微博
QQ
微信
回复
2024-05-27 21:43:30
相关问题
hsp动态分享库对于rawfile访问
704浏览 • 1回复 待解决
求大佬告知如何访问hsp页面?
1100浏览 • 1回复 待解决
HarmonyOS 集成态HSP如何访问rawfile资源
439浏览 • 1回复 待解决
HarmonyOS 静态共享访问问题
570浏览 • 1回复 待解决
Web组件如何访问跨域资源?
840浏览 • 1回复 待解决
HarmonyOS APP开发如何访问绝对路径
534浏览 • 1回复 待解决
Web组件访问本地资源并传递参数。
1213浏览 • 1回复 待解决
Stage模型如何申请网络访问权限
2742浏览 • 1回复 待解决