卡片更新

更新数据

1router方式,通过UIAbility调用fromProvider的updateForm 2message事件方式,通过FormExtensionAbility调用fromProvider的updateForm 3 4核心原理:用到本地存储
router方式,通过UIAbility调用fromProvider的updateForm message事件方式,通过FormExtensionAbility调用fromProvider的updateForm 核心原理:用到本地存储

router更新

更新+跳转

 1let storageUpdateRouter = new LocalStorage()  2  3@Entry  4@Component  5struct Widget02Card {  6 @LocalStorageProp('routerDetail') routerDetail: ResourceStr = $r('app.string.EntryAbility_label')  7 /*  8 * The title.  9 */ 10 readonly title: string = '运动卡片2222'; 11 /* 12 * The action type. 13 */ 14 readonly actionType: string = 'router'; 15 /* 16 * The ability name. 17 */ 18 readonly abilityName: string = 'EntryAbility'; 19 /* 20 * The message. 21 */ 22 readonly message: string = 'add detail'; 23 /* 24 * The width percentage setting. 25 */ 26 readonly fullWidthPercent: string = '100%'; 27 /* 28 * The height percentage setting. 29 */ 30 readonly fullHeightPercent: string = '100%'; 31 32 build() { 33 Column() { 34 Column() { 35 Text(this.title) 36 .fontSize($r('app.float.font_size')) 37 .fontWeight(FontWeight.Medium) 38 .fontColor($r('sys.color.font_primary')) 39 } 40 .width(this.fullWidthPercent) 41 42 Text(this.routerDetail) 43 .fontSize(20) 44 .fontColor(Color.White) 45 46 Flex({ 47 direction: FlexDirection.Row, 48 justifyContent: FlexAlign.SpaceBetween 49 }) { 50 Button('主页').onClick((event: ClickEvent) => { 51 postCardAction(this, { 52 action: this.actionType, 53 abilityName: this.abilityName, 54 params: { 55 targetPage: 'Index' 56 } 57 }) 58 }) 59 Button('数据').onClick((event: ClickEvent) => { 60 postCardAction(this, { 61 action: this.actionType, 62 abilityName: this.abilityName, 63 params: { 64 targetPage: 'DataPage' 65 } 66 }) 67 }) 68 69 Button('更新').onClick((event: ClickEvent) => { 70 postCardAction(this, { 71 action: this.actionType, 72 abilityName: this.abilityName, 73 params: { 74 routerDetail: 'RouterFormCard' 75 } 76 }) 77 }) 78 } 79 .width('100%') 80 } 81 .height(this.fullHeightPercent) 82 .backgroundColor(Color.Brown) 83 84 } 85}
let storageUpdateRouter = new LocalStorage() @Entry @Component struct Widget02Card { @LocalStorageProp('routerDetail') routerDetail: ResourceStr = $r('app.string.EntryAbility_label') /* * The title. */ readonly title: string = '运动卡片2222'; /* * The action type. */ readonly actionType: string = 'router'; /* * The ability name. */ readonly abilityName: string = 'EntryAbility'; /* * The message. */ readonly message: string = 'add detail'; /* * The width percentage setting. */ readonly fullWidthPercent: string = '100%'; /* * The height percentage setting. */ readonly fullHeightPercent: string = '100%'; build() { Column() { Column() { Text(this.title) .fontSize($r('app.float.font_size')) .fontWeight(FontWeight.Medium) .fontColor($r('sys.color.font_primary')) } .width(this.fullWidthPercent) Text(this.routerDetail) .fontSize(20) .fontColor(Color.White) Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween }) { Button('主页').onClick((event: ClickEvent) => { postCardAction(this, { action: this.actionType, abilityName: this.abilityName, params: { targetPage: 'Index' } }) }) Button('数据').onClick((event: ClickEvent) => { postCardAction(this, { action: this.actionType, abilityName: this.abilityName, params: { targetPage: 'DataPage' } }) }) Button('更新').onClick((event: ClickEvent) => { postCardAction(this, { action: this.actionType, abilityName: this.abilityName, params: { routerDetail: 'RouterFormCard' } }) }) } .width('100%') } .height(this.fullHeightPercent) .backgroundColor(Color.Brown) } }

 1import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit';  2import { hilog } from '@kit.PerformanceAnalysisKit';  3import { window } from '@kit.ArkUI';  4import { cardDBUtils } from '../utils/CardDBUtils';  5import preferencesUtil from '../utils/PreferencesUtils';  6import kvStore from '../utils/KVStore';  7import { formBindingData, formInfo, formProvider } from '@kit.FormKit';  8import { BusinessError } from '@kit.BasicServicesKit';  9 10const DOMAIN = 0x0000; 11 12export default class EntryAbility extends UIAbility { 13 // 跳转的路径地址 14 private selectPage: string = '' 15 // 指定的窗口地址 16 private currentWindowStage: window.WindowStage | null = null 17 18 /** 19 * 更新函数 20 * @param want 21 * @param source 22 */ 23 handleFormRouterEvent(want: Want, source: string): void { 24 // 判断是否传参, 以及是否有更新的key 25 if (want.parameters && want.parameters[formInfo.FormParam.IDENTITY_KEY] !== undefined) { 26 // curFormId获取到卡片编号 27 let curFormId = want.parameters[formInfo.FormParam.IDENTITY_KEY].toString() 28 // 自己传过来的key 29 let message: string = (JSON.parse(want.parameters?.params as string))?.routerDetail 30 // 定义要更新的卡片数据 31 let formData: Record<string, string> = { 32 'routerDetail': message + ' ' + source + ' UIAbility', // 和卡片布局中对应 33 } 34 // 要修改的本地存储对象 35 let formMsg = formBindingData.createFormBindingData(formData) 36 // 执行更新 37 formProvider.updateForm(curFormId, formMsg).then((data) => { 38 console.log('update CardFormAbility success') 39 }).catch((error: BusinessError) => { 40 console.log('update CardFormAbility failed') 41 }) 42 } 43 } 44 45 /** 46 * 初始化时执行 47 * 48 * @param want 49 * @param launchParam 50 */ 51 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 52 console.log('onCreate') 53 console.log(`onCreate-${JSON.stringify(want.parameters?.params)}`) 54 this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); 55 hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); 56 if (want.parameters) { 57 let params: Record<string, string> = JSON.parse(JSON.stringify(want.parameters)) 58 this.selectPage = params.targetPage 59 } 60 this.handleFormRouterEvent(want, 'onCreate') 61 } 62 63 /** 64 * 唤起 65 * @param want 66 * @param launchParam 67 */ 68 onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { 69 console.log(`onNewWant-${JSON.stringify(want.parameters?.params)}`) 70 if (want.parameters) { 71 let params: Record<string, string> = JSON.parse(JSON.stringify(want.parameters)) 72 this.selectPage = params.targetPage 73 } 74 if (this.currentWindowStage) { 75 this.onWindowStageCreate(this.currentWindowStage) 76 } 77 this.handleFormRouterEvent(want, 'onNewWant') 78 }
import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; import { cardDBUtils } from '../utils/CardDBUtils'; import preferencesUtil from '../utils/PreferencesUtils'; import kvStore from '../utils/KVStore'; import { formBindingData, formInfo, formProvider } from '@kit.FormKit'; import { BusinessError } from '@kit.BasicServicesKit'; const DOMAIN = 0x0000; export default class EntryAbility extends UIAbility { // 跳转的路径地址 private selectPage: string = '' // 指定的窗口地址 private currentWindowStage: window.WindowStage | null = null /** * 更新函数 * @param want * @param source */ handleFormRouterEvent(want: Want, source: string): void { // 判断是否传参, 以及是否有更新的key if (want.parameters && want.parameters[formInfo.FormParam.IDENTITY_KEY] !== undefined) { // curFormId获取到卡片编号 let curFormId = want.parameters[formInfo.FormParam.IDENTITY_KEY].toString() // 自己传过来的key let message: string = (JSON.parse(want.parameters?.params as string))?.routerDetail // 定义要更新的卡片数据 let formData: Record<string, string> = { 'routerDetail': message + ' ' + source + ' UIAbility', // 和卡片布局中对应 } // 要修改的本地存储对象 let formMsg = formBindingData.createFormBindingData(formData) // 执行更新 formProvider.updateForm(curFormId, formMsg).then((data) => { console.log('update CardFormAbility success') }).catch((error: BusinessError) => { console.log('update CardFormAbility failed') }) } } /** * 初始化时执行 * * @param want * @param launchParam */ onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { console.log('onCreate') console.log(`onCreate-${JSON.stringify(want.parameters?.params)}`) this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); if (want.parameters) { let params: Record<string, string> = JSON.parse(JSON.stringify(want.parameters)) this.selectPage = params.targetPage } this.handleFormRouterEvent(want, 'onCreate') } /** * 唤起 * @param want * @param launchParam */ onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { console.log(`onNewWant-${JSON.stringify(want.parameters?.params)}`) if (want.parameters) { let params: Record<string, string> = JSON.parse(JSON.stringify(want.parameters)) this.selectPage = params.targetPage } if (this.currentWindowStage) { this.onWindowStageCreate(this.currentWindowStage) } this.handleFormRouterEvent(want, 'onNewWant') }

message更新

只更新数据

1Button('Router更新').onClick((event: ClickEvent) => { 2 postCardAction(this, { 3 action: this.actionType, 4 abilityName: this.abilityName, 5 params: { 6 routerDetail: 'RouterFormCard' 7 } 8 }) 9})
Button('Router更新').onClick((event: ClickEvent) => { postCardAction(this, { action: this.actionType, abilityName: this.abilityName, params: { routerDetail: 'RouterFormCard' } }) })

 1import { formBindingData, FormExtensionAbility, formInfo, formProvider } from '@kit.FormKit';  2import { Want } from '@kit.AbilityKit';  3import { BusinessError } from '@kit.BasicServicesKit';  4  5export default class CardFormAbility extends FormExtensionAbility {  6 onAddForm(want: Want) {  7 // Called to return a FormBindingData object.  8 const formData = '';  9 return formBindingData.createFormBindingData(formData); 10 } 11 12 onCastToNormalForm(formId: string) { 13 // Called when the form provider is notified that a temporary form is successfully 14 // converted to a normal form. 15 } 16 17 onUpdateForm(formId: string) { 18 // Called to notify the form provider to update a specified form. 19 } 20 21 // message触发, 进入此函数 22 onFormEvent(formId: string, message: string) { 23 // Called when a specified message event defined by the form provider is triggered. 24 // 定义存储到本地键值对 25 class FormDataClass { 26 routerDetail: string = 'CardFormAbility' 27 } 28 29 // 保存到本地的对象 30 let formData = new FormDataClass() 31 // 得到更新到本地formInfo 32 let formInfo: formBindingData.FormBindingData = formBindingData.createFormBindingData(formData) 33 // updateForm用于更新卡片数据 34 formProvider.updateForm(formId, formInfo).then(() => { 35 console.log('onFormEvent updateForm success') 36 }) 37 } 38 39 onRemoveForm(formId: string) { 40 // Called to notify the form provider that a specified form has been destroyed. 41 } 42 43 onAcquireFormState(want: Want) { 44 // Called to return a {@link FormState} object. 45 return formInfo.FormState.READY; 46 } 47}
import { formBindingData, FormExtensionAbility, formInfo, formProvider } from '@kit.FormKit'; import { Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class CardFormAbility extends FormExtensionAbility { onAddForm(want: Want) { // Called to return a FormBindingData object. const formData = ''; return formBindingData.createFormBindingData(formData); } onCastToNormalForm(formId: string) { // Called when the form provider is notified that a temporary form is successfully // converted to a normal form. } onUpdateForm(formId: string) { // Called to notify the form provider to update a specified form. } // message触发, 进入此函数 onFormEvent(formId: string, message: string) { // Called when a specified message event defined by the form provider is triggered. // 定义存储到本地键值对 class FormDataClass { routerDetail: string = 'CardFormAbility' } // 保存到本地的对象 let formData = new FormDataClass() // 得到更新到本地formInfo let formInfo: formBindingData.FormBindingData = formBindingData.createFormBindingData(formData) // updateForm用于更新卡片数据 formProvider.updateForm(formId, formInfo).then(() => { console.log('onFormEvent updateForm success') }) } onRemoveForm(formId: string) { // Called to notify the form provider that a specified form has been destroyed. } onAcquireFormState(want: Want) { // Called to return a {@link FormState} object. return formInfo.FormState.READY; } }

定时刷新

 1{  2 "name": "widget02",  3 "displayName": "$string:widget02_display_name",  4 "description": "$string:widget02_desc",  5 "src": "./ets/widget02/pages/Widget02Card.ets",  6 "uiSyntax": "arkts",  7 "window": {  8 "designWidth": 720,  9 "autoDesignWidth": true 10 }, 11 "colorMode": "auto", 12 "isDynamic": true, 13 "isDefault": false, 14 "updateEnabled": false, 15 "scheduledUpdateTime": "10:30", // 定点刷新,指定时间刷新1次,需要将定时刷新配置为0 16 "updateDuration": 1, // 定时刷新,默认卡片30分钟刷新1次 17 "defaultDimension": "2*2", 18 "supportDimensions": [ 19 "2*2", 20 "2*4", 21 "4*4" 22 ] 23}
{ "name": "widget02", "displayName": "$string:widget02_display_name", "description": "$string:widget02_desc", "src": "./ets/widget02/pages/Widget02Card.ets", "uiSyntax": "arkts", "window": { "designWidth": 720, "autoDesignWidth": true }, "colorMode": "auto", "isDynamic": true, "isDefault": false, "updateEnabled": false, "scheduledUpdateTime": "10:30", // 定点刷新,指定时间刷新1次,需要将定时刷新配置为0 "updateDuration": 1, // 定时刷新,默认卡片30分钟刷新1次 "defaultDimension": "2*2", "supportDimensions": [ "2*2", "2*4", "4*4" ] }

接口刷新

设置接口触发5分钟后刷新内容

1formProvider.setFormNextRefreshTime(formId, five_minute, (err: BusinessError)=>{})
formProvider.setFormNextRefreshTime(formId, five_minute, (err: BusinessError)=>{})

Powered By 可尔物语

浙ICP备11005866号-12