HarmonyOS 鸿蒙Next中调用截图createFromBuilder接口报错TypeError: Cannot read property observeComponentCreation2 of undefined

HarmonyOS 鸿蒙Next中调用截图createFromBuilder接口报错TypeError: Cannot read property observeComponentCreation2 of undefined 代码如下:

import { image } from '@kit.ImageKit';
import { UIContext } from '@kit.ArkUI';

@Entry
@Component
struct ComponentSnapshotExample {
  @State pixmap: image.PixelMap | undefined = undefined;
  uiContext: UIContext = this.getUIContext();

  build() {
    Column() {
      Button("click to generate UI snapshot")
        .onClick(async () => {
          await buildMarkerSnapshot(this.uiContext)
        })
      Image(this.pixmap)
        .margin(10)
        .height(200)
        .width(200)
        .border({ color: Color.Black, width: 2 })
    }.width('100%').margin({ left: 10, top: 5, bottom: 5 }).height(300)
  }
}

@Builder
function RandomBuilder() {
  Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
    Text('Test menu item 1')
      .fontSize(20)
      .width(100)
      .height(50)
      .textAlign(TextAlign.Center)
    Divider().height(10)
    Text('Test menu item 2')
      .fontSize(20)
      .width(100)
      .height(50)
      .textAlign(TextAlign.Center)
  }
  .width(100)
  .id("builder")
}

async function buildMarkerSnapshot(uiContext: UIContext | undefined): Promise<image.PixelMap | undefined> {
  try {
    const snapshot = uiContext?.getComponentSnapshot().createFromBuilder(
      () => {
        RandomBuilder()
      },
    )
    return snapshot;
  } catch (e) {
    const err = e as Record<string, Object>
    const code = err.code
    const message = err.message
    return undefined
  }
}

更多关于HarmonyOS 鸿蒙Next中调用截图createFromBuilder接口报错TypeError: Cannot read property observeComponentCreation2 of undefined的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

【问题定位】

createFromBuilder在进行离屏截图时,不支持传入全局builer,在文档已有相关说明,详见Class (ComponentSnapshot)

【解决方案】

如果希望正确截图,可以将上述示例中的全局函数放到自定义组件内部,代码如下。

import { image } from '@kit.ImageKit';
import { UIContext } from '@kit.ArkUI';

@Entry
@Component
struct ComponentSnapshotExample {
  @State pixmap: image.PixelMap | undefined = undefined;
  uiContext: UIContext = this.getUIContext();

  @Builder
  RandomBuilder() {
    Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
      Text('Test menu item 1')
        .fontSize(20)
        .width(100)
        .height(50)
        .textAlign(TextAlign.Center)
      Divider().height(10)
      Text('Test menu item 2')
        .fontSize(20)
        .width(100)
        .height(50)
        .textAlign(TextAlign.Center)
    }
    .width(100)
    .id("builder")
  }

  async buildMarkerSnapshot(uiContext: UIContext | undefined): Promise<image.PixelMap | undefined> {
    try {
      const snapshot = uiContext?.getComponentSnapshot().createFromBuilder(
        () => {
          this.RandomBuilder()
        },
      )
      return snapshot;
    } catch (e) {
      const err = e as Record<string, Object>
      const code = err.code
      const message = err.message
      return undefined
    }
  }

  build() {
    Column() {
      Button("click to generate UI snapshot")
        .onClick(async () => {
          this.pixmap = await this.buildMarkerSnapshot(this.uiContext)
        })
      Image(this.pixmap)
        .margin(10)
        .height(200)
        .width(200)
        .border({ color: Color.Black, width: 2 })
    }.width('100%').margin({ left: 10, top: 5, bottom: 5 }).height(300)
  }
}

此时运行代码,可以成功截图。

更多关于HarmonyOS 鸿蒙Next中调用截图createFromBuilder接口报错TypeError: Cannot read property observeComponentCreation2 of undefined的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


有版本混用吗

该错误提示observeComponentCreation2未定义,表明调用createFromBuilder时传入的Builder对象不完整或API已变更。在HarmonyOS Next中,截图接口需从@ohos.multimedia.screenshot导入,并通过createWindowBuildercreateComponentBuilder正确构建Builder实例。确认Builder已调用observeComponentCreation2方法或改用新的createAsync接口传递组件实例。请使用最新SDK并核对API签名。

错误原因是 uiContext 在 struct 字段初始化时获取,此时组件尚未挂载,this.getUIContext() 返回 undefined。随后调用 uiContext?.getComponentSnapshot().createFromBuilder(...) 时,uiContext 实际为 undefined,导致内部方法链报出 observeComponentCreation2 of undefined 异常。正确的做法是在实际需要截图时(如按钮点击回调)再调用 this.getUIContext() 获取有效的上下文。

回到顶部