【每日学点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 小时前
基于hispark_taurus开发板示例学习OpenHarmony编译(1)
操作系统·harmonyos
盖世栗子4 小时前
鸿蒙开发父组件调用子组件方法
harmonyos
二川bro7 小时前
华为鸿蒙系统全景解读:从内核设计到生态落地的技术革命
华为·harmonyos
轻口味7 小时前
【每日学点HarmonyOS Next知识】tabs切换卡顿、输入框焦点、打开全新web、输入框密码类型、非法变量值
华为·harmonyos·harmonyosnext
Bruce_Liuxiaowei9 小时前
HarmonyOS Next~应用开发入门:从架构认知到HelloWorld实战
华为·架构·harmonyos
轻口味10 小时前
【每日学点HarmonyOS Next知识】动图循环播放、监听tab切换、富文本上下滚动、tab默认居中、a标签唤起拨号
华为·harmonyos·harmonyosnext
十九遇你 九十与你12 小时前
在华为设备上,VRRP与BFD结合使用可以快速检测链路故障并触发主备切换
网络·华为
敢嗣先锋12 小时前
鸿蒙5.0实战案例:基于OpenGL渲染视频画面帧
移动开发·音视频·harmonyos·arkts·opengl·arkui·鸿蒙开发
桃子酱紫君14 小时前
华为配置篇-OSPF基础实验
华为