HarmonyOS 鸿蒙Next在CustomDialog中跳转的问题
HarmonyOS 鸿蒙Next在CustomDialog中跳转的问题
场景:回复列表中,点击查看更多 会通过customDialog弹出二级页面,在这个二级页面中点击查看图片会通过Navigation push到图片查看器页面,在层级上表现为图片查看器在这个customDialog的下面了,想要的效果是图片查看器页面展示在customDialog的上方。
2 回复
参考以下代码:
interface RouterParams {
name?: string,
onPop?: (data: PopInfo) => void
}
// 封装路由工具类,并注册自定义弹窗组件
class AppRouters {
private static instance = new AppRouters();
private pathStack: NavPathStack = new NavPathStack();
public static getInstance(): AppRouters {
return AppRouters.instance;
}
public getPathStack(): NavPathStack {
return this.pathStack;
}
private pushPath(name: string): void {
this.pathStack.pushPath({ name: name })
}
public static push(name: string): void {
AppRouters.instance.pushPath(name);
}
public static openDialog(name: string, params?: RouterParams): void {
AppRouters.instance.pathStack.pushPath({
name: name, param: params, onPop: (data: PopInfo) => {
if (params?.onPop) {
params.onPop!(data);
}
}
});
}
public static pop(): void {
AppRouters.instance.pathStack.pop();
}
}
[@Component](/user/Component)
// NavDestinationMode.DIALOG
struct DefaultDialog {
build() {
NavDestination() {
Stack({ alignContent: Alignment.Center }) {
Column() {
}
.width("100%")
.height("100%")
.backgroundColor('rgba(0,0,0,0.5)')
.transition(
TransitionEffect.OPACITY.animation({
duration: 300,
curve: Curve.Friction
})
)
.onClick(() => {
AppRouters.pop();
})
Column() {
Text("dialogA")
.fontColor(Color.White)
Button("push pageC", { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
AppRouters.push("pageC")
})
}
.width("50%")
.height("30%")
.backgroundColor('#ffae2d2d')
.transition(
TransitionEffect.scale({ x: 0, y: 0 }).animation({
duration: 300,
curve: Curve.Friction
})
)
}
.width("100%")
.height("100%")
}
.mode(NavDestinationMode.DIALOG)
.hideTitleBar(true)
}
}
[@Component](/user/Component)
struct PageA {
[@Consume](/user/Consume)('pageInfos') pageInfos: NavPathStack;
build() {
NavDestination() {
Button('push dialogA', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
AppRouters.openDialog("DefaultDialog");
}).margin(10)
}.title('PageA')
}
}
[@Component](/user/Component)
struct PageC {
[@Consume](/user/Consume)('pageInfos') pageInfos: NavPathStack;
build() {
NavDestination() {
Column() {
Text('pageC')
.margin(10)
Button('返回', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
AppRouters.pop();
}).margin(10)
}
}.mode(NavDestinationMode.STANDARD)
}
}
[@Entry](/user/Entry)
[@Component](/user/Component)
struct pageRoot01 {
[@Provide](/user/Provide)('pageInfos') pageInfos: NavPathStack = new NavPathStack()
// 显隐控制设置为不占用
[@State](/user/State) visible: Visibility = Visibility.None
[@Builder](/user/Builder)
PagesMap(name: string) {
if (name == 'pageA') {
PageA()
} else if (name == 'pageC') {
PageC()
} else if (name == 'DefaultDialog') {
DefaultDialog()
}
}
build() {
// 在根页面中注册NavPathStack
Navigation(AppRouters.getInstance().getPathStack()) {
Stack({ alignContent: Alignment.Center }) {
Column() {
Button('push PageA', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
AppRouters.push('pageA')
})
}
}
.height("100%")
.width("100%")
}
.hideTitleBar(true)
.navDestination(this.PagesMap)
}
}
// 封装路由工具类,并注册自定义弹窗组件
class AppRouter {
private static instance = new AppRouter();
private pathStack: NavPathStack = new NavPathStack();
public static getInstance(): AppRouter {
return AppRouter.instance;
}
public getPathStack(): NavPathStack {
return this.pathStack;
}
private pushPath(name: string): void {
this.pathStack.pushPath({ name: name })
}
public static push(name: string): void {
AppRouter.instance.pushPath(name);
}
public static openDialog(name: string, params?: RouterParams): void {
AppRouter.instance.pathStack.pushPath({
name: name, param: params, onPop: (data: PopInfo) => {
if (params?.onPop) {
params.onPop!(data);
}
}
});
}
public static pop(): void {
AppRouter.instance.pathStack.pop();
}
}
是的,目前CustomDialog设计规格如此,也可以定义一个变量;在push前将dialog关闭,返回时再监听拉起,这种没有尝试,不知道跟安卓版本的功能是否差别太大
针对HarmonyOS 鸿蒙Next在CustomDialog中跳转的问题,这里提供一些专业解答:
在HarmonyOS鸿蒙Next中,CustomDialog是一个常用的自定义弹窗组件。如果在CustomDialog中需要进行页面跳转,需要注意以下几点:
- 确保跳转逻辑正确:在CustomDialog中的按钮点击事件中,通过调用页面跳转逻辑(如router.pushUrl)来实现页面跳转。
- 管理弹窗状态:在页面跳转后,原页面的CustomDialog可能会因为页面状态的变化而关闭。为了保持CustomDialog的状态,可以在页面生命周期方法中(如onPageShow)重新打开CustomDialog。
- 避免冲突:确保CustomDialog的显示逻辑与页面跳转逻辑不冲突。例如,不要在CustomDialog的显示逻辑中错误地关闭它。
此外,还需要注意CustomDialog的API使用,确保按照官方文档正确创建和显示CustomDialog。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。