积分0 / 贡献0

提问1答案被采纳0文章0

作者动态

    在OH5.0系统使用 XComponent 在 .onLoad方法无法获取有效的上下文信息

    飞龙腾天_UQOMO 显示全部楼层 发表于 昨天 11:38

    【问题描述】

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

      https://gitee.com/openharmony/applications_app_samples/tree/OpenHarmony-5.0.0-Release/code/BasicFeature/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

      从截图中可以看到打印的上下文object是 undefine。

    4. 我尝试过的解决方法和结果

      在另一款开发板上用OH5.0的系统跑这个例子是同样的情况。

    5. 我想要达到的结果

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

    【运行环境】

    硬件:迅为RK3568开发板 ROM版本:OpenHarmony-5.0.0-Release DevEvoStudio版本:5.0.3.910 SDK版本:API12

    您尚未登录,无法参与评论,登录后可以:
    参与开源共建问题交流
    认同或收藏高质量问答
    获取积分成为开源共建先驱

    Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

    返回顶部