在CustomDialog中打开新的页面,CustomDialog会自动消失

在CustomDialog中打开新的页面,CustomDialog会自动消失,如何解决。

代码描述:

ActionSheetExample.ets:

import { CustomDialogExample } from'…/view/CustomDialogExample'; 
import { UIContext } from '@ohos.arkui.UIContext'; 
import common from '@ohos.app.ability.common'; 
import { DialogManager } from '…/view/DialogManager'; 
 
@Entry 
@Component 
struct ActionSheetExample { 
  @State textValue: string = '' 
  @State inputValue: string = 'click me' 
  @State angle: number = 0 
 
  dialogController: CustomDialogController | null = new CustomDialogController({ 
    builder: CustomDialogExample(), 
    autoCancel: true, 
    alignment: DialogAlignment.Bottom, 
    openAnimation: { duration: 0 }, 
    closeAnimation: { duration: 0 }, 
    offset: { dx: 0, dy: 0 }, 
    // maskColor: ‘rgba(0, 0, 0, 0)’, 
    customStyle: true 
  }) 
 
  constructor() { 
    super() 
  } 
 
  build() { 
    Column() { 
      Text('rotate').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14) 
      Row() 
        .rotate({ 
          x: 0, 
          y: 0, 
          z: 1, 
          angle: this.angle 
        })// 组件以矢量(0,0,1)为旋转轴,绕中心点顺时针旋转300度 
        .width(100) 
        .height(50) 
        .backgroundColor(0xAFEEEE) 
        .onClick(() => { 
          animateTo({ curve: Curve.Ease, onFinish: () => { 
          }, duration: 300 }, () => { 
            this.angle += 180 
          }) 
        }) 
 
      Button(this.inputValue) 
        .onClick(() => { 
          if (this.dialogController != null) { 
            this.dialogController.open() 
          } 
        }).backgroundColor(0x317aff) 
        .margin(20) 
      Button('打开CallerPage').onClick(() => { 
        let context = getContext(this) as common.UIAbilityContext 
        context.startAbility({ 
          bundleName: "com.example.myapplication", 
          abilityName: "CallerAbility", 
        }).catch((err: Error) => { 
          console.log(err.message) 
        }) 
      }) 
    }.width('100%').margin({ top: 5 }) 
  } 
}
  • 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.

CustomDialogExample.ets

import common from '@ohos.app.ability.common'; 
import router from '@ohos.router'; 
 
@CustomDialog 
@Preview 
export struct CustomDialogExample { 
  @State dialogHeight: number = 0 
  controller?: CustomDialogController 
  initOffsetY: number = 0; 
  datas: Array<number> = [1, 2] 
  initHeight: number = 60 * this.datas.length + 90 
  topBorderRadius: number = 20; 
  closeFlag: boolean = false; 
  open = () => { 
    this.moveTo(this.initHeight) 
  } 
  close = () => { 
    this.moveTo(0).then(() => { 
      this.controller?.close() 
    }) 
  } 
  moveTo = (height: number) => { 
    return new Promise<void>((res) => { 
      animateTo({ curve: Curve.Ease, onFinish: res, duration: 300 }, () => { 
        this.dialogHeight = height 
      }) 
    }) 
  } 
 
  // 阻尼函数 
  damping(x: number, max: number) { 
    let y = Math.abs(x); 
    y = 0.82231 * max / (1 + 4338.47 / Math.pow(y, 1.14791)); 
    return Math.round(x < 0 ? -y : y); 
  } 
 
  build() { 
    Column() { 
      Column() { 
        Column() { 
          Row() { 
            Text('Title') 
          } 
          .width('100%') 
          .height(30) 
            .justifyContent(FlexAlign.Center) 
 
          Scroll() { 
            Column() { 
              ForEach(this.datas, () => { 
                Column() { 
                  Image($r('app.media.startIcon')) 
                    .width(50) 
                    .height(50) 
                } 
                .onClick(()=>{ 
                  // let context = getContext(this) as common.UIAbilityContext 
                  // context.startAbility({ 
                  //   bundleName: "com.example.myapplication", 
                  //   abilityName: "CallerAbility", 
                  // }).catch((err: Error) => { 
                  //   console.log(err.message) 
                  // }) 
                  router.pushUrl({ 
                    url:'pages/Index' 
                  }) 
                }) 
                .width('100%') 
                .margin({ top: 10 }) 
                .justifyContent(FlexAlign.Center) 
              }) 
            } 
 
          } 
 
        } 
        .layoutWeight(1) 
 
        Row() { 
          Button('取消') 
            .onClick(() => { 
              this.close() 
            }).width('100%').backgroundColor(0xffffff).fontColor(Color.Black) 
        } 
        .backgroundColor(Color.Red) 
        .width('100%') 
        .height(50) 
      } 
      .justifyContent(FlexAlign.SpaceBetween) 
      .height(this.dialogHeight) 
      .width('100%') 
      .backgroundColor(Color.White) 
      .onAppear(this.open) 
      .zIndex(2) 
      .borderRadius({ topLeft: this.topBorderRadius, topRight: this.topBorderRadius }) 
 
      Column() 
        .width('100%') 
        .height('100%') 
        .backgroundColor(0x33000000) 
        .position({ x: 0, y: 0 }) 
        .zIndex(1) 
    } 
    .width('100%') 
    .height('100%') 
    .justifyContent(FlexAlign.End) 
    // .borderRadius({ topLeft: 40, topRight: 40, bottomLeft: 0, bottomRight: 0 }) 
  } 
}
  • 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.
HarmonyOS
2024-06-05 20:24:00
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
妙蛙菜籽油

通过使用Stack替代CustomDialog,再控制Stack的显隐即可达到效果。

分享
微博
QQ
微信
回复
2024-06-06 21:12:50
相关问题
HarmonyOS CustomDialog跳转其他页面问题
767浏览 • 1回复 待解决
HarmonyOS class显示CustomDialog
660浏览 • 1回复 待解决
HarmonyOS CustomDialog相关
626浏览 • 1回复 待解决