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

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

HarmonyOS
2024-05-23 23:12:43
浏览
收藏 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)); 
}

核心代码解释

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) 
  } 
}
分享
微博
QQ
微信
回复
2024-05-24 23:05:28
相关问题
状态装饰器 ui不刷新问题
1101浏览 • 1回复 待解决
关于emitter、eventHub使用场景
820浏览 • 1回复 待解决
支付成功没有收到回调?
470浏览 • 1回复 待解决
TaskPool里面是否可以使用EventHub
492浏览 • 1回复 待解决
匿名内部类如何更新外部类变量
401浏览 • 0回复 待解决
自定义装饰使用问题
216浏览 • 0回复 待解决
Aspect工具装饰使用示例
539浏览 • 2回复 待解决