鸿蒙开发5.0【C-API版本XComponent接入】

C-API 版本使用XComponent总共分成了两个步骤:

  1. createSurface的时候创建XComponentSurface;
  2. startSurface的时候将CPP的XComponentSurface连接到ArkUI的Xcomponent上。

createSurface的时候主要做了以下的操作:

  1. 创建并将XComponentSurface记录到Map中:

    
    void RNInstanceCAPI::createSurface(facebook::react::Tag surfaceId, std::string const& moduleName) {
    
    m_surfaceById.emplace(
    
    surfaceId,
    
    XComponentSurface(
    
    // ···
    
    surfaceId,
    
    moduleName)
    
    );
    
    }
    
  2. 在XComponentSurface中创建rootView,用于挂载C-API的组件,并在Surface上统一处理Touch事件:

    
    XComponentSurface::XComponentSurface(
    
    //···
    
    SurfaceId surfaceId,
    
    std::string const& appKey)
    
    :
    
    //···
    
    m_nativeXComponent(nullptr),
    
    m_rootView(nullptr),
    
    m_surfaceHandler(SurfaceHandler(appKey, surfaceId)) {
    
    m_scheduler->registerSurface(m_surfaceHandler);
    
    m_rootView = componentInstanceFactory->create(
    
    surfaceId, facebook::react::RootShadowNode::Handle(), "RootView");
    
    m_componentInstanceRegistry->insert(m_rootView);
    
    m_touchEventHandler = std::make_unique<SurfaceTouchEventHandler>(m_rootView);
    
    }
    

startSurface的时候主要做了以下的操作:

  1. 在ArkTS侧创建XComponent,并设置id,type与libraryname属性。其中:

    • id:组件的唯一标识,又由InstanceID和SurfaceID共同组成,记录了此XComponent属于哪一个Instance与Surface;

    • type:node,标识该XComponent是一个占位组件,组件的实现都在C-API侧;

    • libraryname:表示C-API组件在哪个so库中实现,并加载该so库,自动调用该so中定义的Init函数。当前React Native for OpenHarmony默认的so名字rnoh_app。

      
      XComponent({
      
      id: this.ctx.rnInstance.getId() + "_" + this.surfaceHandle.getTag(),
      
      type: "node",
      
      libraryname: 'rnoh_app'
      
      })
      
  2. 在CPP侧的Init中调用registerNativeXComponent函数,该函数中调用了OH_NativeXComponent_GetXComponentId用于获取ArkTS设置的id,并根据id找到对应的Instance与Surface。同时还要获取nativeXComponent对象,记录ArkTS侧的XComponent。

    
    if (OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize) !=
    
    OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
    
    ···
    
    }
    
    std::string xcomponentStr(idStr);
    
    std::stringstream ss(xcomponentStr);
    
    std::string instanceId;
    
    std::getline(ss, instanceId, '_');
    
    std::string surfaceId;
    
    std::getline(ss, surfaceId, '_');
    
  3. 调用OH_NativeXComponent_AttachNativeRootNode,将XComponentSurface中记录的rootView连接到ArkTS侧的XComponent上:

    
    OH_NativeXComponent_AttachNativeRootNode(
    
    nativeXComponent,
    
    rootView.getLocalRootArkUINode().getArkUINodeHandle());
    

以上就是本篇文章所带来的鸿蒙开发中一小部分技术讲解;想要学习完整的鸿蒙全栈技术。可以在结尾找我可全部拿到!
下面是鸿蒙的完整学习路线,展示如下:
1

除此之外,根据这个学习鸿蒙全栈学习路线,也附带一整套完整的学习【文档+视频】,内容包含如下

内容包含了:(ArkTS、ArkUI、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、鸿蒙南向开发、鸿蒙项目实战)等技术知识点。帮助大家在学习鸿蒙路上快速成长!

鸿蒙【北向应用开发+南向系统层开发】文档

鸿蒙【基础+实战项目】视频

鸿蒙面经

2

为了避免大家在学习过程中产生更多的时间成本,对比我把以上内容全部放在了↓↓↓想要的可以自拿喔!谢谢大家观看!
3

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值