openharmony 鸿蒙 js-apis-arkui-xcomponentNode

2025-06-12 浏览 (1)

XComponentNode

The XComponentNode module provides APIs for the XComponentNode, which represent an XComponent in the component tree. You can write EGL/OpenGL ES and media data and display it on the XComponent, whose rendering type can be dynamically modified.

NOTE

The APIs of this module are deprecated since API version 12. You are advised to use XComponent type node for implementation instead.

The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.

XComponentNode is not available in DevEco Studio Previewer.

Modules to Import

import { XComponentNode } from "@kit.ArkUI";

XComponentNode

constructor

constructor(uiContext: UIContext, options: RenderOptions, id: string, type: XComponentType, libraryName?: string)

A constructor used to create an XComponentNode.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
uiContextUIContextYesUI context. For details about how to obtain it, see Obtaining UI Context.
optionsRenderOptionsYesParameters for creating an XComponentNode.
idstringYesUnique ID of the XComponent. The value can contain a maximum of 128 characters. For details, see XComponent.
typeXComponentTypeYesType of the XComponent. For details, see XComponent.
libraryNamestringNoName of the dynamic library generated during compilation at the native layer. For details, see XComponent.

NOTE

You need to explicitly specify selfIdealSize in RenderOptions. Otherwise, the XComponentNode's content size is empty, resulting in no content being displayed.

onCreate

onCreate(event?: Object): void

Called when the XComponentNode loading is complete.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
eventObjectNoContext of the XComponent object. The APIs contained in the context are defined by you at the C++ layer.

onDestroy

onDestroy(): void

Called when the XComponentNode is destroyed.

System capability: SystemCapability.ArkUI.ArkUI.Full

changeRenderType

changeRenderType(type: NodeRenderType): boolean

Changes the rendering type of the XComponentNode.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
typeNodeRenderTypeYesTarget rendering type.

Return value

TypeDescription
booleanWhether the rendering type is changed successfully.

Example

import { NodeController, FrameNode, XComponentNode, NodeRenderType, UIContext} from '@kit.ArkUI'

class XComponentNodeController extends NodeController {
  private xComponentNode: MyXComponentNode|null = null;
  private soName: string = "tetrahedron_napi" // This .so file is written and generated by you using the Node-API.

  constructor() {
    super();
  }

  makeNode(context: UIContext): FrameNode|null {
    this.xComponentNode = new MyXComponentNode(context, {
      selfIdealSize: { width: 200, height: 200 }
    }, "xComponentId", XComponentType.SURFACE, this.soName);
    return this.xComponentNode;
  }

  changeRenderType(renderType: NodeRenderType): void {
    if (this.xComponentNode) {
      this.xComponentNode.changeRenderType(renderType);
    }
  }
}

class MyXComponentNode extends XComponentNode {
  onCreate(event: Object) {
    // do something when XComponentNode has created
  }

  onDestroy() {
    // do something when XComponentNode is destroying
  }
}

@Entry
@Component
struct Index {
  build() {
    Row() {
      Column() {
        NodeContainer(new XComponentNodeController())
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}

XComponentNodeSample

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkUI

harmony 鸿蒙ARKUI_TextPickerCascadeRangeContent

harmony 鸿蒙ARKUI_TextPickerRangeContent

harmony 鸿蒙ArkUI_AnimateCompleteCallback

harmony 鸿蒙ArkUI_AttributeItem

harmony 鸿蒙ArkUI_ColorStop

harmony 鸿蒙ArkUI_ContextCallback

harmony 鸿蒙ArkUI_EventModule

harmony 鸿蒙ArkUI_ExpectedFrameRateRange

harmony 鸿蒙ArkUI_IntOffset

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/YXMC6R