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

【每日学点HarmonyOS Next知识】网络请求回调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
@Component
struct 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
}


@Component
export 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
@Component
export 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),仅仅依靠自适应布局可能出现图片异常放大或页面内容稀疏、留白过多等问题,此时就需要借助响应式布局能力调整页面结构。

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

相关推荐
小小小小小星2 小时前
鸿蒙开发之ArkUI框架进阶:从声明式范式到跨端实战
harmonyos·arkui
鸿蒙小灰2 小时前
鸿蒙开发对象字面量类型标注的问题
harmonyos
鸿蒙先行者2 小时前
鸿蒙Next不再兼容安卓APK,开发者该如何应对?
harmonyos
YF云飞5 小时前
.NET 在鸿蒙系统(HarmonyOS Next)上的适配探索与实践
华为·.net·harmonyos
Quarkn9 小时前
鸿蒙原生应用ArkUI之自定义List下拉刷新动效
list·harmonyos·arkts·鸿蒙·arkui
AlbertZein10 小时前
HarmonyOS5 凭什么学鸿蒙 —— Context详解
harmonyos
whysqwhw18 小时前
鸿蒙音频播放方式总结
harmonyos
whysqwhw18 小时前
鸿蒙音频录制方式总结
harmonyos
zhanshuo20 小时前
HarmonyOS 实战:用 @Observed + @ObjectLink 玩转多组件实时数据更新
harmonyos
zhanshuo20 小时前
鸿蒙任务调度机制深度解析:优先级、时间片、多核与分布式的流畅秘密
harmonyos