C-API组件混合方案的使用
需要注意的是,在 C-API 中可以使用的 ArkTS 组件,当前仅支持叶子节点,如
Image,不支持容器节点,如Stack。
C-API中渲染ArkTS组件
-
实现自定义组件,并在业务代码中使用。
-
创建一个自定义的构建函数,使用
@Builder进行装饰。在构建函数中设计工厂方法,用于根据ComponentName构建对应的组件。参数是ComponentBuilderContext,记录了组件构建时需要用到的信息,如ComponentName、Tag等。需要注意的是,工厂方法需要被一个Stack组件包裹,该Stack组件需要设置position属性为(0,0),用于保证组件显示的位置正确。@Builder export function buildCustomComponent(ctx: ComponentBuilderContext) { Stack() { if (ctx.componentName === MarqueeView.NAME) { MarqueeView({ ctx: ctx.rnComponentContext, tag: ctx.tag }) } } .position({ x: 0, y: 0 }) } -
封装构建函数为
wrapBuilder,并作为参数传递给RNApp或是RNComponentContext,RNComponentContext是RNSurface的必要参数。const wrappedCustomRNComponentBuilder = wrapBuilder(buildCustomRNComponent); ··· // 作为RNApp的参数 RNApp({ ··· wrappedCustomRNComponentBuilder: wrappedCustomRNComponentBuilder, ··· }) // 作为RNComponentContext的参数 new RNComponentContext( RNOHContext.fromCoreContext(rnohCoreContext!, rnInstance), wrappedCustomRNComponentBuilder, wrapBuilder(buildRNComponentForTag), new Map(), ) -
在创建
RNApp或RNInstance的时候,将组件的 name 放到数组中,并作为arkTsComponentNames的参数传入:const arkTsComponentNames = [MarqueeView.NAME]; // 作为RNApp的参数 RNApp({ rnInstanceConfig: { ··· arkTsComponentNames: arkTsComponentNames }, ··· }) // 创建RNInstance时传入 await rnohCoreContext.createAndRegisterRNInstance({ ··· arkTsComponentNames: arkTsComponentNames }); -
使用
RNApp或者RNSurface启动 React Native for OpenHarmony。