• 【中秋国庆不断更】OpenHarmony定义扩展组件样式:@Extend装饰器


    在前文的示例中,可以使用@Styles用于样式的扩展,在@Styles的基础上,我们提供了@Extend,用于扩展原生组件样式。

    说明: 从API version 9开始,该装饰器支持在ArkTS卡片中使用。

    装饰器使用说明

    语法

    @Extend(UIComponentNamefunction functionName { ... }
    

    使用规则

    • 和@Styles不同,@Extend仅支持定义在全局,不支持在组件内部定义。
    • 和@Styles不同,@Extend支持封装指定的组件的私有属性和私有事件,以及预定义相同组件的@Extend的方法。
    1. // @Extend(Text)可以支持Text的私有属性fontColor
    2. @Extend(Text) function fancy () {
    3. .fontColor(Color.Red)
    4. }
    5. // superFancyText可以调用预定义的fancy
    6. @Extend(Text) function superFancyText(size:number) {
    7. .fontSize(size)
    8. .fancy()
    9. }
    • 和@Styles不同,@Extend装饰的方法支持参数,开发者可以在调用时传递参数,调用遵循TS方法传值调用。
    1. // xxx.ets
    2. @Extend(Text) function fancy (fontSize: number) {
    3. .fontColor(Color.Red)
    4. .fontSize(fontSize)
    5. }
    6. @Entry
    7. @Component
    8. struct FancyUse {
    9. build() {
    10. Row({ space: 10 }) {
    11. Text('Fancy')
    12. .fancy(16)
    13. Text('Fancy')
    14. .fancy(24)
    15.     }
    16.   }
    17. }
    • @Extend装饰的方法的参数可以为function,作为Event事件的句柄。
    1. @Extend(Textfunction makeMeClick(onClick: () => void) {
    2.   .backgroundColor(Color.Blue)
    3.   .onClick(onClick)
    4. }
    5. @Entry
    6. @Component
    7. struct FancyUse {
    8.   @State labelstring = 'Hello World';
    9.   onClickHandler() {
    10.     this.label = 'Hello ArkUI';
    11.   }
    12.   build() {
    13.     Row({ space10 }) {
    14.       Text(`${this.label}`)
    15.         .makeMeClick(this.onClickHandler)
    16.     }
    17.   }
    18. }
    • @Extend的参数可以为状态变量,当状态变量改变时,UI可以正常的被刷新渲染。
    1. @Extend(Text) function fancy (fontSize: number) {
    2.   .fontColor(Color.Red)
    3.   .fontSize(fontSize)
    4. }
    5. @Entry
    6. @Component
    7. struct FancyUse {
    8.   @State fontSizeValue: number = 20
    9.   build() {
    10.     Row({ space10 }) {
    11.       Text('Fancy')
    12.         .fancy(this.fontSizeValue)
    13.         .onClick(() => {
    14.           this.fontSizeValue = 30
    15.         })
    16.     }
    17.   }
    18. }

    使用场景

    以下示例声明了3个Text组件,每个Text组件均设置了fontStyle、fontWeight和backgroundColor样式。

    1. @Entry
    2. @Component
    3. struct FancyUse {
    4.   @State label: string = 'Hello World'
    5. build() {
    6. Row({ space: 10 }) {
    7. Text(`${this.label}`)
    8. .fontStyle(FontStyle.Italic)
    9. .fontWeight(100)
    10. .backgroundColor(Color.Blue)
    11. Text(`${this.label}`)
    12. .fontStyle(FontStyle.Italic)
    13. .fontWeight(200)
    14. .backgroundColor(Color.Pink)
    15. Text(`${this.label}`)
    16. .fontStyle(FontStyle.Italic)
    17. .fontWeight(300)
    18. .backgroundColor(Color.Orange)
    19.     }.margin('20%')
    20.   }
    21. }

    @Extend将样式组合复用,示例如下。

    1. @Extend(Text) function fancyText(weightValue: number, color: Color) {
    2. .fontStyle(FontStyle.Italic)
    3. .fontWeight(weightValue)
    4. .backgroundColor(color)
    5. }

    通过@Extend组合样式后,使得代码更加简洁,增强可读性。

    1. @Entry
    2. @Component
    3. struct FancyUse {
    4.   @State label: string = 'Hello World'
    5. build() {
    6. Row({ space: 10 }) {
    7. Text(`${this.label}`)
    8. .fancyText(100, Color.Blue)
    9. Text(`${this.label}`)
    10. .fancyText(200, Color.Pink)
    11. Text(`${this.label}`)
    12. .fancyText(300, Color.Orange)
    13.     }.margin('20%')
    14.   }
    15. }
  • 相关阅读:
    高手速成 | 过滤器、监听器的创建与配置
    中国五氯化磷市场调研与投资预测报告(2022版)
    AI电话机器人能否代替人工?优缺点介绍
    微软开源在线代码编辑器,编辑器天花板之Monaco Editor
    4.6、在线调试工具 ILA 的使用
    msys2 |arch pacman:tesseract ocr 安装 - 思源笔记自动调用
    荧光染料AF488 azide,AF488 叠氮化物,CAS:1679326-36-3
    Prometheus监控之SNMP Exporter介绍和数据展现
    online java
    【热门话题】Stable Diffusion:本地部署教程
  • 原文地址:https://blog.csdn.net/OpenHarmony_dev/article/details/133309761