1e41f4b71Sopenharmony_ci# AttributeUpdater
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci将属性直接设置给组件,无需标记为状态变量即可直接触发UI更新。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci>  **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci>  从API Version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## 导入模块
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci```ts
14e41f4b71Sopenharmony_ciimport { AttributeUpdater } from '@kit.ArkUI'
15e41f4b71Sopenharmony_ci```
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci>  **使用说明:**
18e41f4b71Sopenharmony_ci>  
19e41f4b71Sopenharmony_ci>  1. 由于与属性方法同时设置或者在AttributeUpdater中实现applyNormalAttribute等方法时,涉及到与状态管理更新机制同时使用,易出现混淆,因此不建议在同一组件上同时用两种方法设置相同属性。
20e41f4b71Sopenharmony_ci>  
21e41f4b71Sopenharmony_ci>  2. 当与属性方法同时设置时,属性生效的原则为:后设置的生效。
22e41f4b71Sopenharmony_ci>  若先进行属性直通更新,后通过状态管理机制更新属性方法,则后更新的属性方法生效;
23e41f4b71Sopenharmony_ci>  若先通过状态管理机制更新属性方法,后进行属性直通更新,则属性直通更新生效。
24e41f4b71Sopenharmony_ci>  
25e41f4b71Sopenharmony_ci>  3. 一个AttributeUpdater对象只能同时关联一个组件,否则将出现设置的属性只在一个组件上生效的现象。
26e41f4b71Sopenharmony_ci>  
27e41f4b71Sopenharmony_ci>  4. 开发者需要自行保障AttributeUpdater中T和C的类型匹配。比如T为ImageAttribute,C要对应为ImageInterface,否则可能导致
28e41f4b71Sopenharmony_ci>  使用updateConstructorParams时功能异常。
29e41f4b71Sopenharmony_ci>  
30e41f4b71Sopenharmony_ci>  5. updateConstructorParams当前只支持Button,Image,Text和Span组件。
31e41f4b71Sopenharmony_ci>  
32e41f4b71Sopenharmony_ci>  6. AttributeUpdater不支持深浅色切换等状态管理相关的操作。
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci## Initializer
35e41f4b71Sopenharmony_citype Initializer\<T> = () => T
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci可以将属性更新到本地的修饰器。
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci## AttributeUpdater<T, C = Initializer\<T>>
44e41f4b71Sopenharmony_ci为[AttributeModifier](arkui-ts/ts-universal-attributes-attribute-modifier.md#AttributeModifier)的实现类,开发者需要自定义class继承AttributeUpdater。
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci其中C代表组件的构造函数类型,比如Text组件的TextInterface,Image组件的ImageInterface等,需要使用updateConstructorParams时才需要传递C类型。
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci### applyNormalAttribute
51e41f4b71Sopenharmony_ciapplyNormalAttribute?(instance: T): void
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci定义正常态更新属性函数。
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci**参数:**
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                                     |
62e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------------------ |
63e41f4b71Sopenharmony_ci| instance | T | 是 | 组件的属性类,用来标识进行属性设置的组件的类型,比如Button组件的ButtonAttribute,Text组件的TextAttribute等。|
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci### initializeModifier
66e41f4b71Sopenharmony_ciinitializeModifier(instance: T): void
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ciAttributeUpdater首次设置给组件时提供的样式。
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci**参数:**
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                                     |
77e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------------------ |
78e41f4b71Sopenharmony_ci| instance | T | 是 | 组件的属性类,用来标识进行属性设置的组件的类型,比如Button组件的ButtonAttribute,Text组件的TextAttribute等。|
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci**示例:** 
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci通过initializeModifier方法初始化设置属性值。
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci```ts
85e41f4b71Sopenharmony_ci// xxx.ets
86e41f4b71Sopenharmony_ciimport { AttributeUpdater } from '@kit.ArkUI'
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ciclass MyButtonModifier extends AttributeUpdater<ButtonAttribute> {
89e41f4b71Sopenharmony_ci  initializeModifier(instance: ButtonAttribute): void {
90e41f4b71Sopenharmony_ci    instance.backgroundColor('#ff2787d9')
91e41f4b71Sopenharmony_ci      .width('50%')
92e41f4b71Sopenharmony_ci      .height(30)
93e41f4b71Sopenharmony_ci  }
94e41f4b71Sopenharmony_ci}
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci@Entry
97e41f4b71Sopenharmony_ci@Component
98e41f4b71Sopenharmony_cistruct updaterDemo1 {
99e41f4b71Sopenharmony_ci  modifier: MyButtonModifier = new MyButtonModifier()
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci  build() {
102e41f4b71Sopenharmony_ci    Row() {
103e41f4b71Sopenharmony_ci      Column() {
104e41f4b71Sopenharmony_ci        Button("Button")
105e41f4b71Sopenharmony_ci          .attributeModifier(this.modifier)
106e41f4b71Sopenharmony_ci      }
107e41f4b71Sopenharmony_ci      .width('100%')
108e41f4b71Sopenharmony_ci    }
109e41f4b71Sopenharmony_ci    .height('100%')
110e41f4b71Sopenharmony_ci  }
111e41f4b71Sopenharmony_ci}
112e41f4b71Sopenharmony_ci```
113e41f4b71Sopenharmony_ci![attributeUpdater1](figures/attribute-updater1.PNG)
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci### attribute
117e41f4b71Sopenharmony_ciget attribute(): T | undefined
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci获取AttributeUpdater中组件对应的属性类实例,通过该实例实现属性直通更新的功能。
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci**返回值:**
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci| 类型             | 描述                                                         |
128e41f4b71Sopenharmony_ci| -------------------- | ------------------------------------------------------------ |
129e41f4b71Sopenharmony_ci| T \| undefined |如果AttributeUpdater中组件的属性类实例存在,则返回对应组件的属性类实例,否则返回undefined。|
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci**示例:** 
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci通过属性直通设置方式更新属性值。
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci```ts
136e41f4b71Sopenharmony_ci// xxx.ets
137e41f4b71Sopenharmony_ciimport { AttributeUpdater } from '@kit.ArkUI'
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ciclass MyButtonModifier extends AttributeUpdater<ButtonAttribute> {
140e41f4b71Sopenharmony_ci  initializeModifier(instance: ButtonAttribute): void {
141e41f4b71Sopenharmony_ci    instance.backgroundColor('#ffd5d5d5')
142e41f4b71Sopenharmony_ci      .width('50%')
143e41f4b71Sopenharmony_ci      .height(30)
144e41f4b71Sopenharmony_ci  }
145e41f4b71Sopenharmony_ci}
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci@Entry
148e41f4b71Sopenharmony_ci@Component
149e41f4b71Sopenharmony_cistruct updaterDemo2 {
150e41f4b71Sopenharmony_ci  modifier: MyButtonModifier = new MyButtonModifier()
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci  build() {
153e41f4b71Sopenharmony_ci    Row() {
154e41f4b71Sopenharmony_ci      Column() {
155e41f4b71Sopenharmony_ci        Button("Button")
156e41f4b71Sopenharmony_ci          .attributeModifier(this.modifier)
157e41f4b71Sopenharmony_ci          .onClick(() => {
158e41f4b71Sopenharmony_ci            this.modifier.attribute?.backgroundColor('#ff2787d9').width('30%')
159e41f4b71Sopenharmony_ci          })
160e41f4b71Sopenharmony_ci      }
161e41f4b71Sopenharmony_ci      .width('100%')
162e41f4b71Sopenharmony_ci    }
163e41f4b71Sopenharmony_ci    .height('100%')
164e41f4b71Sopenharmony_ci  }
165e41f4b71Sopenharmony_ci}
166e41f4b71Sopenharmony_ci```
167e41f4b71Sopenharmony_ci![attributeUpdater2](figures/attribute-updater2.gif)
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci### updateConstructorParams
170e41f4b71Sopenharmony_ciupdateConstructorParams: C
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci用来更改组件的构造入参。C代表组件的构造函数类型。
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci其中C代表组件的构造函数类型,比如Text组件的TextInterface,Image组件的ImageInterface等。
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci**示例:** 
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci使用updateConstructorParams更新组件构造入参。
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci```ts
185e41f4b71Sopenharmony_ci// xxx.ets
186e41f4b71Sopenharmony_ciimport { AttributeUpdater } from '@kit.ArkUI'
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ciclass MyTextModifier extends AttributeUpdater<TextAttribute, TextInterface> {
189e41f4b71Sopenharmony_ci  initializeModifier(instance: TextAttribute) {
190e41f4b71Sopenharmony_ci  }
191e41f4b71Sopenharmony_ci}
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci@Entry
194e41f4b71Sopenharmony_ci@Component
195e41f4b71Sopenharmony_cistruct attributeDemo3 {
196e41f4b71Sopenharmony_ci  private modifier: MyTextModifier = new MyTextModifier()
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci  build() {
199e41f4b71Sopenharmony_ci    Row() {
200e41f4b71Sopenharmony_ci      Column() {
201e41f4b71Sopenharmony_ci        Text("Initialize")
202e41f4b71Sopenharmony_ci          .attributeModifier(this.modifier)
203e41f4b71Sopenharmony_ci          .fontSize(14).border({ width: 1 }).textAlign(TextAlign.Center).lineHeight(20)
204e41f4b71Sopenharmony_ci          .width(200).height(50)
205e41f4b71Sopenharmony_ci          .backgroundColor('#fff7f7f7')
206e41f4b71Sopenharmony_ci          .onClick(() => {
207e41f4b71Sopenharmony_ci            this.modifier.updateConstructorParams("Updated")
208e41f4b71Sopenharmony_ci          })
209e41f4b71Sopenharmony_ci      }
210e41f4b71Sopenharmony_ci      .width('100%')
211e41f4b71Sopenharmony_ci    }
212e41f4b71Sopenharmony_ci    .height('100%')
213e41f4b71Sopenharmony_ci  }
214e41f4b71Sopenharmony_ci}
215e41f4b71Sopenharmony_ci```
216e41f4b71Sopenharmony_ci![attributeUpdater3](figures/attribute-updater3.gif)
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci### onComponentChanged
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_cionComponentChanged(component: T): void
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_ci绑定相同的自定义的Modifier对象,组件发生切换时,通过该接口通知到应用。
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci**参数:**
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                                     |
231e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------------------ |
232e41f4b71Sopenharmony_ci| component | T | 是 | 组件的属性类,用来标识进行属性设置的组件的类型,比如Button组件的ButtonAttribute,Text组件的TextAttribute等。|
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci**示例:** 
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ci```ts
237e41f4b71Sopenharmony_ci// xxx.ets
238e41f4b71Sopenharmony_ciimport { AttributeUpdater } from '@kit.ArkUI'
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ciclass MyButtonModifier extends AttributeUpdater<ButtonAttribute> {
241e41f4b71Sopenharmony_ci  initializeModifier(instance: ButtonAttribute): void {
242e41f4b71Sopenharmony_ci    instance.backgroundColor('#ff2787d9')
243e41f4b71Sopenharmony_ci      .width('50%')
244e41f4b71Sopenharmony_ci      .height(30)
245e41f4b71Sopenharmony_ci  }
246e41f4b71Sopenharmony_ci
247e41f4b71Sopenharmony_ci  onComponentChanged(instance: ButtonAttribute) :void {
248e41f4b71Sopenharmony_ci    instance.backgroundColor('#ff2787d9')
249e41f4b71Sopenharmony_ci      .width('50%')
250e41f4b71Sopenharmony_ci      .height(30)
251e41f4b71Sopenharmony_ci  }
252e41f4b71Sopenharmony_ci}
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_ci@Entry
255e41f4b71Sopenharmony_ci@Component
256e41f4b71Sopenharmony_cistruct updaterDemo4 {
257e41f4b71Sopenharmony_ci  @State btnState: boolean = false
258e41f4b71Sopenharmony_ci  modifier: MyButtonModifier = new MyButtonModifier()
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ci  build() {
261e41f4b71Sopenharmony_ci    Row() {
262e41f4b71Sopenharmony_ci      Column() {
263e41f4b71Sopenharmony_ci        Button("Test")
264e41f4b71Sopenharmony_ci          .onClick(() => {
265e41f4b71Sopenharmony_ci          this.btnState = !this.btnState
266e41f4b71Sopenharmony_ci        })
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci        if (this.btnState) {
269e41f4b71Sopenharmony_ci          Button("Button")
270e41f4b71Sopenharmony_ci            .attributeModifier(this.modifier)
271e41f4b71Sopenharmony_ci        } else {
272e41f4b71Sopenharmony_ci          Button("Button")
273e41f4b71Sopenharmony_ci            .attributeModifier(this.modifier)
274e41f4b71Sopenharmony_ci        }
275e41f4b71Sopenharmony_ci      }
276e41f4b71Sopenharmony_ci      .width('100%')
277e41f4b71Sopenharmony_ci    }
278e41f4b71Sopenharmony_ci    .height('100%')
279e41f4b71Sopenharmony_ci  }
280e41f4b71Sopenharmony_ci}
281e41f4b71Sopenharmony_ci```