WidgetCard.ets
@Entry
@Component
struct WidgetCard {
@State num: number = 0
@LocalStorageProp('formId')
@Watch('updataFormId')
formId:string=''
updataFormId(){
// 卡片向app传参
postCardAction(this, {
action: 'call',
abilityName: 'EntryAbility',
params:{
method:'updataFormId',
num:this.formId
}
});
}
build() {
Row() {
Column() {
Text('ID:'+this.formId)
Button(this.num + '++').onClick(() => {
this.num++
postCardAction(this, {
action: 'call',
abilityName: 'EntryAbility',
params:{
method:'updataNum',
num:this.num
}
});
})
}.width('100%')
}
.height('100%')
.onClick(() => {
postCardAction(this, {
action: 'router', //跳转事件
abilityName: 'EntryAbility',
});
})
}
}
获取卡片id
在卡片的entryability中:EntryFormAbility
export default class EntryFormAbility extends FormExtensionAbility {
onAddForm(want: Want) {
// Called to return a FormBindingData object.
let formData = '';
return formBindingData.createFormBindingData({
// form id
formId:want.parameters!['ohos.extra.param.key.form_identity']as string
});
}
在app的entryability中:
class Params implements rpc.Parcelable{
marshalling(dataOut: rpc.MessageSequence): boolean {
return true
}
unmarshalling(dataIn: rpc.MessageSequence): boolean {
return true
}
}
class CaraParam{
num:number=0
formId:string=''
}
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
this.callee.on('updataNum',(data)=>{
let res=JSON.parse(data.readString())as CaraParam
AppStorage.setOrCreate('num',res.num)
return new Params()
})
// 卡片创建时,调用了updataFormId,此时这里就会被触发,然后app将值传过去
this.callee.on('updataFormId',(data)=>{
let res=JSON.parse(data.readString())as CaraParam
// AppStorage.setOrCreate('num',res.formId)
// 数据可以写到首选项中存储
// 把最新的数据推过去
formProvider.updateForm(res.formId,formBindingData.createFormBindingData({
num:AppStorage.get('num')
}))
return new Params()
})
}