#鸿蒙通关秘籍#如何根据鸿蒙卡片的状态动态刷新不同内容?

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
系统小卫士

可通过本地数据库存储卡片状态,在onUpdateForm回调中根据状态选择要刷新的内容:

javascript import { preferences } from '@kit.ArkData'; import { formBindingData, FormExtensionAbility, formProvider } from '@kit.FormKit';

export default class UpdateByStatusFormAbility extends FormExtensionAbility { onAddForm(want) { let formId = JSON.stringify(want.parameters[formInfo.FormParam.IDENTITY_KEY]); let promise = preferences.getPreferences(this.context, 'myStore'); promise.then((storeDB) => { storeDB.put('A' + formId, 'false'); storeDB.put('B' + formId, 'false'); storeDB.flush(); });

return formBindingData.createFormBindingData({});

}

onUpdateForm(formId) { let promise = preferences.getPreferences(this.context, 'myStore'); promise.then(async (storeDB) => { let stateA = await storeDB.get('A' + formId, 'false'); let stateB = await storeDB.get('B' + formId, 'false'); if (stateA === 'true') { let param = { 'textA': 'AAA' }; formProvider.updateForm(formId, formBindingData.createFormBindingData(param)); } if (stateB === 'true') { let param = { 'textB': 'BBB' }; formProvider.updateForm(formId, formBindingData.createFormBindingData(param)); } }); }

onFormEvent(formId, message) { let promise = preferences.getPreferences(this.context, 'myStore'); promise.then(async (storeDB) => { let msg = JSON.parse(message); if (msg.selectA !== undefined) { storeDB.put('A' + formId, msg.selectA); } if (msg.selectB !== undefined) { storeDB.put('B' + formId, msg.selectB); } storeDB.flush(); }); } }

使用preferences进行信息的持久化,并在相应的状态变化后更新卡片内容。


分享
微博
QQ
微信
回复
2天前
相关问题