1# 通过router或call事件刷新卡片内容 2 3 4在卡片页面中可以通过**postCardAction**接口触发router事件或者call事件拉起UIAbility,然后由UIAbility刷新卡片内容,下面是这种刷新方式的简单示例。 5 6## 通过router事件刷新卡片内容 7 8- 在卡片页面通过注册Button的onClick点击事件回调,并在回调中调用**postCardAction**接口触发router事件至FormExtensionAbility。 9 10 ```ts 11 let storage = new LocalStorage(); 12 @Entry(storage) 13 @Component 14 struct WidgetCard { 15 @LocalStorageProp('detail') detail: string = 'init'; 16 17 build() { 18 Column() { 19 Button('跳转') 20 .margin('20%') 21 .onClick(() => { 22 console.info('postCardAction to EntryAbility'); 23 postCardAction(this, { 24 'action': 'router', 25 'abilityName': 'EntryAbility', // 只能跳转到当前应用下的UIAbility 26 'params': { 27 'detail': 'RouterFromCard' 28 } 29 }); 30 }) 31 Text(`${this.detail}`).margin('20%') 32 } 33 .width('100%') 34 .height('100%') 35 } 36 } 37 ``` 38 39- 在UIAbility的onCreate()或者onNewWant()生命周期中可以通过入参want获取卡片的formID和传递过来的参数信息,然后调用[updateForm](../reference/apis/js-apis-app-form-formProvider.md#updateform)接口刷新卡片。 40 41 ```ts 42 import UIAbility from '@ohos.app.ability.UIAbility'; 43 import formBindingData from '@ohos.app.form.formBindingData'; 44 import formProvider from '@ohos.app.form.formProvider'; 45 import formInfo from '@ohos.app.form.formInfo'; 46 47 export default class EntryAbility extends UIAbility { 48 // 如果UIAbility第一次启动,在收到Router事件后会触发onCreate生命周期回调 49 onCreate(want, launchParam) { 50 console.info('Want:' + JSON.stringify(want)); 51 if (want.parameters[formInfo.FormParam.IDENTITY_KEY] !== undefined) { 52 let curFormId = want.parameters[formInfo.FormParam.IDENTITY_KEY]; 53 let message = JSON.parse(want.parameters.params).detail; 54 console.info(`UpdateForm formId: ${curFormId}, message: ${message}`); 55 let formData = { 56 "detail": message + ': onCreate UIAbility.', // 和卡片布局中对应 57 }; 58 let formMsg = formBindingData.createFormBindingData(formData) 59 formProvider.updateForm(curFormId, formMsg).then((data) => { 60 console.info('updateForm success.' + JSON.stringify(data)); 61 }).catch((error) => { 62 console.error('updateForm failed:' + JSON.stringify(error)); 63 }) 64 } 65 } 66 // 如果UIAbility已在后台运行,在收到Router事件后会触发onNewWant生命周期回调 67 onNewWant(want, launchParam) { 68 console.info('onNewWant Want:' + JSON.stringify(want)); 69 if (want.parameters[formInfo.FormParam.IDENTITY_KEY] !== undefined) { 70 let curFormId = want.parameters[formInfo.FormParam.IDENTITY_KEY]; 71 let message = JSON.parse(want.parameters.params).detail; 72 console.info(`UpdateForm formId: ${curFormId}, message: ${message}`); 73 let formData = { 74 "detail": message + ': onNewWant UIAbility.', // 和卡片布局中对应 75 }; 76 let formMsg = formBindingData.createFormBindingData(formData) 77 formProvider.updateForm(curFormId, formMsg).then((data) => { 78 console.info('updateForm success.' + JSON.stringify(data)); 79 }).catch((error) => { 80 console.error('updateForm failed:' + JSON.stringify(error)); 81 }) 82 } 83 } 84 85 ... 86 } 87 ``` 88 89## 通过call事件刷新卡片内容 90 91- 在使用**postCardAction**接口的call事件时,需要在FormExtensionAbility中的onAddForm生命周期回调中更新formId。 92 93 ```ts 94 import formBindingData from '@ohos.app.form.formBindingData'; 95 import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility'; 96 97 export default class EntryFormAbility extends FormExtensionAbility { 98 onAddForm(want) { 99 let formId = want.parameters["ohos.extra.param.key.form_identity"]; 100 let dataObj1 = { 101 "formId": formId 102 }; 103 let obj1 = formBindingData.createFormBindingData(dataObj1); 104 return obj1; 105 } 106 107 ... 108 }; 109 ``` 110 111- 在卡片页面通过注册Button的onClick点击事件回调,并在回调中调用**postCardAction**接口触发call事件至UIAbility。 112 113 ```ts 114 let storage = new LocalStorage(); 115 @Entry(storage) 116 @Component 117 struct WidgetCard { 118 @LocalStorageProp('detail') detail: string = 'init'; 119 @LocalStorageProp('formId') formId: string = '0'; 120 121 build() { 122 Column() { 123 Button('拉至后台') 124 .margin('20%') 125 .onClick(() => { 126 console.info('postCardAction to EntryAbility'); 127 postCardAction(this, { 128 'action': 'call', 129 'abilityName': 'EntryAbility', // 只能拉起当前应用下的UIAbility 130 'params': { 131 'method': 'funA', 132 'formId': this.formId, 133 'detail': 'CallFromCard' 134 } 135 }); 136 }) 137 Text(`${this.detail}`).margin('20%') 138 } 139 .width('100%') 140 .height('100%') 141 } 142 } 143 ``` 144 145- 在UIAbility的onCreate生命周期中监听call事件所需的方法,然后在对应方法中调用[updateForm](../reference/apis/js-apis-app-form-formProvider.md#updateform)接口刷新卡片。 146 147 ```ts 148 import UIAbility from '@ohos.app.ability.UIAbility'; 149 import formBindingData from '@ohos.app.form.formBindingData'; 150 import formProvider from '@ohos.app.form.formProvider'; 151 import formInfo from '@ohos.app.form.formInfo'; 152 153 const MSG_SEND_METHOD: string = 'funA' 154 155 // 在收到call事件后会触发callee监听的方法 156 function FunACall(data) { 157 // 获取call事件中传递的所有参数 158 let params = JSON.parse(data.readString()) 159 if (params.formId !== undefined) { 160 let curFormId = params.formId; 161 let message = params.detail; 162 console.info(`UpdateForm formId: ${curFormId}, message: ${message}`); 163 let formData = { 164 "detail": message 165 }; 166 let formMsg = formBindingData.createFormBindingData(formData) 167 formProvider.updateForm(curFormId, formMsg).then((data) => { 168 console.info('updateForm success.' + JSON.stringify(data)); 169 }).catch((error) => { 170 console.error('updateForm failed:' + JSON.stringify(error)); 171 }) 172 } 173 return null; 174 } 175 export default class EntryAbility extends UIAbility { 176 // 如果UIAbility第一次启动,call事件后会触发onCreate生命周期回调 177 onCreate(want, launchParam) { 178 console.info('Want:' + JSON.stringify(want)); 179 try { 180 // 监听call事件所需的方法 181 this.callee.on(MSG_SEND_METHOD, FunACall); 182 } catch (error) { 183 console.log(`${MSG_SEND_METHOD} register failed with error ${JSON.stringify(error)}`) 184 } 185 } 186 ... 187 } 188 ``` 189