HarmonyOS 用Navigation显示dialog问题

用Navigation显示dialog问题。在A(Navigation)页面弹出B(NavDestination、NavDestinationMode.DIALOG)dialog显示正常,然后B跳转页面C(NavDestination),侧滑返回 B dialog 显示背景变白色,具体代码看demo。请问怎么解决?

HarmonyOS
2024-10-22 11:23:02
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

因为navigation的布局是stack,弹出的页面B设置了透明度0.1,所以可以看到下层的页面A。但从C返回B时,只有页面B,所以显示背景变白色,这边建议A页面先跳转到B页面,B页面再弹出弹框,此时弹框再跳转到其他页面时,不会再出现背景页面不展示现象。

代码如下:

//主页面  
build() {  
  Navigation(this.pageInfos) {  
    Button('push Page01')  
      // Button('push Dialog01')  
      .width('80%')  
      .onClick(() => {  
        this.pageInfos.pushPathByName('Page01', ''); //此处先跳转page01  
      })  
  }  
  .mode(NavigationMode.Stack)  
  .titleMode(NavigationTitleMode.Mini)  
  .title('主页')  
  .navDestination(this.PagesMap)  
}  
}  
  
//dialog弹框组件  
build() {  
  NavDestination() {  
    Stack() {  
      Column()  
        .width('100%')  
        .height('100%')  
        .backgroundColor(Color.Gray)  
        .opacity(0.1)  
        .onClick(() => {  
          this.pageInfos.pop();  
        })  
      Column() {  
        Text('Dialog01')  
          .fontSize(30)  
          .fontWeight(2)  
        Button('push Page02')  
          .width('80%')  
          .onClick(() => {  
            this.pageInfos.pushPathByName('Page02', ''); //Dialog01弹框处再跳转page02(两层页面弹框跳转后再返回页面,背景不会消失)  
          })
  • 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.

C页面返回B页面时A页面不显示是因为A页面没有放在路由栈里,可以将A页面路由放在aboutToAppear生命周期中,参考代码如下:

@Entry  
@Component  
struct Index {  
  @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack()  
  isLogin: boolean = false;  
  @State pathUrl:Array<string> = [];  
  
  @Builder  
  PagesMap(name: string) {  
    if (name == 'Page00') {  
      Page00()  
    } else if (name == 'Page01') {  
      Page01()  
    } else if (name == 'Dialog01') {  
      Dialog01()  
    } else if (name == 'Page02') {  
      Page02()  
    }  
  }  
  
  aboutToAppear() {  
    if (this.pageInfos === undefined) {  
      this.pageInfos = new NavPathStack();  
    }  
    this.pageInfos.pushPath({name: 'Page00'}, false)  
  }  
  
  build() {  
    Navigation(this.pageInfos) {}  
    .mode(NavigationMode.Stack)  
    .titleMode(NavigationTitleMode.Mini)  
    .title('主页')  
    .navDestination(this.PagesMap)  
    .backgroundColor('pink')  
    .width('100%')  
    .height('100%')  
    .border({width:3})  
  }  
  
  onBackPress(): boolean | void {  
    console.log("onBackPress", "Index")  
  }  
}  
  
@Component  
struct Page00 {  
  @Consume('pageInfos') pageInfos: NavPathStack;  
  build() {  
    NavDestination() {  
      Button('push Dialog')  
        .width('80%')  
        .onClick(() => {  
          this.pageInfos.pushPathByName('Dialog01', '');  
          console.log('[LOG]getAllPathName-Page02',this.pageInfos.getAllPathName())  
        })  
    }  
  }  
}  
  
@Component  
struct Page01 {  
  @Consume('pageInfos') pageInfos: NavPathStack;  
  
  build() {  
    NavDestination() {  
      Button('push Page01')  
        .width('80%')  
        .onClick(() => {  
          this.pageInfos.pushPathByName('Page01', '');  
        })  
        .margin({ top: 10, bottom: 10 })  
      Button('push Dialog01')  
        .width('80%')  
        .onClick(() => {  
          this.pageInfos.pushPathByName('Dialog01', '');  
        })  
        .margin({ top: 10, bottom: 10 })  
    }  
    .hideTitleBar(true)  
  }  
}  
  
@Component  
struct Page02 {  
  @Consume('pageInfos') pageInfos: NavPathStack;  
  
  build() {  
    NavDestination() {  
      Button('push Page02')  
        .width('80%')  
        .onClick(() => {  
        })  
        .margin({ top: 10, bottom: 10 })  
    }  
    .onBackPressed(()=>{  
      console.log('[LOG]getAllPathName-Page02',this.pageInfos.getAllPathName());  
      return false;  
    })  
    .hideTitleBar(true)  
  }  
}  
  
@Preview({  
  title: 'Dialog01'  
})  
  
@Component  
struct Dialog01 {  
  @Consume('pageInfos') pageInfos: NavPathStack;  
  
  build() {  
    NavDestination() {  
      Stack() {  
        Column()  
          .width('100%')  
          .height('100%')  
          .backgroundColor(Color.Gray)  
          .opacity(0.1)  
          .onClick(() => {  
            this.pageInfos.pop();  
          })  
        // Add controls for business processing  
        Column() {  
          Text('Dialog01')  
            .fontSize(30)  
            .fontWeight(2)  
          Button('push Page02')  
            .width('80%')  
            .onClick(() => {  
              this.pageInfos.pushPathByName('Page02', '');  
              console.log('[LOG]getAllPathName-Dialog01',this.pageInfos.getAllPathName())  
            })  
            .margin({ top: 10, bottom: 10 })  
          Button('push Dialog01')  
            .width('80%')  
            .onClick(() => {  
              this.pageInfos.pushPathByName('Dialog01', '');  
            })  
            .margin({ top: 10, bottom: 10 })  
          Button('pop')  
            .width('80%')  
            .onClick(() => {  
              this.pageInfos.pop();  
            })  
            .margin({ top: 10, bottom: 10 })  
        }  
        .padding(10)  
        .width(250)  
        .backgroundColor(Color.White)  
        .borderRadius(10)  
      }  
    }  
    .hideTitleBar(true)  
    // Set the mode property of this NavDestination to DIALOG  
    .mode(NavDestinationMode.DIALOG)  
    .onBackPressed(() => {  
      console.log('[LOG]getAllPathName-Dialog01Back',this.pageInfos.getAllPathName())  
      return this.onBackPress();  
    })  
  }  
  
  onBackPress(): boolean {  
    console.log("onBackPress", "Dialog01")  
    return false  
  }  
}
  • 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.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
分享
微博
QQ
微信
回复
2024-10-22 16:31:27
相关问题
HarmonyOS 自定义Dialog显示问题
1318浏览 • 1回复 待解决
HarmonyOS Navigation实现Dialog转场动画
720浏览 • 1回复 待解决
HarmonyOS CustomDialog自定义Dialog
1172浏览 • 1回复 待解决
HarmonyOS Navigation和router怎么
1033浏览 • 1回复 待解决
HarmonyOS dialog调用open方法,不显示
1129浏览 • 1回复 待解决
HarmonyOS Navigation无法正确显示
678浏览 • 1回复 待解决
路由导航router还是navigation
366浏览 • 1回复 待解决
HarmonyOS dialog的创建问题
1287浏览 • 1回复 待解决
HarmonyOS dialog弹窗相关问题
1147浏览 • 1回复 待解决
HarmonyOS dialog覆盖的问题
952浏览 • 1回复 待解决
HarmonyOS dialog弹窗跳转问题
711浏览 • 1回复 待解决
HarmonyOS Navigation中title怎么居中显示
866浏览 • 1回复 待解决
HarmonyOS关于navigation问题
1518浏览 • 1回复 待解决
Dialog如何覆盖状态栏全屏显示
11232浏览 • 1回复 待解决
HarmonyOS Navigation使用问题
1287浏览 • 1回复 待解决
HarmonyOS Navigation 使用问题
1192浏览 • 1回复 待解决
HarmonyOS Navigation路由问题
782浏览 • 1回复 待解决