HarmonyOS 通过子组件调用父组件方法修改父组件的值后,如何显示以及动态刷新父组件bindsheet标题栏的UI

父组件里用的bindsheet组件有个标题栏需要根据子组件滑动页面里的某个值,做到动态变化显示,

但是当前情况是日志打印可以看到刷新函数里的值获取到新的,父组件的文字却没有显示(只有按钮显示了,文字一直是空白),没有滑动子组件时:

父组件:

class TitleBuildParams {
  tileDisplayName: string = "";
}

const TAG = 'ParentView';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State parentSheetShow: boolean = false;
  @State mParentDisplayName: string = '';

  private updateDescription(formItem: string): void {
    this.mParentDisplayName = formItem;
    this.message = formItem;
    console.log(`onChange parent: ${this.mParentDisplayName}`);
  }

  @Builder
  bindSheetBuilder() {
    Column() {
      ChildComponent({
        mDisplayController: this.updateDescription
      })
    }
    .height(230)
  }


  @Builder
  titleAllBuilder() {
    this.titleBuilder({tileDisplayName: this.mParentDisplayName});
  }

  @Builder
  titleBuilder($$: TitleBuildParams) {
    Row() {
      Button() {
        Image($r('sys.media.ohos_ic_compnent_titlebar_back'))
          .width(24)
          .height(24)
          .fillColor($r('sys.color.ohos_id_color_titlebar_text'))
          .draggable(false)
      }
      .width(40)
      .height(40)
      .backgroundColor($r('sys.color.ohos_id_color_button_normal'))
      .onClick(() => {
        this.parentSheetShow = false;
      })

      Blank().width(8).visibility(Visibility.Hidden);

       //该处需要绑定显示动态刷新的值
      Text($$.tileDisplayName)
        .id(`${TAG}_Text_title`)
        .draggable(false)
        .fontColor($r('sys.color.ohos_id_color_text_primary'))
        .fontSize($r('sys.float.ohos_id_text_size_headline8'))
        .fontWeight(FontWeight.Bold)
        .lineHeight(30)
        .maxLines(1)
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .textAlign(TextAlign.Start)
    }
    .width('100%')
    .height('100%')
    .alignItems(VerticalAlign.Center)
    .justifyContent(FlexAlign.Start)
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Top },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
      Button().width(30).height(30)
        .onClick((event) => {
          console.log("click button")
          this.parentSheetShow = true;
        })
      Stack().bindSheet($$this.parentSheetShow, this.bindSheetBuilder(), {
        title: this.titleAllBuilder(),
        width: 300,
        showClose: true,
        dragBar: false,
        blurStyle: BlurStyle.COMPONENT_ULTRA_THICK,
        backgroundColor: Color.Transparent,
        detents: [, 0],
        mode: SheetMode.EMBEDDED,
        onAppear: () => {
          console.log('Parent bindSheet onAppear');
        },
        onDisappear: () => {
          console.log(`Parent bindsheet onDisappear`);
          this.parentSheetShow = false;
        }
      })
    }
    .height('100%')
    .width('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.

子组件:

@Component
export default struct ChildComponent {
  private mDisplayController?: (formItem: string) => void = () => {};
  private mDisplayList : string[]= [];

  aboutToAppear(): void {
    this.mDisplayList = ["111", "222", "333"];
  }

  build() {
    Column() {
      Swiper() {
        ForEach(this.mDisplayList, (displayName: string, item: number) => {
          Column() {
            Text(displayName).fontSize(12).width(40).height(40)
          }.height(200).width('100%')
        }, (item: string, index: number): string => {
          return `${this.mDisplayList.length}${index}`;
        })
      }
      .onChange((index: number) => {
	//子组件有一个swiper切换index时,调用父组件的函数更新父组件的值
        if (this.mDisplayController != undefined && this.mDisplayList.length > index) {
          console.log(`onChange child: ${this.mDisplayList[index]}`);
          this.mDisplayController(this.mDisplayList[index]);
        }
      })
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.

HarmonyOS 通过子组件调用父组件方法修改父组件的值后,如何显示以及动态刷新父组件bindsheet标题栏的UI -鸿蒙开发者社区

HarmonyOS
2024-12-25 12:00:40
1319浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

已达到效果,在父组件中创建子组件时,换调用方式

ChildComponent({
  mDisplayController: (formItem: string) => {
    this.updateDescription(formItem)
  }
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
分享
微博
QQ
微信
回复
2024-12-25 14:48:01
相关问题
组件调用组件方法
2227浏览 • 1回复 待解决
组件调用组件方法
1118浏览 • 1回复 待解决
HarmonyOS 组件调用组件方法
886浏览 • 1回复 待解决
HarmonyOS 组件调用组件方法demo
851浏览 • 1回复 待解决
HarmonyOS 组件怎么调用组件方法
1222浏览 • 1回复 待解决
HarmonyOS 组件超过组件范围
803浏览 • 1回复 待解决