使用EventHub通信接收到消息后更新@States装饰的变量布局刷新

使用EventHub通信接收到消息后更新@States装饰的变量布局刷新

HarmonyOS
2024-05-23 23:12:43
569浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
xiaohur

使用的核心API

callback被emit触发时,调用方是EventHub对象,如果要修改callback中this的指向,可以使用箭头函数。

onHomeStatusSwitch = (status: number) => { 
  // 触发事件,完成相应的业务操作 
  this.homeStatusData =status; 
  console.info('hh_info', '=============status=' + status); 
  // console.info('hh_info', 'data=' + JSON.stringify(data)); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

核心代码解释

import common from '@ohos.app.ability.common'; 
  
@Entry 
@Component 
struct MainPage { 
  @State tabs: string[] = []; 
  @State homeStatusData: number = 0; 
  @State currentIndex: number = 0; 
  private context = getContext(this) as common.UIAbilityContext; 
  
  aboutToAppear() { 
    this.tabs.push('首页') 
    this.tabs.push('我的') 
    this.context.eventHub.on('intent.action.home_tab.status', this.onHomeStatusSwitch); 
  } 
  
  aboutToDisappear(): void { 
    this.context.eventHub.off('intent.action.home_tab.status'); 
  } 
  
  /** 
   * 接收到首页图片切换状态的消息 
   * @param status 
   */ 
  onHomeStatusSwitch=(status: number)=> { 
    // 触发事件,完成相应的业务操作 
    this.homeStatusData =status; 
    console.info('hh_info', '=============status=' + status); 
    // console.info('hh_info', 'data=' + JSON.stringify(data)); 
  } 
  
  testValue=0; 
  build() { 
    Row() { 
      Tabs({ barPosition: BarPosition.End }) { 
        ForEach(this.tabs, (item: string, index: number) => { 
          TabContent() { 
            Column() { 
              if(this.homeStatusData==1){ 
                Text('首页--homeStatusData==1+' + item) 
              }else{ 
                Text('首页--homeStatusData==0+' + item) 
              } 
  
            }.backgroundColor(index == 0 ? Color.Yellow : Color.Pink) 
            .width('100%') 
            .height('100%') 
          } 
          .tabBar(this.tabBuilder(item, 0, $r('app.media.startIcon'), $r('app.media.startIcon'))) 
        }) 
      } 
      .onTabBarClick((index: number)=>{ 
        this.testValue=this.testValue==0?1:0; 
        this.context.eventHub.emit('intent.action.home_tab.status',this.testValue); 
      }) 
      .vertical(false) 
      .barWidth('100%') 
      .barHeight(50) 
      .animationDuration(0) 
      .barMode(BarMode.Fixed) 
      .scrollable(false) 
    } 
    .height('100%') 
  } 
  
  @Builder 
  tabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) { 
    Column() { 
      Image(this.currentIndex === targetIndex ? selectedImg : normalImg) 
        .size({ width: 25, height: 25 }) 
      Text(title) 
        .fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B') 
    } 
    .width('100%') 
    .height(50) 
    .justifyContent(FlexAlign.Center) 
  } 
}
  • 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.
分享
微博
QQ
微信
回复
2024-05-24 23:05:28
相关问题
卡片收到消息更新?
666浏览 • 1回复 待解决
HarmonyOS 接收不到eventhub事件
754浏览 • 1回复 待解决
HarmonyOS 更新数据UI不刷新
1035浏览 • 1回复 待解决
【列表数据更新页面不刷新
371浏览 • 1回复 待解决
HarmonyOS 推送后台消息,未收到
2119浏览 • 1回复 待解决