HarmonyOS NativeXComponent获取window问题?

Native中获取XComponent window,只能通过map方式来存储NativeXComponent,然后使用时通过id查找吗,我看官方例子都是这样的,有没有其他方式获取,比如可以在ArkTS层监听onCreate回调,然后再通过native获取对应的window不呢?

阅读 679
1 个回答

一、获取Xcomponent对应的Native Window:参考https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/native-window-guidelines-V5可直接在事件回调OnSurfaceCreatedCB中获取到对应的Native XComponent 和 Native Window

二、在ArkTS获取id和XCompont的映射关系:由于id和controller都是ArkTS 组件指定的,context可在onLoad回调获取,可在ArkTS 建立id和context的映射关系,context上可以挂载Native的方法,参考https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/drawing-guidelines-V5Native Window可在OnSurfaceCreatedCB获取通过context上挂载的方法可以操作Native Window

三、如希望ArkTS能响应特定的Native事件,可参考:

  1. https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/drawing-guidelines-V5参考示例代码中绑定方法到context上的部分,绑定一个回调注册函数如SetEventFunction
  2. 在ArkTS侧声明回调函数,如doAction,在onLoad获取到context后,注册该回调到Nativethis.context.SetEventFunction(this.doAction.bind(this))
  3. 参考https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/use-napi-asynchronous-task-V5在Native侧的SetEventFunction解析并创建该回调的referencenapi\_create\_reference(env, args[0], 1, &ref)
  4. Native事件触发后,调用ArkTS的回调napi\_get\_reference\_value(env, ref, &callback)…napi\_call\_function(env, undefined, callback, 1, callbackArg, &result)参数可以携带id等标识
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进