3. 菜单构建与动态显示
@Builder MenuBuilder() {
if(this.selectedDate === '收件箱') {
Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
Column() {
Row() {
Image($r('app.media.edit')).width(20).height(20).margin({ left:$r('app.float.padding'),right: 5 })
Text(`写邮件`).fontSize(15)
}
.width('100%')
.height(30)
.onClick(() => {
this.pageInfos.pushPathByName('EmailPage',null)
})
// 其他收件箱菜单选项...
}.padding(5)
}.width(100)
} else if(this.selectedDate === '通讯录') {
// 通讯录菜单选项...
} else if(this.selectedDate === '业务') {
// 业务菜单选项...
} else if(this.selectedDate === '消息') {
this.MessageTitle()
}
}
MenuBuilder方法根据selectedDate的值动态显示不同的菜单内容。- 不同的菜单选项点击后执行相应的导航操作,如写邮件、创建群聊等。
4. 扫码功能实现
ScanCode() {
let options: scanBarcode.ScanOptions = {
scanTypes: [scanCore.ScanType.ALL],
enableMultiMode: true,
enableAlbum: true
};
try {
scanBarcode.startScanForResult(getContext(this), options).then((result: scanBarcode.ScanResult) => {
hilog.info(0x0001, this.TAG, 'Promise scan result: %{public}s', JSON.stringify(result));
this.showScanResult(result);
}).catch((error: BusinessError) => {
hilog.error(0x0001, this.TAG, 'Promise error: %{public}s', JSON.stringify(error));
});
} catch (error) {
hilog.error(0x0001, this.TAG, 'failReason: %{public}s', JSON.stringify(error));
}
}
async showScanResult(result: scanBarcode.ScanResult) {
if (result) {
try {
promptAction.showToast({
message: JSON.stringify(result),
duration: 5000
});
} catch (error) {
hilog.error(0x0001, this.TAG, 'showToast error: %{public}s ', JSON.stringify(error));
}
}
}
ScanCode方法定义扫码参数并启动扫码操作。showScanResult方法将扫码结果通过promptAction.showToast显示给用户。
总结
通过上述核心代码,我们在 HarmonyOS Next 中成功构建了一个通用的页面头部组件。该组件通过传入的 selectedDate 属性动态显示不同的菜单内容,提供了统一的导航和操作入口。同时,组件集成了扫码功能,方便用户进行扫码操作。开发者可以根据实际需求进一步扩展该组件,如添加更多的菜单选项、优化扫码功能等,以满足更多办公场景的需求。