W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
在卡片頁面中可以通過postCardAction接口觸發(fā)router或call事件拉起UIAbility,然后由UIAbility刷新卡片內(nèi)容,下面是這種刷新方式的簡單示例。
- let storage = new LocalStorage();
- @Entry(storage)
- @Component
- struct WidgetCard {
- @LocalStorageProp('detail') detail: string = 'init';
- build() {
- Column() {
- Button('跳轉(zhuǎn)')
- .margin('20%')
- .onClick(() => {
- console.info('postCardAction to EntryAbility');
- postCardAction(this, {
- 'action': 'router',
- 'abilityName': 'EntryAbility', // 只能跳轉(zhuǎn)到當前應用下的UIAbility
- 'params': {
- 'detail': 'RouterFromCard'
- }
- });
- })
- Text(`${this.detail}`).margin('20%')
- }
- .width('100%')
- .height('100%')
- }
- }
- import UIAbility from '@ohos.app.ability.UIAbility';
- import formBindingData from '@ohos.app.form.formBindingData';
- import formProvider from '@ohos.app.form.formProvider';
- import formInfo from '@ohos.app.form.formInfo';
- export default class EntryAbility extends UIAbility {
- // 如果UIAbility第一次啟動,在收到Router事件后會觸發(fā)onCreate生命周期回調(diào)
- onCreate(want, launchParam) {
- console.info('Want:' + JSON.stringify(want));
- if (want.parameters[formInfo.FormParam.IDENTITY_KEY] !== undefined) {
- let curFormId = want.parameters[formInfo.FormParam.IDENTITY_KEY];
- let message = JSON.parse(want.parameters.params).detail;
- console.info(`UpdateForm formId: ${curFormId}, message: ${message}`);
- let formData = {
- "detail": message + ': onCreate UIAbility.', // 和卡片布局中對應
- };
- let formMsg = formBindingData.createFormBindingData(formData)
- formProvider.updateForm(curFormId, formMsg).then((data) => {
- console.info('updateForm success.' + JSON.stringify(data));
- }).catch((error) => {
- console.error('updateForm failed:' + JSON.stringify(error));
- })
- }
- }
- // 如果UIAbility已在后臺運行,在收到Router事件后會觸發(fā)onNewWant生命周期回調(diào)
- onNewWant(want, launchParam) {
- console.info('onNewWant Want:' + JSON.stringify(want));
- if (want.parameters[formInfo.FormParam.IDENTITY_KEY] !== undefined) {
- let curFormId = want.parameters[formInfo.FormParam.IDENTITY_KEY];
- let message = JSON.parse(want.parameters.params).detail;
- console.info(`UpdateForm formId: ${curFormId}, message: ${message}`);
- let formData = {
- "detail": message + ': onNewWant UIAbility.', // 和卡片布局中對應
- };
- let formMsg = formBindingData.createFormBindingData(formData)
- formProvider.updateForm(curFormId, formMsg).then((data) => {
- console.info('updateForm success.' + JSON.stringify(data));
- }).catch((error) => {
- console.error('updateForm failed:' + JSON.stringify(error));
- })
- }
- }
- ...
- }
- import formBindingData from '@ohos.app.form.formBindingData';
- import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
- export default class EntryFormAbility extends FormExtensionAbility {
- onAddForm(want) {
- let formId = want.parameters["ohos.extra.param.key.form_identity"];
- let dataObj1 = {
- "formId": formId
- };
- let obj1 = formBindingData.createFormBindingData(dataObj1);
- return obj1;
- }
- ...
- };
- let storage = new LocalStorage();
- @Entry(storage)
- @Component
- struct WidgetCard {
- @LocalStorageProp('detail') detail: string = 'init';
- @LocalStorageProp('formId') formId: string = '0';
- build() {
- Column() {
- Button('拉至后臺')
- .margin('20%')
- .onClick(() => {
- console.info('postCardAction to EntryAbility');
- postCardAction(this, {
- 'action': 'call',
- 'abilityName': 'EntryAbility', // 只能跳轉(zhuǎn)到當前應用下的UIAbility
- 'params': {
- 'method': 'funA',
- 'formId': this.formId,
- 'detail': 'CallFromCard'
- }
- });
- })
- Text(`${this.detail}`).margin('20%')
- }
- .width('100%')
- .height('100%')
- }
- }
- import UIAbility from '@ohos.app.ability.UIAbility';
- import formBindingData from '@ohos.app.form.formBindingData';
- import formProvider from '@ohos.app.form.formProvider';
- import formInfo from '@ohos.app.form.formInfo';
- const MSG_SEND_METHOD: string = 'funA'
- // 在收到call事件后會觸發(fā)callee監(jiān)聽的方法
- function FunACall(data) {
- // 獲取call事件中傳遞的所有參數(shù)
- let params = JSON.parse(data.readString())
- if (params.formId !== undefined) {
- let curFormId = params.formId;
- let message = params.detail;
- console.info(`UpdateForm formId: ${curFormId}, message: ${message}`);
- let formData = {
- "detail": message
- };
- let formMsg = formBindingData.createFormBindingData(formData)
- formProvider.updateForm(curFormId, formMsg).then((data) => {
- console.info('updateForm success.' + JSON.stringify(data));
- }).catch((error) => {
- console.error('updateForm failed:' + JSON.stringify(error));
- })
- }
- return null;
- }
- export default class EntryAbility extends UIAbility {
- // 如果UIAbility第一次啟動,call事件后會觸發(fā)onCreate生命周期回調(diào)
- onCreate(want, launchParam) {
- console.info('Want:' + JSON.stringify(want));
- try {
- // 監(jiān)聽call事件所需的方法
- this.callee.on(MSG_SEND_METHOD, FunACall);
- } catch (error) {
- console.log(`${MSG_SEND_METHOD} register failed with error ${JSON.stringify(error)}`)
- }
- }
- ...
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: