ArkTS语言HarmonyOS/OpenHarmony应用开发-message事件刷新卡片内容-鸿蒙开发者社区-51CTO.COM

ArkTS语言HarmonyOS/OpenHarmony应用开发-message事件刷新卡片内容 原创

鸿蒙时代
发布于 2023-6-13 10:10
浏览
0收藏

一、效果视频
点击查看视频
二、开发过程
在卡片页面中可以通过postCardAction接口触发message事件拉起FormExtensionAbility,然后由FormExtensionAbility刷新卡片内容。
ArkTS语言HarmonyOS/OpenHarmony应用开发-message事件刷新卡片内容-鸿蒙开发者社区
common:公共文件

通过点击button按钮,刷新卡片内容。代码示例:
WidgetCard.ets

let storage = new LocalStorage();

@Entry(storage)
@Component
struct WidgetCard {
  @LocalStorageProp('title') title: string = 'title';
  @LocalStorageProp('content') detail: string = 'content';
  @LocalStorageProp('img') img: string = '/common/img/1.jpg';

  build() {
    Column() {
      Image(this.img).width(50).height(50)
      Text(`${this.title}`)
      Text(`${this.detail}`)
      Button('刷新').type(ButtonType.Capsule).height(20).margin({top:10})
        .onClick(() => {
          postCardAction(this, {
            'action': 'message',
            'params': {
              'msg': 'refresh'
            }
          });
        })

    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }
}

在onFormEvent中主动刷新
EntryFormAbility.ts

import formInfo from '@ohos.app.form.formInfo';
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 {
  onAddForm(want) {
    // Called to return a FormBindingData object.
    let formData = {};
    return formBindingData.createFormBindingData(formData);
  }

  onCastToNormalForm(formId) {
    // Called when the form provider is notified that a temporary form is successfully
    // converted to a normal form.
  }

  onUpdateForm(formId) {
    // Called to notify the form provider to update a specified form.
  }

  onChangeFormVisibility(newStatus) {
    // Called when the form provider receives form events from the system.
  }

  onFormEvent(formId, message) {
    // Called when a specified message event defined by the form provider is triggered.
    let formData = {
      'title': '标题',
      'content': '内容',
      'img': '/common/img/2.jpg',
    };
    let formInfo = formBindingData.createFormBindingData(formData)
    formProvider.updateForm(formId, formInfo).then((data) => {
      console.info('FormAbility updateForm success.' + JSON.stringify(data));
    }).catch((error) => {
      console.error('FormAbility updateForm failed: ' + JSON.stringify(error));
    })
  }

  onRemoveForm(formId) {
    // Called to notify the form provider that a specified form has been destroyed.
  }

  onAcquireFormState(want) {
    // Called to return a {@link FormState} object.
    return formInfo.FormState.READY;
  }
};

案例效果:
打开模拟器
ArkTS语言HarmonyOS/OpenHarmony应用开发-message事件刷新卡片内容-鸿蒙开发者社区
添加卡片至桌面
ArkTS语言HarmonyOS/OpenHarmony应用开发-message事件刷新卡片内容-鸿蒙开发者社区

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
ArkTS语言HarmonyOSOpenHarmony应用开发-me.docx 116.03K 20次下载
收藏
回复
举报
回复
    相关推荐