OpenHarmony开发者论坛

标题: 在OH5.0系统使用 XComponent 在 .onLoad方法无法获取有效的上下文信息 [打印本页]

作者: 飞龙腾天_UQOMO    时间: 2024-12-3 11:38
标题: 在OH5.0系统使用 XComponent 在 .onLoad方法无法获取有效的上下文信息
[md]### 【问题描述】

1. 介绍问题现象和发生的背景
   使用XComponent 官方例子工程时发现无法从onLoad方法中获取上下文信息

   https://gitee.com/openharmony/ap ... e/Native/XComponent
2. 相关的代码(请勿使用截图)

   ```
   /*
    * Copyright (c) 2023 Huawei Device Co., Ltd.
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *     http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */

   import XComponentContext from "../interface/XComponentContext"

   @Entry
   @Component
   struct Index {
     @State currentStatus: string = "init";
     private xComponentContext: XComponentContext | undefined = undefined;

     build() {
       Column() {
         Row() {
           Text('Native XComponent Sample')
             .fontSize('24fp')
             .fontWeight(500)
             .margin({
               left: 24,
               top: 12
             })
         }
         .margin({ top: 24 })
         .width('100%')
         .height(56)

         Column({ space: 10 }) {
           XComponent({
             id: 'xcomponentId',
             type: XComponentType.SURFACE,
             libraryname: 'nativerender'
           })
             .onLoad((xComponentContext) => {
               this.xComponentContext = xComponentContext as XComponentContext;
               this.currentStatus = "index";
               console.log("xft xComponentContext1:" +  this.xComponentContext);
               if(!!( this.xComponentContext ))
               {
                 console.log("xft xComponentContext2:" +  this.xComponentContext);
               }
             })
             .onDestroy(() => {
               console.log('onDestroy');
             })
             .id("xcomponent")

           Text(this.currentStatus)
             .fontSize('24fp')
             .fontWeight(500)
         }
         .onClick(() => {
           let hasChangeColor: boolean = false;
           if (this.xComponentContext && this.xComponentContext.getStatus()) {
             hasChangeColor = this.xComponentContext.getStatus().hasChangeColor;
           }
           if (hasChangeColor) {
             this.currentStatus = "change color";
           }
         })
         .margin({
           top: 27,
           left: 12,
           right: 12
         })
         .height('40%')
         .width('90%')

         Row() {
           Button('Draw Star')
             .fontSize('16fp')
             .fontWeight(500)
             .margin({ bottom: 24 })
             .onClick(() => {
               if (this.xComponentContext) {
                 this.xComponentContext.drawPattern();
                 let hasDraw: boolean = false;
                 if (this.xComponentContext.getStatus()) {
                   hasDraw = this.xComponentContext.getStatus().hasDraw;
                 }
                 if (hasDraw) {
                   this.currentStatus = "draw star";
                 }
               }
             })
             .width('53.6%')
             .height(40)
         }
         .width('100%')
         .justifyContent(FlexAlign.Center)
         .alignItems(VerticalAlign.Bottom)
         .layoutWeight(1)
       }
       .width('100%')
       .height('100%')
     }
   }
   ```
3. 运行结果、错误截图

   ![image.png](https://forums-obs.openharmony.c ... ch55hahw57eeg6b.png "image.png")

   从截图中可以看到打印的上下文object是 undefine。
4. 我尝试过的解决方法和结果

   在另一款开发板上用OH5.0的系统跑这个例子是同样的情况。
5. 我想要达到的结果

   能够在XComponent中通过onLoad方法获取有效的上下文信息。

### 【运行环境】

硬件:迅为RK3568开发板
ROM版本:OpenHarmony-5.0.0-Release
DevEvoStudio版本:5.0.3.910
SDK版本:API12
[/md]
作者: 深开鸿_王石    时间: 2024-12-9 18:26
你试试这个库,https://gitee.com/openharmony-sig/ohos_ijkplayer,我这边可以在5.0上跑,
作者: 飞龙腾天_UQOMO    时间: 2024-12-10 09:41
回复 深开鸿_王石: 我最开始是在这个ohos_ijkplayer工程里发现这个问题的,在onload中无法执行 initDelayPlay 这个函数,后面我就在Xcomponent 例子工程中复现了这个情况,其它的Xcomponent 都是这个问题,都拿不到有效的event
作者: 飞龙腾天_UQOMO    时间: 2024-12-10 09:45
回复 深开鸿_王石: 我也换了其它 的 IDE版本和FULL-SDK 还是有这个问题,您测试用的哪一版本IDE和 SDK 我这边也试一下, 因项目需要用到RTSP的播放和存储,目前在ohos_ijkplayer 工程中在这个onload方法这里给难住了
作者: fengyunrenwu    时间: 2024-12-10 10:33
code/BasicFeature/Native/XComponent内没有相关代码,看描述应该是同目录下的NdkXComponent的示例代码,测试完,问题没能复现。
作者: 飞龙腾天_UQOMO    时间: 2024-12-10 10:48
回复 fengyunrenwu: 你们测试用的哪一版本IDE和 SDK 可以发我一下吗
作者: 深开鸿_王石    时间: 2024-12-10 17:56
回复 飞龙腾天_UQOMO: Build Version: 5.0.3.900, built on October 8, 2024,dayu200,OpenHarmony:5.0.071
作者: fengyunrenwu    时间: 2024-12-11 11:25
回复 飞龙腾天_UQOMO: RK3568镜像:https://cidownload.openharmony.c ... -Release_img.tar.gz

SDK: https://cidownload.openharmony.c ... _4.0-release.tar.gz

IDE应该是没有影响的





欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5