2024年鸿蒙最新鸿蒙HarmonyOS实战-Stage模型(开发卡片事件)_鸿蒙卡片开发(1),2024年最新字节跳动双选会面试问题

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!


img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

build() {
Column() {
Button(‘功能A’)
.margin(‘20%’)
.onClick(() => {
console.info(‘call EntryAbility funA’);
postCardAction(this, {
‘action’: ‘call’,
‘abilityName’: ‘EntryAbility’, // 只能跳转到当前应用下的UIAbility
‘params’: {
‘method’: ‘funA’ // 在EntryAbility中调用的方法名
}
});
})
Button(‘功能B’)
.margin(‘20%’)
.onClick(() => {
console.info(‘call EntryAbility funB’);
postCardAction(this, {
‘action’: ‘call’,
‘abilityName’: ‘EntryAbility’, // 只能跳转到当前应用下的UIAbility
‘params’: {
‘method’: ‘funB’, // 在EntryAbility中调用的方法名
‘num’: 1 // 需要传递的其他参数
}
});
})
}
.width(‘100%’)
.height(‘100%’)
}
}

2、UIAbility接收参数

import UIAbility from ‘@ohos.app.ability.UIAbility’;

function FunACall(data) {
// 获取call事件中传递的所有参数
console.log(‘FunACall param:’ + JSON.stringify(data.readString()));
return null;
}
function FunBCall(data) {
console.log(‘FunACall param:’ + JSON.stringify(data.readString()));
return null;
}

export default class CameraAbility extends UIAbility {
// 如果UIAbility第一次启动,在收到call事件后会触发onCreate生命周期回调
onCreate(want, launchParam) {
try {
// 监听call事件所需的方法
this.callee.on(‘funA’, FunACall);
this.callee.on(‘funB’, FunBCall);
} catch (error) {
console.log('register failed with error. Cause: ’ + JSON.stringify(error));
}
}

// 进程退出时,解除监听
onDestroy() {
try {
this.callee.off(‘funA’);
this.callee.off(‘funB’);
} catch (error) {
console.log('register failed with error. Cause: ’ + JSON.stringify(error));
}
}
};

不截图同上

4.通过message事件刷新卡片内容

1、卡片页面

let storage = new LocalStorage();

@Entry(storage)
@Component
struct WidgetCard {
@LocalStorageProp(‘title’) title: string = ‘init’;
@LocalStorageProp(‘detail’) detail: string = ‘init’;

build() {
Column() {
Button(‘刷新’)
.onClick(() => {
postCardAction(this, {
‘action’: ‘message’,
‘params’: {
‘msgTest’: ‘messageEvent’
}
});
})
Text(${this.title})
Text(${this.detail})
}
.width(‘100%’)
.height(‘100%’)
}
}

2、卡片FormExtensionAbility

import formBindingData from ‘@ohos.app.form.formBindingData’;
import FormExtensionAbility from ‘@ohos.app.form.FormExtensionAbility’;
import formProvider from ‘@ohos.app.form.formProvider’;

export default class EntryFormAbility extends FormExtensionAbility {
onFormEvent(formId, message) {
// Called when a specified message event defined by the form provider is triggered.
console.info(FormAbility onEvent, formId = ${formId}, message: ${JSON.stringify(message)});
let formData = {
‘title’: ‘Title Update Success.’, // 和卡片布局中对应
‘detail’: ‘Detail Update Success.’, // 和卡片布局中对应
};
let formInfo = formBindingData.createFormBindingData(formData)
formProvider.updateForm(formId, formInfo).then((data) => {
console.info(‘FormAbility updateForm success.’ + JSON.stringify(data));
}).catch((error) => {
console.error('FormAbility updateForm failed: ’ + JSON.stringify(error));
})
}
}

在这里插入图片描述

5.通过router或call事件刷新卡片内容

🦋5.1 router

1、卡片

let storage = new LocalStorage();

@Entry(storage)
@Component
struct WidgetCard {
@LocalStorageProp(‘detail’) detail: string = ‘init’;

build() {
Column() {
Button(‘跳转’)
.margin(‘20%’)
.onClick(() => {
console.info(‘postCardAction to EntryAbility’);
postCardAction(this, {
‘action’: ‘router’,
‘abilityName’: ‘EntryAbility’, // 只能跳转到当前应用下的UIAbility
‘params’: {
‘detail’: ‘RouterFromCard’
}
});
})
Text(${this.detail}).margin(‘20%’)
}
.width(‘100%’)
.height(‘100%’)
}
}

2、UIAbility

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事件后会触发onCreate生命周期回调
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事件后会触发onNewWant生命周期回调
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));
})
}
}


}

🦋5.2 call

1、在使用postCardAction接口的call事件时,需要在FormExtensionAbility中的onAddForm生命周期回调中更新formId。

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;
}


};

2、卡片界面

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’, // 只能跳转到当前应用下的UIAbility
‘params’: {
‘method’: ‘funA’,
‘formId’: this.formId,
‘detail’: ‘CallFromCard’
}
});
})
Text(${this.detail}).margin(‘20%’)
}
.width(‘100%’)
.height(‘100%’)
}
}

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’, // 只能跳转到当前应用下的UIAbility
‘params’: {
‘method’: ‘funA’,
‘formId’: this.formId,
‘detail’: ‘CallFromCard’
}
});
})
Text(${this.detail}).margin(‘20%’)

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!


img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

blog.csdnimg.cn/direct/743b668910224b259a5ffe804fa6d0db.png)
[外链图片转存中…(img-uOy2wAGV-1715795495747)]
[外链图片转存中…(img-UvrG5ZuX-1715795495747)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

  • 18
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值