arkts-ui-widget-event-formextensionability.md 3.1 KB
Newer Older
W
wangkailong 已提交
1
# 通过message事件刷新卡片内容
Z
zhongjianfei 已提交
2 3


D
dujingcheng 已提交
4
说明:<br/>  本文主要介绍动态卡片的事件开发。对于静态卡片,请参见[FormLink](../../application-dev/reference/arkui-ts/ts-container-formlink.md)<br/>
W
wangkailong 已提交
5
在卡片页面中可以通过**postCardAction**接口触发message事件拉起FormExtensionAbility,然后由FormExtensionAbility刷新卡片内容,下面是这种刷新方式的简单示例。
Z
zhongjianfei 已提交
6 7


W
form  
wangkailong 已提交
8
- 在卡片页面通过注册Button的onClick点击事件回调,并在回调中调用**postCardAction**接口触发事件至FormExtensionAbility。卡片页面中使用[LocalStorageProp](../quick-start/arkts-localstorage.md#localstorageprop)装饰需要刷新的卡片数据。
Z
zhongjianfei 已提交
9 10 11 12 13 14
  
  ```ts
  let storage = new LocalStorage();
  @Entry(storage)
  @Component
  struct WidgetCard {
Z
zhongjianfei 已提交
15 16
    @LocalStorageProp('title') title: string = 'Title default';
    @LocalStorageProp('detail') detail: string = 'Description default';
Z
zhongjianfei 已提交
17 18 19
  
    build() {
      Column() {
Z
zhongjianfei 已提交
20 21 22 23 24 25 26 27
        Column() {
          Text(`${this.title}`)
            .margin(5).fontWeight(FontWeight.Medium).fontSize('14fp')
          Text(`${this.detail}`)
            .margin(5).fontColor(Color.Gray).fontSize('12fp').height('25%')
        }.width('100%').alignItems(HorizontalAlign.Start)
        Button('UPDATE')
          .margin(15).width('90%')
Z
zhongjianfei 已提交
28 29 30 31 32 33 34 35
          .onClick(() => {
            postCardAction(this, {
              'action': 'message',
              'params': {
                'msgTest': 'messageEvent'
              }
            });
          })
Z
zhongjianfei 已提交
36
      }.width('90%').height('90%').margin('5%')
Z
zhongjianfei 已提交
37 38 39 40
    }
  }
  ```
  
41
- 在FormExtensionAbility的onFormEvent生命周期中调用[updateForm](../reference/apis/js-apis-app-form-formProvider.md#updateform)接口刷新卡片。
Z
zhongjianfei 已提交
42 43 44 45 46 47 48
  
  ```ts
  import formBindingData from '@ohos.app.form.formBindingData';
  import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
  import formProvider from '@ohos.app.form.formProvider';
  
  export default class EntryFormAbility extends FormExtensionAbility {
X
xuzhihao 已提交
49
    onFormEvent(formId: string, message: string) {
Z
zhongjianfei 已提交
50 51
      // Called when a specified message event defined by the form provider is triggered.
      console.info(`FormAbility onEvent, formId = ${formId}, message: ${JSON.stringify(message)}`);
X
xuzhihao 已提交
52 53 54
      let formData = new Map<Object, string>();
      formData.set('title', 'Title Update.'); // 和卡片布局中对应
      formData.set('detail', 'Description update success.'); // 和卡片布局中对应
X
xuzhihao 已提交
55 56 57 58
      let formInfo: formBindingData.FormBindingData = formBindingData.createFormBindingData(formData);
      formProvider.updateForm(formId, formInfo).then(() => {
        console.info('FormAbility updateForm success.');
      });
Z
zhongjianfei 已提交
59
    }
X
xuzhihao 已提交
60

61
    ...
Z
zhongjianfei 已提交
62 63 64
  }
  ```

Z
zhongjianfei 已提交
65 66 67 68 69
  运行效果如下图所示。
  
  | 初始状态                                                | 点击刷新                                              |
  | ------------------------------------------------------- | ----------------------------------------------------- |
  | ![WidgetUpdateBefore](figures/widget-update-before.PNG) | ![WidgetUpdateAfter](figures/widget-update-after.PNG) |