美文网首页
HarmonyOS UI范式-基本语法

HarmonyOS UI范式-基本语法

作者: Aaron升 | 来源:发表于2023-12-28 17:49 被阅读0次

    参考文档

    https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V4/2_4ui_u8303_u5f0f-0000001637400294-V4

    目录

    • 基本语法概述
    • 声明式UI描述
    • 创建自定义组件
    • 页面和自定义组件生命周期
    • 装饰器

    基本语法概述

    我们用一个具体的示例来说明ArkTS的基本组成。如下图所示,当开发者点击按钮时,文本内容从“Hello World”变为“Hello ArkUI”:

    本示例中,ArkTS的基本组成如下所示:

    • 装饰器: 用于装饰类、结构、方法以及变量,并赋予其特殊的含义。如上述示例中@Entry、@Component和@State都是装饰器,@Component表示自定义组件,@Entry表示该自定义组件为入口组件,@State表示组件中的状态变量,状态变量变化会触发UI刷新。

    • UI描述:以声明式的方式来描述UI的结构,例如build()方法中的代码块。

    • 自定义组件:可复用的UI单元,可组合其他组件,如上述被@Component装饰的struct Hello。

    • 系统组件:ArkUI框架中默认内置的基础和容器组件,可直接被开发者调用,比如示例中的Column、Text、Divider、Button。

    • 属性方法:组件可以通过链式调用配置多项属性,如fontSize()、width()、height()、backgroundColor()等。

    • 事件方法:组件可以通过链式调用设置多个事件的响应逻辑,如跟随在Button后面的onClick()。

    • 系统组件、属性方法、事件方法具体使用可参考基于ArkTS的声明式开发范式

    声明式UI描述

    ArkTS以声明方式组合和扩展组件来描述应用程序的UI,同时还提供了基本的属性、事件和子组件配置方法,帮助开发者实现应用交互逻辑。

    创建组件

    根据组件构造方法的不同,创建组件包含有参数和无参数两种方式。

    Column() {
      // string类型的参数
      Text('item 1')
      // 无参数
      Divider()
      // $r形式引入应用资源,可应用于多语言场景
      Text($r('app.string.title_value'))
      // 使用变量作为参数
      Image(this.imagePath)
      // 使用表达式作为参数
      Image('https://' + this.imageUrl)
    }
    

    配置属性

    属性方法以“.”链式调用的方式配置系统组件的样式和其他属性,建议每个属性方法单独写一行。

    Text('test')
      // 配置字体大小
      .fontSize(12)
      // 使用预定义的枚举作为参数
      .fontColor(Color.Red)
    Image('test.jpg')
      // 使用变量或表达式作为参数
      .width(this.count % 2 === 0 ? 100 : 200)
      .height(this.offset)
    

    配置事件

    使用匿名函数表达式配置组件的事件方法,要求使用“() => {...}”,以确保函数与组件绑定,同时符合ArtTS语法规范。

    Button('Click me')
      .onClick(() => {
        this.myText = 'ArkUI';
      })
    

    使用组件的成员函数配置组件的事件方法。

    myClickHandler(): void {
      this.myText = 'ArkUI';
    }
    ...
    Button('add counter')
      .onClick(this.myClickHandler)
    

    配置子组件

    容器组件均支持子组件配置,可以实现相对复杂的多级嵌套。
    Column、Row、Stack、Grid、List等组件都是容器组件。

    Column() {
      Text('Hello')
        .fontSize(100)
      Row() {
        Image('test1.jpg')
          .width(100)
          .height(100)
        Button('click +1')
          .onClick(() => {
            console.info('+1 clicked!');
          })
      }
    }
    

    创建自定义组件

    在ArkUI中,UI显示的内容均为组件,由框架直接提供的称为系统组件,由开发者定义的称为自定义组件。在进行 UI 界面开发时,通常不是简单的将系统组件进行组合使用,而是需要考虑代码可复用性、业务逻辑与UI分离,后续版本演进等因素。因此,将UI和部分业务逻辑封装成自定义组件是不可或缺的能力。

    自定义组件具有以下特点:

    • 可组合:允许开发者组合使用系统组件、及其属性和方法。
    • 可重用:自定义组件可以被其他组件重用,并作为不同的实例在不同的父组件或容器中使用。
    • 数据驱动UI更新:通过状态变量的改变,来驱动UI的刷新。

    以下示例展示了自定义组件的基本用法。

    @Component
    struct HelloComponent {
      @State message: string = 'Hello, World!';
    
      build() {
        // HelloComponent自定义组件组合系统组件Row和Text
        Row() {
          Text(this.message)
            .onClick(() => {
              // 状态变量message的改变驱动UI刷新,UI从'Hello, World!'刷新为'Hello, ArkUI!'
              this.message = 'Hello, ArkUI!';
            })
        }
      }
    }
    

    HelloComponent可以在其他自定义组件中的build()函数中多次创建,实现自定义组件的重用。

    自定义组件的基本结构
    • struct:自定义组件基于struct实现,struct + 自定义组件名 + {...}的组合构成自定义组件,不能有继承关系。对于struct的实例化,可以省略new。
    • @Component:@Component装饰器仅能装饰struct关键字声明的数据结构。struct被@Component装饰后具备组件化的能力,需要实现build方法描述UI,一个struct只能被一个@Component装饰。
    • @Entry:@Entry装饰的自定义组件将作为UI页面的入口。在单个UI页面中,最多可以使用@Entry装饰一个自定义组件。@Entry可以接受一个可选的LocalStorage的参数。

    自定义组件的参数规定

    我们可以在build方法或者@Builder装饰的函数里创建自定义组件,并传入参数。

    @Component
    struct MyComponent {
      private countDownFrom: number = 0;
      private color: Color = Color.Blue;
    
      build() {
      }
    }
    
    @Entry
    @Component
    struct ParentComponent {
      private someColor: Color = Color.Pink;
    
      build() {
        Column() {
          // 创建MyComponent实例,并将创建MyComponent成员变量countDownFrom初始化为10,将成员变量color初始化为this.someColor
          MyComponent({ countDownFrom: 10, color: this.someColor })
        }
      }
    }
    

    build()函数

    所有声明在build()函数的语言,我们统称为UI描述语言,UI描述语言需要遵循以下规则:

    • @Entry装饰的自定义组件,其build()函数下的根节点唯一且必要,且必须为容器组件,其中ForEach禁止作为根节点。
      @Component装饰的自定义组件,其build()函数下的根节点唯一且必要,可以为非容器组件,其中ForEach禁止作为根节点。
    @Entry
    @Component
    struct MyComponent {
      build() {
        // 根节点唯一且必要,必须为容器组件
        Row() {
          ChildComponent()
        }
      }
    }
    
    @Component
    struct ChildComponent {
      build() {
        // 根节点唯一且必要,可为非容器组件
        Image('test.jpg')
      }
    }
    
    • 不允许声明本地变量,反例如下。
    build() {
      // 反例:不允许声明本地变量
      let a: number = 1;
    }
    
    • 不允许在UI描述里直接使用console.info,但允许在方法或者函数里使用,反例如下。
    build() {
      // 反例:不允许console.info
      console.info('print debug log');
    }
    
    • 不允许创建本地的作用域,反例如下。
    build() {
      // 反例:不允许本地作用域
      {
        ...
      }
    }
    
    • 不允许调用除了被@Builder装饰以外的方法,允许系统组件的参数是TS方法的返回值。
    @Component
    struct ParentComponent {
      doSomeCalculations() {
      }
    
      calcTextValue(): string {
        return 'Hello World';
      }
    
      @Builder doSomeRender() {
        Text(`Hello World`)
      }
    
      build() {
        Column() {
          // 反例:不能调用没有用@Builder装饰的方法
          this.doSomeCalculations();
          // 正例:可以调用
          this.doSomeRender();
          // 正例:参数可以为调用TS方法的返回值
          Text(this.calcTextValue())
        }
      }
    }
    
    • 不允许switch语法,如果需要使用条件判断,请使用if。反例如下。
    build() {
      Column() {
        // 反例:不允许使用switch语法
        switch (expression) {
          case 1:
            Text('...')
            break;
          case 2:
            Image('...')
            break;
          default:
            Text('...')
            break;
        }
      }
    }
    
    • 不允许使用表达式,反例如下。
    build() {
      Column() {
        // 反例:不允许使用表达式
        (this.aVar > 10) ? Text('...') : Image('...')
      }
    }
    

    自定义组件通用样式

    自定义组件通过“.”链式调用的形式设置通用样式。

    @Component
    struct MyComponent2 {
      build() {
        Button(`Hello World`)
      }
    }
    
    @Entry
    @Component
    struct MyComponent {
      build() {
        Row() {
          MyComponent2()
            .width(200)
            .height(300)
            .backgroundColor(Color.Red)
        }
      }
    }
    

    页面和自定义组件生命周期

    在开始之前,我们先明确自定义组件和页面的关系:

    • 自定义组件:@Component装饰的UI单元,可以组合多个系统组件实现UI的复用,可以调用组件的生命周期。
    • 页面:即应用的UI页面。可以由一个或者多个自定义组件组成,@Entry装饰的自定义组件为页面的入口组件,即页面的根节点,一个页面有且仅能有一个@Entry。只有被@Entry装饰的组件才可以调用页面的生命周期。

    页面生命周期,即被@Entry装饰的组件生命周期,提供以下生命周期接口:

    • onPageShow:页面每次显示时触发一次,包括路由过程、应用进入前台等场景,仅@Entry装饰的自定义组件生效。(相当于iOS中viewDidAppear)

    • onPageHide:页面每次隐藏时触发一次,包括路由过程、应用进入后台等场景,仅@Entry装饰的自定义组件生效。(相当于iOS中viewDidDisappear)

    • onBackPress:当用户点击返回按钮时触发,仅@Entry装饰的自定义组件生效。

    组件生命周期,即一般用@Component装饰的自定义组件的生命周期,提供以下生命周期接口:

    • aboutToAppear:组件即将出现时回调该接口,具体时机为在创建自定义组件的新实例后,在执行其build()函数之前执行。(在iOS中类似super.init()之后)

    • aboutToDisappear:aboutToDisappear函数在自定义组件析构销毁之前执行。不允许在aboutToDisappear函数中改变状态变量,特别是@Link变量的修改可能会导致应用程序行为不稳定。(在iOS中类似deinit())

    生命周期流程如下图所示,下图展示的是被@Entry装饰的组件(首页)生命周期。

    根据上面的流程图,我们从自定义组件的初始创建、重新渲染和删除来详细解释。

    自定义组件的创建和渲染流程

    1. 自定义组件的创建:自定义组件的实例由ArkUI框架创建。

    2. 初始化自定义组件的成员变量:通过本地默认值或者构造方法传递参数来初始化自定义组件的成员变量,初始化顺序为成员变量的定义顺序。

    3. 如果开发者定义了aboutToAppear,则执行aboutToAppear方法。

    4. 在首次渲染的时候,执行build方法渲染系统组件,如果子组件为自定义组件,则创建自定义组件的实例。在执行build()函数的过程中,框架会观察每个状态变量的读取状态,将保存两个map:

      a. 状态变量 -> UI组件(包括ForEach和if)。
      b. UI组件 -> 此组件的更新函数,即一个lambda方法,作为build()函数的子集,创建对应的UI组件并执行其属性方法,示意如下。

    build() {
      ...
      this.observeComponentCreation(() => {
        Button.create();
      })
    
      this.observeComponentCreation(() => {
        Text.create();
      })
      ...
    }
    

    当应用在后台启动时,此时应用进程并没有销毁,所以仅需要执行onPageShow。

    自定义组件的删除

    如果if组件的分支改变,或者ForEach循环渲染中数组的个数改变,组件将被删除:

    1. 在删除组件之前,将调用其aboutToDisappear生命周期函数,标记着该节点将要被销毁。ArkUI的节点删除机制是:后端节点直接从组件树上摘下,后端节点被销毁,对前端节点解引用,前端节点已经没有引用时,将被JS虚拟机垃圾回收。

    2. 自定义组件和它的变量将被删除,如果其有同步的变量,比如@Link@Prop@StorageLink,将从同步源上取消注册。

    不建议在生命周期aboutToDisappear内使用async await,如果在生命周期的aboutToDisappear使用异步操作(Promise或者回调方法),自定义组件将被保留在Promise的闭包中,直到回调方法被执行完,这个行为阻止了自定义组件的垃圾回收。

    以下示例展示了生命周期的调用时机:

    // Index.ets
    import router from '@ohos.router';
    
    @Entry
    @Component
    struct MyComponent {
      @State showChild: boolean = true;
      @State btnColor:string = "#FF007DFF"
    
      // 只有被@Entry装饰的组件才可以调用页面的生命周期
      onPageShow() {
        console.info('Index onPageShow');
      }
      // 只有被@Entry装饰的组件才可以调用页面的生命周期
      onPageHide() {
        console.info('Index onPageHide');
      }
    
      // 只有被@Entry装饰的组件才可以调用页面的生命周期
      onBackPress() {
        console.info('Index onBackPress');
        this.btnColor ="#FFEE0606"
        return true // 返回true表示页面自己处理返回逻辑,不进行页面路由;返回false表示使用默认的路由返回逻辑,不设置返回值按照false处理
      }
    
      // 组件生命周期
      aboutToAppear() {
        console.info('MyComponent aboutToAppear');
      }
    
      // 组件生命周期
      aboutToDisappear() {
        console.info('MyComponent aboutToDisappear');
      }
    
      build() {
        Column() {
          // this.showChild为true,创建Child子组件,执行Child aboutToAppear
          if (this.showChild) {
            Child()
          }
          // this.showChild为false,删除Child子组件,执行Child aboutToDisappear
          Button('delete Child')
          .margin(20)
          .backgroundColor(this.btnColor)
          .onClick(() => {
            this.showChild = false;
          })
          // push到page页面,执行onPageHide
          Button('push to next page')
            .onClick(() => {
              router.pushUrl({ url: 'pages/page' });
            })
        }
    
      }
    }
    
    @Component
    struct Child {
      @State title: string = 'Hello World';
      // 组件生命周期
      aboutToDisappear() {
        console.info('[lifeCycle] Child aboutToDisappear')
      }
      // 组件生命周期
      aboutToAppear() {
        console.info('[lifeCycle] Child aboutToAppear')
      }
    
      build() {
        Text(this.title).fontSize(50).margin(20).onClick(() => {
          this.title = 'Hello ArkUI';
        })
      }
    }
    
    // page.ets
    @Entry
    @Component
    struct page {
      @State textColor: Color = Color.Black;
      @State num: number = 0
    
      onPageShow() {
        this.num = 5
      }
    
      onPageHide() {
        console.log("page onPageHide");
      }
    
      onBackPress() { // 不设置返回值按照false处理
        this.textColor = Color.Grey
        this.num = 0
      }
    
      aboutToAppear() {
        this.textColor = Color.Blue
      }
    
      build() {
        Column() {
          Text(`num 的值为:${this.num}`)
            .fontSize(30)
            .fontWeight(FontWeight.Bold)
            .fontColor(this.textColor)
            .margin(20)
            .onClick(() => {
              this.num += 5
            })
        }
        .width('100%')
      }
    }
    

    以上示例中,Index页面包含两个自定义组件,一个是被@Entry装饰的MyComponent,也是页面的入口组件,即页面的根节点;一个是Child,是MyComponent的子组件。只有@Entry装饰的节点才可以使页面级别的生命周期方法生效,所以MyComponent中声明了当前Index页面的页面生命周期函数。MyComponent和其子组件Child也同时声明了组件的生命周期函数。

    • 应用冷启动的初始化流程为:MyComponent aboutToAppear --> MyComponent build --> Child aboutToAppear --> Child build --> Child build执行完毕 --> MyComponent build执行完毕 --> Index onPageShow。
    • 点击“delete Child”,if绑定的this.showChild变成false,删除Child组件,会执行Child aboutToDisappear方法。
    • 点击“push to next page”,调用router.pushUrl接口,跳转到另外一个页面,当前Index页面隐藏,执行页面生命周期Index onPageHide。此处调用的是router.pushUrl接口,Index页面被隐藏,并没有销毁,所以只调用onPageHide。跳转到新页面后,执行初始化新页面的生命周期的流程。
    • 如果调用的是router.replaceUrl,则当前Index页面被销毁,执行的生命周期流程将变为:Index onPageHide --> MyComponent aboutToDisappear --> Child aboutToDisappear。上文已经提到,组件的销毁是从组件树上直接摘下子树,所以先调用父组件的aboutToDisappear,再调用子组件的aboutToDisappear,然后执行初始化新页面的生命周期流程。
    • 点击返回按钮,触发页面生命周期Index onBackPress,且触发返回一个页面后会导致当前Index页面被销毁。
    • 最小化应用或者应用进入后台,触发Index onPageHide。当前Index页面没有被销毁,所以并不会执行组件的aboutToDisappear。应用回到前台,执行Index onPageShow。
    • 退出应用,执行Index onPageHide --> MyComponent aboutToDisappear --> Child aboutToDisappear。

    装饰器

    @Builder装饰器:自定义构建函数

    自定义组件内部UI结构固定,仅与使用方进行数据传递。ArkUI还提供了一种更轻量的UI元素复用机制@Builder,@Builder所装饰的函数遵循build()函数语法规则,开发者可以将重复使用的UI元素抽象成一个方法,在build方法里调用。
    为了简化语言,我们将@Builder装饰的函数也称为“自定义构建函数”。

    装饰器使用说明

    自定义组件内自定义构建函数

    定义的语法:

    @Builder MyBuilderFunction() { ... }
    

    使用方法:

    this.MyBuilderFunction()
    
    • 允许在自定义组件内定义一个或多个@Builder方法,该方法被认为是该组件的私有、特殊类型的成员函数。
    • 自定义构建函数可以在所属组件的build方法和其他自定义构建函数中调用,但不允许在组件外调用。
    • 在自定义函数体中,this指代当前所属组件,组件的状态变量可以在自定义构建函数内访问。建议通过this访问自定义组件的状态变量而不是参数传递。
    全局自定义构建函数

    定义的语法:

    @Builder function MyGlobalBuilderFunction() { ... }
    

    使用方法:

     MyGlobalBuilderFunction()
    
    • 全局的自定义构建函数可以被整个应用获取,不允许使用this和bind方法。

    • 如果不涉及组件状态变化,建议使用全局的自定义构建方法。

    参数传递规则

    自定义构建函数的参数传递有按值传递按引用传递两种,均需遵守以下规则:

    • 参数的类型必须与参数声明的类型一致,不允许undefined、null和返回undefined、null的表达式。

    • 在@Builder修饰的函数内部,不允许改变参数值。

    • @Builder内UI语法遵循UI语法规则

    • 只有传入一个参数,且参数需要直接传入对象字面量才会按引用传递该参数,其余传递方式均为按值传递。

    按引用传递参数

    按引用传递参数时,传递的参数可为状态变量,且状态变量的改变会引起@Builder方法内的UI刷新。ArkUI提供$$作为按引用传递参数的范式。

    class ABuilderParam {
      paramA1: string = ''
      paramB1: string = ''
    }
    @Builder function ABuilder($$ : ABuilderParam) {...}
    
    按值传递参数

    调用@Builder装饰的函数默认按值传递。当传递的参数为状态变量时,状态变量的改变不会引起@Builder方法内的UI刷新。所以当使用状态变量的时候,推荐使用按引用传递

    不同参数传递方式的示例
    class ABuilderParam {
      paramA1: string = ''
    }
    
    // 使用$$作为按引用传递参数的范式。当传递的参数为状态变量,且状态变量的改变会引起@Builder方法内的UI刷新
    @Builder function BuilderByReference($$: ABuilderParam) {
      Row() {
        Text(`UseStateVarByReference: ${$$.paramA1} `)
      }
      .backgroundColor(Color.Red)
    }
    
    // 默认按值传递。当传递的参数为状态变量时,状态变量的改变不会引起@Builder方法内的UI刷新
    @Builder function BuilderByValue(paramA1: string) {
      Row() {
        Text(`UseStateVarByValue: ${paramA1} `)
      }
      .backgroundColor(Color.Green)
    }
    
    @Entry
    @Component
    struct Parent {
      @State message: string = 'Hello World';
    
      build() {
        Row() {
          Column({ space: 10 }) {
            // 将this.message引用传递给BuilderByReference,message的改变会引起UI变化
            BuilderByReference({ paramA1: this.message })
            // 将this.message按值传递给BuilderByValue,message的改变不会引起UI变化
            BuilderByValue(this.message)
            Button('Click me').onClick(() => {
              // 点击“Click me”后,UI从“Hello World”刷新为“ArkUI”
              this.message = 'ArkUI';
            })
          }
          .width('100%')
        }
        .height('100%')
      }
    }
    
    状态变量改变时,按引用传递参数的@Builder方法内的UI才会刷新

    @BuilderParam装饰器:引用@Builder函数

    当开发者创建了自定义组件,并想对该组件添加特定功能时,例如在自定义组件中添加一个点击跳转操作。若直接在组件内嵌入事件方法,将会导致所有引入该自定义组件的地方均增加了该功能。为解决此问题,ArkUI引入了@BuilderParam装饰器,@BuilderParam用来装饰指向@Builder方法的变量,开发者可在初始化自定义组件时对此属性进行赋值,为自定义组件增加特定的功能。该装饰器用于声明任意UI描述的一个元素,类似slot占位符。

    初始化@BuilderParam装饰的方法

    @BuilderParam装饰的方法只能被自定义构建函数(@Builder装饰的方法)初始化

    • 使用所属自定义组件的自定义构建函数或者全局的自定义构建函数,在本地初始化@BuilderParam。
    @Builder function GlobalBuilder0() {}
    
    @Component
    struct Child {
      @Builder doNothingBuilder() {};
    
      @BuilderParam aBuilder0: () => void = this.doNothingBuilder;
      @BuilderParam aBuilder1: () => void = GlobalBuilder0;
      build(){}
    }
    
    • 用父组件自定义构建函数初始化子组件@BuilderParam装饰的方法。
    @Component
    struct Child {
      @Builder FunABuilder0() {}
      @BuilderParam aBuilder0: () => void = this.FunABuilder0;
    
      build() {
        Column() {
          this.aBuilder0()
        }
      }
    }
    
    @Entry
    @Component
    struct Parent {
      @Builder componentBuilder() {
        Text(`Parent builder `)
      }
    
      build() {
        Column() {
          Child({ aBuilder0: this.componentBuilder })
        }
      }
    }
    
    • 需注意this指向正确。
      以下示例中,Parent组件在调用this.componentBuilder()时,this指向其所属组件,即“Parent”。@Builder componentBuilder()传给子组件@BuilderParam aBuilder0,在Child组件中调用this.aBuilder0()时,this指向在Child的label,即“Child”。对于@BuilderParam aBuilder1,在将this.componentBuilder传给aBuilder1时,调用bind绑定了this,因此其this.label指向Parent的label。

    说明
    开发者谨慎使用bind改变函数调用的上下文,可能会使this指向混乱。

    @Component
    struct Child {
      label: string = `Child`
      @Builder FunABuilder0() {}
      @Builder FunABuilder1() {}
      @BuilderParam aBuilder0: () => void = this.FunABuilder0;
      @BuilderParam aBuilder1: () => void = this.FunABuilder1;
    
      build() {
        Column() {
          this.aBuilder0()
          this.aBuilder1()
        }
      }
    }
    
    @Entry
    @Component
    struct Parent {
      label: string = `Parent`
    
      @Builder componentBuilder() {
        Text(`${this.label}`)
      }
    
      build() {
        Column() {
          this.componentBuilder()
          Child({ aBuilder0: this.componentBuilder, aBuilder1: ():void=>{this.componentBuilder()} })
        }
      }
    }
    

    使用场景

    @BuilderParam装饰的方法可以是有参数和无参数的两种形式,需与指向的@Builder方法类型匹配。@BuilderParam装饰的方法类型需要和@Builder方法类型一致。

    class Tmp{
      label:string = ''
    }
    @Builder function GlobalBuilder1($$ : Tmp) {
      Text($$.label)
        .width(400)
        .height(50)
        .backgroundColor(Color.Green)
    }
    
    @Component
    struct Child {
      label: string = 'Child'
      @Builder FunABuilder0() {}
      // 无参数类,指向的componentBuilder也是无参数类型
      @BuilderParam aBuilder0: () => void = this.FunABuilder0;
      // 有参数类型,指向的GlobalBuilder1也是有参数类型的方法
      @BuilderParam aBuilder1: ($$ : Tmp) => void = GlobalBuilder1;
    
      build() {
        Column() {
          this.aBuilder0()
          this.aBuilder1({label: 'global Builder label' } )
        }
      }
    }
    
    @Entry
    @Component
    struct Parent {
      label: string = 'Parent'
    
      @Builder componentBuilder() {
        Text(`${this.label}`)
      }
    
      build() {
        Column() {
          this.componentBuilder()
          Child({ aBuilder0: this.componentBuilder, aBuilder1: GlobalBuilder1 })
        }
      }
    }
    

    尾随闭包初始化组件

    在自定义组件中使用@BuilderParam装饰的属性时也可通过尾随闭包进行初始化。在初始化自定义组件时,组件后紧跟一个大括号“{}”形成尾随闭包场景。

    说明:

    • 此场景下自定义组件内有且仅有一个使用@BuilderParam装饰的属性。
    • 此场景下自定义组件不支持使用通用属性。


      多个@BuilderParam装饰的属性,编译报错

    开发者可以将尾随闭包内的内容看做@Builder装饰的函数传给@BuilderParam。示例如下:

    // xxx.ets
    @Component
    struct CustomContainer {
      @Prop header: string = '';
      @Builder CloserFun(){}
      @BuilderParam closer: () => void = this.CloserFun
    
      build() {
        Column() {
          Text(this.header)
            .fontSize(30)
          this.closer()
        }
      }
    }
    
    @Builder function specificParam(label1: string, label2: string) {
      Column() {
        Text(label1)
          .fontSize(30)
        Text(label2)
          .fontSize(30)
      }
    }
    
    @Entry
    @Component
    struct CustomContainerUser {
      @State text: string = 'header';
    
      build() {
        Column() {
          // 创建CustomContainer,在创建CustomContainer时,通过其后紧跟一个大括号“{}”形成尾随闭包
          // 作为传递给子组件CustomContainer @BuilderParam closer: () => void的参数
          CustomContainer({ header: this.text }) {
            Column() {
              specificParam('testA', 'testB')
            }.backgroundColor(Color.Yellow)
            .onClick(() => {
              this.text = 'changeHeader';
            })
          }
        }
      }
    }
    

    @Styles装饰器:定义组件重用样式

    如果每个组件的样式都需要单独设置,在开发过程中会出现大量代码在进行重复样式设置,虽然可以复制粘贴,但为了代码简洁性和后续方便维护,我们推出了可以提炼公共样式进行复用的装饰器@Styles。

    @Styles装饰器可以将多条样式设置提炼成一个方法,直接在组件声明的位置调用。通过@Styles装饰器可以快速定义并复用自定义样式。用于快速定义并复用自定义样式。

    装饰器使用说明

    @Styles方法不支持参数,反例如下。

    // 反例: @Styles不支持参数
    @Styles function globalFancy (value: number) {
      .width(value)
    }
    
    • @Styles可以定义在组件内或全局,在全局定义时需在方法名前面添加function关键字,组件内定义时则不需要添加function关键字。
    // 全局
    @Styles function functionName() { ... }
    
    // 在组件内
    @Component
    struct FancyUse {
      @Styles fancy() {
        .height(100)
      }
    }
    
    • 定义在组件内的@Styles可以通过this访问组件的常量和状态变量,并可以在@Styles里通过事件来改变状态变量的值,示例如下:
    @Component
    struct FancyUse {
      @State heightValue: number = 100
      @Styles fancy() {
        .height(this.heightValue)
        .backgroundColor(Color.Yellow)
        .onClick(() => {
          this.heightValue = 200
        })
      }
    }
    
    • 组件内@Styles的优先级高于全局@Styles。
      框架优先找当前组件内的@Styles,如果找不到,则会全局查找。
    使用场景

    以下示例中演示了组件内@Styles和全局@Styles的用法。

    // 定义在全局的@Styles封装的样式
    @Styles function globalFancy  () {
      .width(150)
      .height(100)
      .backgroundColor(Color.Pink)
    }
    
    @Entry
    @Component
    struct FancyUse {
      @State heightValue: number = 100
      // 定义在组件内的@Styles封装的样式
      @Styles fancy() {
        .width(200)
        .height(this.heightValue)
        .backgroundColor(Color.Yellow)
        .onClick(() => {
          this.heightValue = 200
        })
      }
    
      build() {
        Column({ space: 10 }) {
          // 使用全局的@Styles封装的样式
          Text('FancyA')
            .globalFancy ()
            .fontSize(30)
          // 使用组件内的@Styles封装的样式
          Text('FancyB')
            .fancy()
            .fontSize(30)
        }
      }
    }
    

    @Extend装饰器:定义扩展组件样式

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

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

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

    @Entry
    @Component
    struct FancyUse {
      @State label: string = 'Hello World'
    
      build() {
        Row({ space: 10 }) {
          Text(`${this.label}`)
            .fontStyle(FontStyle.Italic)
            .fontWeight(100)
            .backgroundColor(Color.Blue)
          Text(`${this.label}`)
            .fontStyle(FontStyle.Italic)
            .fontWeight(200)
            .backgroundColor(Color.Pink)
          Text(`${this.label}`)
            .fontStyle(FontStyle.Italic)
            .fontWeight(300)
            .backgroundColor(Color.Orange)
        }.margin('20%')
      }
    }
    

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

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

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

    @Entry
    @Component
    struct FancyUse {
      @State label: string = 'Hello World'
    
      build() {
        Row({ space: 10 }) {
          Text(`${this.label}`)
            .fancyText(100, Color.Blue)
          Text(`${this.label}`)
            .fancyText(200, Color.Pink)
          Text(`${this.label}`)
            .fancyText(300, Color.Orange)
        }.margin('20%')
      }
    }
    

    stateStyles:多态样式

    @Styles和@Extend仅仅应用于静态页面的样式复用,stateStyles可以依据组件的内部状态的不同,快速设置不同样式。这就是我们本章要介绍的内容stateStyles(又称为:多态样式)。

    概述

    stateStyles是属性方法,可以根据UI内部状态来设置样式,类似于css伪类,但语法不同。ArkUI提供以下五种状态:
    focused:获焦态。
    normal:正常态。
    pressed:按压态。
    disabled:不可用态。
    selected10+:选中态。

    基础场景

    下面的示例展示了stateStyles最基本的使用场景。Button1处于第一个组件,Button2处于第二个组件。按压时显示为pressed态指定的黑色。使用Tab键走焦,先是Button1获焦并显示为focus态指定的粉色。当Button2获焦的时候,Button2显示为focus态指定的粉色,Button1失焦显示normal态指定的红色。

    @Entry
    @Component
    struct StateStylesSample {
      build() {
        Column() {
          Button('Button1')
            .stateStyles({
              focused: {
                .backgroundColor(Color.Pink)
              },
              pressed: {
                .backgroundColor(Color.Black)
              },
              normal: {
                .backgroundColor(Color.Red)
              }
            })
            .margin(20)
          Button('Button2')
            .stateStyles({
              focused: {
                .backgroundColor(Color.Pink)
              },
              pressed: {
                .backgroundColor(Color.Black)
              },
              normal: {
                .backgroundColor(Color.Red)
              }
            })
        }.margin('30%')
      }
    }
    
    图1 获焦态和按压态
    @Styles和stateStyles联合使用

    以下示例通过@Styles指定stateStyles的不同状态。

    @Entry
    @Component
    struct MyComponent {
      @Styles normalStyle() {
        .backgroundColor(Color.Gray)
      }
    
      @Styles pressedStyle() {
        .backgroundColor(Color.Red)
      }
    
      build() {
        Column() {
          Text('Text1')
            .fontSize(50)
            .fontColor(Color.White)
            .stateStyles({
              normal: this.normalStyle,
              pressed: this.pressedStyle,
            })
        }
      }
    }
    
    图2 正常态和按压态
    在stateStyles里使用常规变量和状态变量

    stateStyles可以通过this绑定组件内的常规变量和状态变量。

    @Entry
    @Component
    struct CompWithInlineStateStyles {
      @State focusedColor: Color = Color.Red;
      normalColor: Color = Color.Green
    
      build() {
        Column() {
          Button('clickMe').height(100).width(100)
            .stateStyles({
              normal: {
                .backgroundColor(this.normalColor)
              },
              focused: {
                .backgroundColor(this.focusedColor)
              }
            })
            .onClick(() => {
              this.focusedColor = Color.Pink
            })
            .margin('30%')
        }
      }
    }
    

    Button默认normal态显示绿色,第一次按下Tab键让Button获焦显示为focus态的红色,点击事件触发后,再次按下Tab键让Button获焦,focus态变为粉色。

    图3 点击改变获焦态样式

    @AnimatableExtend装饰器:定义可动画属性

    @AnimatableExtend装饰器用于自定义可动画的属性方法,在这个属性方法中修改组件不可动画的属性。在动画执行过程时,通过逐帧回调函数修改不可动画属性值,让不可动画属性也能实现动画效果。

    • 可动画属性:如果一个属性方法在animation属性前调用,改变这个属性的值可以生效animation属性的动画效果,这个属性称为可动画属性。比如height、width、backgroundColor、translate属性,Text组件的fontSize属性等。

    • 不可动画属性:如果一个属性方法在animation属性前调用,改变这个属性的值不能生效animation属性的动画效果,这个属性称为不可动画属性。比如Ployline组件的points属性等。

    语法
    @AnimatableExtend(UIComponentName) function functionName(value: typeName) { 
      .propertyName(value)
    }
    
    • @AnimatableExtend仅支持定义在全局,不支持在组件内部定义。
    • @AnimatableExtend定义的函数参数类型必须为number类型或者实现 AnimtableArithmetic<T>接口的自定义类型。
    • @AnimatableExtend定义的函数体内只能调用@AnimatableExtend括号内组件的属性方法。
    AnimtableArithmetic<T>接口说明

    对复杂数据类型做动画,需要实现AnimtableArithmetic<T>接口中加法、减法、乘法和判断相等函数。

    名称 入参类型 返回值类型 说明
    plus AnimtableArithmetic<T> AnimtableArithmetic<T> 加法函数
    subtract AnimtableArithmetic<T> AnimtableArithmetic<T> 减法函数
    multiply number AnimtableArithmetic<T> 乘法函数
    equals AnimtableArithmetic<T> boolean 相等判断函数
    使用场景

    以下示例实现字体大小的动画效果。

    @AnimatableExtend(Text) function animatableFontSize(size: number) {
      .fontSize(size)
    }
    
    @Entry
    @Component
    struct AnimatablePropertyExample {
      @State fontSize: number = 20
      build() {
        Column() {
          Text("AnimatableProperty")
            .animatableFontSize(this.fontSize)
            .animation({duration: 1000, curve: "ease"})
          Button("Play")
            .onClick(() => {
              this.fontSize = this.fontSize == 20 ? 36 : 20
            })
        }.width("100%")
        .padding(10)
      }
    }
    

    以下示例实现折线的动画效果。

    class Point {
      x: number
      y: number
    
      constructor(x: number, y: number) {
        this.x = x
        this.y = y
      }
      plus(rhs: Point): Point {
        return new Point(this.x + rhs.x, this.y + rhs.y)
      }
      subtract(rhs: Point): Point {
        return new Point(this.x - rhs.x, this.y - rhs.y)
      }
      multiply(scale: number): Point {
        return new Point(this.x * scale, this.y * scale)
      }
      equals(rhs: Point): boolean {
        return this.x === rhs.x && this.y === rhs.y
      }
    }
    
    class PointVector extends Array<Point> implements AnimatableArithmetic<PointVector> {
      constructor(value: Array<Point>) {
        super();
        value.forEach(p => this.push(p))
      }
      plus(rhs: PointVector): PointVector {
        let result = new PointVector([])
        const len = Math.min(this.length, rhs.length)
        for (let i = 0; i < len; i++) {
          result.push((this as Array<Point>)[i].plus((rhs as Array<Point>)[I]))
        }
        return result
      }
      subtract(rhs: PointVector): PointVector {
        let result = new PointVector([])
        const len = Math.min(this.length, rhs.length)
        for (let i = 0; i < len; i++) {
          result.push((this as Array<Point>)[i].subtract((rhs as Array<Point>)[I]))
        }
        return result
      }
      multiply(scale: number): PointVector {
        let result = new PointVector([])
        for (let i = 0; i < this.length; i++) {
          result.push((this as Array<Point>)[i].multiply(scale))
        }
        return result
      }
      equals(rhs: PointVector): boolean {
        if (this.length != rhs.length) {
          return false
        }
        for (let i = 0; i < this.length; i++) {
          if (!(this as Array<Point>)[i].equals((rhs as Array<Point>)[i])) {
            return false
          }
        }
        return true
      }
      get(): Array<Object[]> {
        let result: Array<Object[]> = []
        this.forEach(p => result.push([p.x, p.y]))
        return result
      }
    }
    
    @AnimatableExtend(Polyline) function animatablePoints(points: PointVector) {
      .points(points.get())
    }
    
    @Entry
    @Component
    struct AnimatablePropertyExample {
      @State points: PointVector = new PointVector([
        new Point(50, Math.random() * 200),
        new Point(100, Math.random() * 200),
        new Point(150, Math.random() * 200),
        new Point(200, Math.random() * 200),
        new Point(250, Math.random() * 200),
      ])
      build() {
        Column() {
          Polyline()
            .animatablePoints(this.points)
            .animation({duration: 1000, curve: "ease"})
            .size({height:220, width:300})
            .fill(Color.Green)
            .stroke(Color.Red)
            .backgroundColor('#eeaacc')
          Button("Play")
            .onClick(() => {
              this.points = new PointVector([
                new Point(50, Math.random() * 200),
                new Point(100, Math.random() * 200),
                new Point(150, Math.random() * 200),
                new Point(200, Math.random() * 200),
                new Point(250, Math.random() * 200),
              ])
            })
        }.width("100%")
        .padding(10)
      }
    }
    

    相关文章

      网友评论

          本文标题:HarmonyOS UI范式-基本语法

          本文链接:https://www.haomeiwen.com/subject/jtxxndtx.html