HarmonyOS鸿蒙Next中attributeModifier中使用onVisibleAreaChange崩溃?

HarmonyOS鸿蒙Next中attributeModifier中使用onVisibleAreaChange崩溃?

[a href=“https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-universal-attributes-attribute-modifier#attributemodifier” target="_blank"]attributemodifier官网文档[/a]

文档中不支持列表不包含这个onVisibleAreaChange

下面代码运行报错:

@Entry
@Component
struct AttributeModifier0 {
  build() {
    Column() {
      Text('测试').attributeModifier(new TitleImgMod())
    }
  }
}

export class TitleImgMod implements AttributeModifier<TextAttribute> {
  constructor() {
  }

  applyNormalAttribute(instance: TextAttribute): void {
    instance
      .onVisibleAreaChange([0, 1], (isEx, ratio) => {
      })
    // Error message:Method not implemented.
    // Stacktrace:
    //     at onVisibleAreaChange
  }
}

更多关于HarmonyOS鸿蒙Next中attributeModifier中使用onVisibleAreaChange崩溃?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

8 回复

经过验证,确实不支持

以下表里没有列举全面:

表1 CommonAttribute属性接口支持例外范围

属性 支持情况 告警信息 备注
accessibilityChecked 不支持 is not callable -
accessibilitySelected 不支持 is not callable -
accessibilityTextHint 不支持 is not callable -
accessibilityVirtualNode 不支持 is not callable 不支持入参为CustomBuilder。
animation 不支持 Method not implemented. 不支持animation相关属性。
attributeModifier 不支持 - attributeModifier不支持嵌套使用,不生效。
background 不支持 Method not implemented. 不支持入参为CustomBuilder。
backgroundFilter 不支持 is not callable -
bindContentCover 不支持 Method not implemented. 不支持入参为CustomBuilder。
bindContextMenu 不支持 Method not implemented. 不支持入参为CustomBuilder。
bindPopup 不支持 Method not implemented. 不支持入参为CustomBuilder。
bindSheet 不支持 Method not implemented. 不支持入参为CustomBuilder。
chainWeight 不支持 is not callable -
compositingFilter 不支持 is not callable -
drawModifier 不支持 is not callable 不支持modifier相关的属性。
foregroundFilter 不支持 is not callable -
freeze 不支持 is not callable -
gesture 不支持 Method not implemented. 不支持gesture相关的属性。
gestureModifier 不支持 is not callable 不支持modifier相关的属性。
onAccessibilityHover 不支持 is not callable -
onDigitalCrown 不支持 is not callable. -
onDragStart 不支持 Method not implemented. 不支持返回值为CustomBuilder。
parallelGesture 不支持 Method not implemented. 不支持gesture相关的属性。
priorityGesture 不支持 Method not implemented. 不支持gesture相关的属性。
reuseId 不支持 Method not implemented. -
stateStyles 不支持 Method not implemented. 不支持stateStyles相关的属性。
useSizeType 不支持 Method not implemented. 不支持已废弃属性。
visualEffect 不支持 is not callable -
bindMenu 部分支持 - 不支持入参为CustomBuilder。
dragPreview 部分支持 Builder is not supported. 不支持入参为CustomBuilder。

更多关于HarmonyOS鸿蒙Next中attributeModifier中使用onVisibleAreaChange崩溃?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


onVisibleAreaChange属性属于组件特有事件监听方法,而AttributeModifier目前不支持事件类属性,

TextAttribute在applyNormalAttribute回调中未开放onVisibleAreaChange方法,导致调用时触发未实现错误

直接在组件声明中绑定事件试一下—

@Entry
@Component
struct AttributeModifier0 {
  build() {
    Column() {
      Text('测试')
        .attributeModifier(new TitleImgMod())
        .onVisibleAreaChange([0, 1], (isEx, ratio) => {}) // 移出AttributeModifier
    }
  }
}

export class TitleImgMod implements AttributeModifier<TextAttribute> {
  applyNormalAttribute(instance: TextAttribute): void {
    instance.fontSize(20) // 仅设置支持的属性
  }
}

所以文档里的【不支持的清单】是障眼法吗 : D,

  • 项目名称: 不支持的清单
  • 项目状态: 活跃
  • 创建者: 王晓明
  • 创建日期: 2023-05-01
  • 最近更新: 2023-05-08

  • 描述:

    • 这是一个测试项目,用于验证一些假设。
  • 成员:

    • 张三
    • 李四
    • 王五

  • 里程碑:
    • 第一阶段完成
    • 第二阶段进行中
    • 预计第三阶段开始日期:2023-05-15

我刚刚写了一遍没有报错啊

动态设置组件的属性,支持开发者在属性设置时使用if/else语法,且根据需要使用多态样式设置属性。

说明

从API version 11开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

在attributeModifier中设置的属性尽量不要与其他方法设置的属性相同,避免在页面刷新时attributeModifier不生效。

对于仅需根据条件设置组件单一属性的简单场景,可以使用三目表达式(如.width(isFullScreen ? 200 : 100))。

API version 20开始,attributeModifier支持自定义组件。

原因:可能API不到20所以不让用,我这边是API20

我是19,好像15也可以,

你的Index文件没有修改路由名的话,可能没有加载这个组件,没报错可能是这个原因,

在HarmonyOS Next中,attributeModifier使用onVisibleAreaChange崩溃可能是由于可见性回调触发时组件状态异常导致。检查是否在回调中直接修改了组件属性而未使用主线程更新UI。确保onVisibleAreaChange回调中不执行耗时操作,避免阻塞UI线程。若使用动态属性修改,需确认AttributeModifier的生命周期与组件保持一致。崩溃日志中通常会有"ui thread blocked"或"state inconsistency"相关提示。

这是一个已知的HarmonyOS Next平台限制问题。在attributeModifier中直接使用onVisibleAreaChange确实会导致崩溃,因为该方法尚未在AttributeModifier接口中实现。

目前可行的替代方案是:

  1. 直接在组件上使用onVisibleAreaChange而不是通过attributeModifier
  2. 等待后续版本修复该限制

代码可以改为:

@Entry
@Component
struct AttributeModifier0 {
  build() {
    Column() {
      Text('测试')
        .onVisibleAreaChange([0, 1], (isEx, ratio) => {})
        .attributeModifier(new TitleImgMod())
    }
  }
}

这个问题已在开发者社区反馈,建议关注后续版本更新说明。

回到顶部