写点什么

【每日学点鸿蒙知识】网络请求回调 toast 问题、Popup 问题、禁止弹窗返回、navigation 折叠屏不显示返回键、响应式布局

作者:轻口味
  • 2025-03-06
    北京
  • 本文字数:3197 字

    阅读完需:约 10 分钟

【每日学点鸿蒙知识】网络请求回调toast问题、Popup问题、禁止弹窗返回、navigation折叠屏不显示返回键、响应式布局

1、HarmonyOS http 请求回调后,showToast 报错?

使用 http.HttpRequest.request 发起 http 请求后,在 promise 的 then 回调里执行自定义回调函数,但是回调到页面代码后,调用 toast 会报错[ArkRuntime Log] Error: Internal error. UI execution context not found.


操作步骤:1、http.createHttp();创建请求 2、request.request;发起请求 3、promise.then 中处理数据然后执行回调函数 4、回调函数中使用 promptAction.showToast 进行输出 toast


根据报错信息:Internal error. UI execution context not found.是在调用 promptAction.showToast 接口时,接口识别到场景下 UI 实例缺失主动抛出的。绑定 UI 实例来调用接口,同时进行合理的 try catch 异常捕捉。 demo 如下:


@Entry@Componentstruct Index {  @State message: string = '从窗口实例中获取UI实例,通过UI实例使用接口';
aboutToAppear(): void { setTimeout(() => { //抓取异常 try { this.getUIContext().getPromptAction().showToast({ message: '弹窗测试', duration: 2000 }) } catch (e) { console.error("弹窗异常,异常信息:" + JSON.stringify(e)) } }, 1000) }
build() { Column() { Text(this.message) .id('HelloWorld') .fontSize(15) .fontWeight(FontWeight.Bold) .textAlign(TextAlign.Center) .width('100%') }.width('100%') }}
复制代码

2、HarmonyOS Button 上使用 bindPopup,里面是一个自定义的组件展示内容,展示内容怎么能根据外部的变量动态展示呢?

Button 上使用 bindPopup,里面是一个自定义的组件展示内容,请问展示内容怎么能根据外部的变量动态展示呢


因为 bindPopup 里非 @component 的子组件,所以无法使用 @state 和 @link 声明式变量进行传递和更新。可以使用 AppStorage,对某一个收藏夹的是否公开属性进行新建或更新,使用为 AppStorage.SetOrCreate(‘album_id’, this.pub ? ‘0’ : “1”),然后在实现 bindPopup 时通过声明 @StorageLink(‘Album_id’) pub:string = ‘0’ 进行同步更新,获取数据并使用。


Demo:


export enum PopoverItemType {  Default,  AddVideo,  ManageVideo,  ModifyName,  ModifyPrivacy,  DeleteFolder}
export interface PopoverItem { title: string type: PopoverItemType}

@Componentexport struct CollectionManageMenu { @State popoverItemList1: PopoverItem[] = [] @State popoverItemList2: PopoverItem[] = [] @StorageLink('pub') pub: string = '0'

aboutToAppear(): void { this.popoverItemList1 = [ { title: '添加视频', type: PopoverItemType.AddVideo }, { title: '批量管理视频', type: PopoverItemType.ManageVideo }, { title: '修改名称', type: PopoverItemType.ModifyName }, { //TODO:文案要根据状态动态改变 title: '设置为私密', type: PopoverItemType.ModifyPrivacy }, { title: '删除收藏夹', type: PopoverItemType.DeleteFolder } ]
this.popoverItemList2 = [ { title: '添加视频', type: PopoverItemType.AddVideo }, { title: '批量管理视频', type: PopoverItemType.ManageVideo }, { title: '修改名称', type: PopoverItemType.ModifyName }, { //TODO:文案要根据状态动态改变 title: '设置为公开', type: PopoverItemType.ModifyPrivacy }, { title: '删除收藏夹', type: PopoverItemType.DeleteFolder } ] }
build() { Column() { List() { ForEach(this.pub == '0' ? this.popoverItemList1 : this.popoverItemList2, (item: PopoverItem, index) => { ListItem() { Text(item.title) } .height(52) .padding({ left: 16, right: 16, top: 14, bottom: 14 }) .onClick(() => {
}) }, (item: PopoverItem) => JSON.stringify(item.title)) } .width('100%') .scrollBar(BarState.Off) } .width(161) .alignItems(HorizontalAlign.Center) .borderRadius(8) }}
@Entry@Componentexport struct CollectionDetailPageNavBar { @State showPopover: boolean = false @State pub: boolean = true @Builder manageMenu() { CollectionManageMenu() }
build() { Column() { Button('管理') .width(62) .height(32) .borderRadius(16) .fontSize(15) .onClick(() => { this.showPopover = !this.showPopover }) .bindPopup( this.showPopover, { builder: () => { this.manageMenu() }, radius: 8, enableArrow: true, placement: Placement.BottomLeft, targetSpace: 20, offset: { x: -6 }, onStateChange: (event) => { if (!event.isVisible) { this.showPopover = false } } } )
Button('改变私密/公开状态') .width(62) .height(32) .borderRadius(16) .fontSize(15) .onClick(() => { this.pub = !this.pub AppStorage.SetOrCreate('pub', this.pub ? '0' : "1") }) } }}
复制代码

3、HarmonyOS 如何禁止自定义弹窗返回键关闭?

@Entry 修饰的组件能获取返回事件的监听,可以通过重写 onBackPress 监听到返回事件的按下。

4、HarmonyOS 使用 navigation 方式加载页面,折叠屏全屏二级页面第一个页面不显示返回按钮?

一级页面 是通过 router.pushUrl 跳转到 二级页面的,二级页面是使用 Navigation 方法写的,二级页面的第一个页面跳转到二级页面的第二个页面是通过 this.pageInfos.pushPathByName() 方法跳转的。


关于这个问题:Navigation 在使用时 hideBackButton 隐藏标题栏中的返回键。 不支持隐藏 NavDestination 组件标题栏中的返回键默认值:false


true: 隐藏返回键。false: 显示返回键。说明:返回键仅针对 titleMode 为 NavigationTitleMode.Mini 时才生效


对应文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navigation-V5

5、HarmonyOS 页面布局如何能根据显示大小做到同步缩放?

可使用响应式布局动态调整页面,请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/responsive-layout-V5


自适应布局可以保证窗口尺寸在一定范围内变化时,页面的显示是正常的。但是将窗口尺寸变化较大时(如窗口宽度从 400vp 变化为 1000vp),仅仅依靠自适应布局可能出现图片异常放大或页面内容稀疏、留白过多等问题,此时就需要借助响应式布局能力调整页面结构。


响应式布局是指页面内的元素可以根据特定的特征(如窗口宽度、屏幕方向等)自动变化以适应外部容器变化的布局能力。响应式布局中最常使用的特征是窗口宽度,可以将窗口宽度划分为不同的范围(下文中称为断点)。当窗口宽度从一个断点变化到另一个断点时,改变页面布局(如将页面内容从单列排布调整为双列排布甚至三列排布等)以获得更好的显示效果。

发布于: 刚刚阅读数: 5
用户头像

轻口味

关注

🏆2021年InfoQ写作平台-签约作者 🏆 2017-10-17 加入

Android、音视频、AI相关领域从业者。 欢迎加我微信wodekouwei拉您进InfoQ音视频沟通群 邮箱:qingkouwei@gmail.com

评论

发布
暂无评论
【每日学点鸿蒙知识】网络请求回调toast问题、Popup问题、禁止弹窗返回、navigation折叠屏不显示返回键、响应式布局_HarmonyOS_轻口味_InfoQ写作社区