HarmonyOS鸿蒙Next中动态卡片携参传送门及刷新卡片数据方法(router和message两种方法)模拟器真机看,热加载也不行
HarmonyOS鸿蒙Next中动态卡片携参传送门及刷新卡片数据方法(router和message两种方法)模拟器真机看,热加载也不行
1.router方式
// WidgetCard.ets
```typescript
let storageUpdateRouter = new LocalStorage()
[@Entry](/user/Entry)(storageUpdateRouter)
[@Component](/user/Component)
struct Widget02Card {
// 卡片数据刷新-本地存储初始化
[@LocalStorageProp](/user/LocalStorageProp)('routerDetail') routerDetail:ResourceStr = $r('app.string.EntryFormAbility_label')
/*
* The title.
*/
readonly title: string = '动态卡片';
/*
* 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() {
Row() {
Column() {
// 卡片数据刷新
Text(`卡片更新数据:${this.routerDetail}`)
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween }) {
// 卡片跳转-携带参数跳转到主页界面
Button('主页')
.onClick(() => {
postCardAction(this, {
action: this.actionType,
abilityName: this.abilityName,
params: {
targetPage: 'Index'
}
});
})
// 卡片跳转-携带参数跳转到Traces界面
Button('Traces')
.onClick(() => {
postCardAction(this, {
action: this.actionType,
abilityName: this.abilityName,
params: {
targetPage: 'Traces'
}
});
})
// 卡片数据刷新-router方式跳转
Button('router更新卡片')
.onClick(() => {
postCardAction(this, {
action: this.actionType,
abilityName: this.abilityName,
params: {
routerDetail: 'RouterCard'
}
});
})
// 卡片数据刷新-message方式跳转
Button('message更新卡片')
.onClick(() => {
postCardAction(this, {
action: 'message'
});
})
}
}
.width(this.fullWidthPercent)
}
.height(this.fullHeightPercent)
}
}
// EntryAbility.ets
import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '[@kit](/user/kit).AbilityKit';
import { hilog } from '[@kit](/user/kit).PerformanceAnalysisKit';
import { window } from '[@kit](/user/kit).ArkUI';
import { CartDB } from 'feature';
import { formBindingData, formInfo, formProvider } from '[@kit](/user/kit).FormKit';
import { BusinessError } from '[@kit](/user/kit).BasicServicesKit';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
handleFormRouterEvent(want: Want, source: string): void {
// 判断是否有parameters,以及是否有更新的key
if (want.parameters && want.parameters[formInfo.FormParam.IDENTITY_KEY] !== undefined) {
// curFormId 卡片编号
let curFormId = want.parameters[formInfo.FormParam.IDENTITY_KEY].toString();
// want.parameters.params 对应 postCardAction() 中 params 内容
// 获取传过来的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) => {
// hilog.info(DOMAIN_NUMBER, TAG, 'updateForm success.', JSON.stringify(data));
console.log('卡片updateForm success')
}).catch((error: BusinessError) => {
// hilog.info(DOMAIN_NUMBER, TAG, 'updateForm failed.', JSON.stringify(error));
console.log('卡片updateForm failed')
});
}
}
// 卡片跳转-跳转的路径地址
private selectPage:string = ''
// 卡片跳转-当前窗口对象
private currentWindowStage:window.WindowStage | undefined
// 卡片跳转-初始化时候进来
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
if (want.parameters !== undefined) {
let params:Record<string,string> = JSON.parse(JSON.stringify(want.parameters))
this.selectPage = params.targetPage
}
// 更新卡片
this.handleFormRouterEvent(want,'onCreate')
}
// 卡片跳转-不是初始化后重新进来
onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
if (want.parameters !== undefined) {
let params:Record<string,string> = JSON.parse(JSON.stringify(want.parameters))
this.selectPage = params.targetPage
}
if (this.currentWindowStage !== undefined) {
this.onWindowStageCreate(this.currentWindowStage)
}
// 更新卡片
this.handleFormRouterEvent(want,'onNewWant')
}
onDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// 加载数据库
CartDB.initCartDB(this.context, 'MYCARDB')
// Main window is created, set main page for this ability
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
// 卡片跳转
let targetPage:string = ''
switch(this.selectPage){
case 'Index':
targetPage = 'pages/Index'
break;
case 'Traces':
targetPage = 'pages/Traces'
break;
default :
targetPage = 'pages/Index'
}
if (this.currentWindowStage === undefined) {
this.currentWindowStage = windowStage
}
// 卡片跳转-替换为动态卡片页面
windowStage.loadContent(targetPage, (err) => {
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
// Ability has brought to foreground
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
// Ability has back to background
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
}
}
2.message方式
EntryFormAbility.ets
// 卡片刷新回调
onFormEvent(formId: string, message: string) {
// 定义本地键值对
class FormClass{
routerDetail:string = 'EntryFormAbility-message'
}
// 实例化对象
let formData = new FormClass()
// 得到更新的本地数据
let formInfo: formBindingData.FormBindingData = formBindingData.createFormBindingData(formData);
formProvider.updateForm(formId, formInfo).then(() => {
console.log('卡片mesage更新成功')
});
}
可设置定时定点刷新,冲突时定时刷新优先级高,要定点则将updateDuration设置为0
1.定时刷新
src/main/resources/base/profile/form_config.json中 (1)updateEnabled字段为true (2)updateDuration:1 // 默认30分钟,1*30
2.定点刷新
src/main/resources/base/profile/form_config.json中
"scheduledUpdateTime": "10:30", // 即早上十点半
更多关于HarmonyOS鸿蒙Next中动态卡片携参传送门及刷新卡片数据方法(router和message两种方法)模拟器真机看,热加载也不行的实战教程也可以访问 https://www.itying.com/category-93-b0.html
你这边可以检查一下代码,真机和模拟器对比一下相关配置
更多关于HarmonyOS鸿蒙Next中动态卡片携参传送门及刷新卡片数据方法(router和message两种方法)模拟器真机看,热加载也不行的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,动态卡片的数据刷新和参数传递可以通过router
和message
两种方法实现。使用router
时,可以通过router.push
传递参数,并在目标页面接收。使用message
时,可以通过postMessage
发送数据,监听message
事件接收并更新卡片数据。在模拟器和真机中,确保开发工具版本一致,若热加载无效,可尝试重新编译部署。