HarmonyOS 鸿蒙Next CustomDialog通过路由打开页面出现的问题
HarmonyOS 鸿蒙Next CustomDialog通过路由打开页面出现的问题
我在page1中打开一个dialog,然后dialog里有个按钮跳转到page2,但是很奇怪page2不是覆盖上来,而是插入在page1跟dialog之间,dialog在最顶层,请问这种要如何处理?
2 回复
试试这个
//index.ets
import { CustomDialogA, CustomDialogB,webDialog } from '../cases/dialog/CustomDialog'
import { CaseDialog } from '../cases/dialog/CaseDialog'
import { AppRouter } from '../router/AppRouter'
@Entry
@Component
struct Index {
@Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack()
aboutToAppear() {
AppRouter.getInstance().register(this.pageInfos);
}
@Builder
PageMap(name: string) {
if (name === 'dialog') {
CaseDialog()
} else if (name === 'customDialog') {
CustomDialogA()
} else if (name === 'fullDialog') {
CustomDialogB()
} else if(name === 'webDialog'){
webDialog()
}
}
build() {
Navigation(this.pageInfos) {
Column() {
Button('Dialog', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
this.pageInfos.pushPath({ name: 'dialog' })
})
}
}
.hideTitleBar(true)
.navDestination(this.PageMap)
}
}
//AppRouter.ets
import { HashMap } from '@kit.ArkTS';
export interface RouterParams {
name?: string
}
export class AppRouter {
private pageStack?: NavPathStack;
private routeMap: HashMap<string, WrappedBuilder<[object]>> = new HashMap();
public static getInstance(): AppRouter {
return instance;
}
register(pageStack: NavPathStack): void {
this.pageStack = pageStack;
}
public addRouteMap(name: string, builder:
WrappedBuilder<[object]>): void {
this.routeMap.set(name, builder)
}
private pushPath(name: string): void {
this.pageStack!.pushPath({ name: name })
}
public static push(name: string): void {
instance.pushPath(name);
}
public static openDialog(name: string, params?: RouterParams): void {
instance.pageStack?.pushPath({ name: name, param: params });
}
public static replace(name: string, params?: RouterParams): void {
instance.pageStack?.replacePath({ name: name, param: params });
}
public static getParams(name: string): string {
return JSON.stringify(instance.pageStack?.getParamByName(name));
}
public static pop(): void {
instance.pageStack?.pop();
}
}
let instance: AppRouter = new AppRouter();
export interface AnimateCallback {
finish: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
start: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
onFinish: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
timeout: (number | undefined) | undefined;
}
export class CustomTransition {
static delegate = new CustomTransition();
customTransitionMap: Map<string, AnimateCallback> = new Map()
static getAnimateParam(name: string): AnimateCallback {
let result: AnimateCallback = {
start: CustomTransition.delegate.customTransitionMap.get(name)?.start,
finish: CustomTransition.delegate.customTransitionMap.get(name)?.finish,
timeout: CustomTransition.delegate.customTransitionMap.get(name)?.timeout,
onFinish: CustomTransition.delegate.customTransitionMap.get(name)?.onFinish
};
return result;
}
static registerNavParam(name: string, startCallback: (operation: boolean, isExit: boolean) => void,
endCallback: (operation: boolean, isExit: boolean) => void,
onFinish: (opeation: boolean, isExit: boolean) => void, timeout: number): void {
if (CustomTransition.delegate.customTransitionMap.has(name)) {
let param = CustomTransition.delegate.customTransitionMap.get(name);
if (param != undefined) {
param.start = startCallback;
param.finish = endCallback;
param.timeout = timeout;
param.onFinish = onFinish;
return;
}
}
let params: AnimateCallback = { timeout: timeout, start: startCallback, finish: endCallback, onFinish: onFinish };
CustomTransition.delegate.customTransitionMap.set(name, params);
}
}
//CaseDialog.ets
import { AppRouter } from '../../router/AppRouter';
@Entry
@Component
export struct CaseDialog {
@State message: string = "弹窗背景";
build() {
NavDestination() {
Column() {
Text(this.message)
Row()
.height("90%")
.width("100%")
.backgroundColor(Color.Brown)
Button('打开弹窗', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
// 打开组件弹窗
AppRouter.openDialog("customDialog");
// 打开web页面
// AppRouter.openDialog("webDialog");
})
}
}
}
}
//CustomDialog.ets
import { AppRouter, RouterParams } from '../../router/AppRouter';
import web_webview from '@ohos.web.webview';
import router from '@ohos.router';
@Component
export struct CustomDialogA {
@State heightSize: string = "50%";
@State params: string = "";
aboutToAppear() {
this.params = AppRouter.getParams("customDialog");
}
build() {
NavDestination() {
Stack({ alignContent: Alignment.Bottom }) {
Column() {
}
.width("100%")
.height("100%")
.backgroundColor('rgba(0,0,0,0.5)')
.transition(
TransitionEffect.OPACITY.animation({
duration: 300,
curve: Curve.Friction
})
)
.onClick(() => {
AppRouter.pop();
})
Column() {
Text("弹窗")
.fontColor(Color.White)
Text("参数:" + this.params)
.fontSize(18)
.fontColor(Color.White)
Button("新弹窗", { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
router.pushUrl({url:'pages/PageOne'})
// AppRouter.openDialog("fullDialog", { name: "new Open" } as RouterParams)
})
}
.width("100%")
.backgroundColor(Color.Blue)
.height(this.heightSize)
// .align(Alignment.Bottom)
.transition(
TransitionEffect.move(TransitionEdge.BOTTOM)
.animation({
duration: 300,
curve: Curve.Friction
})
)
}
.width("100%")
.height("100%")
}
.mode(NavDestinationMode.DIALOG)
.hideTitleBar(true)
}
}
@Component
export struct CustomDialogB {
@State params: string = "";
aboutToAppear() {
this.params = AppRouter.getParams("fullDialog");
}
build() {
NavDestination() {
Column() {
Text("全屏弹窗")
Text("参数:" + this.params)
.fontSize(18)
Button("返回", { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
AppRouter.pop()
})
}
.backgroundColor(Color.White)
.width("100%")
.height("100%")
// .align(Alignment.Bottom)
.transition(
TransitionEffect.move(TransitionEdge.BOTTOM)
.animation({
duration: 300,
curve: Curve.Friction
})
)
}
.mode(NavDestinationMode.DIALOG)
.hideTitleBar(true)
}
}
@Component
export struct webDialog {
@State params: string = "";
webviewController:web_webview.WebviewController = new web_webview.WebviewController();
aboutToAppear() {
this.params = AppRouter.getParams("webDialog");
}
build() {
NavDestination() {
Column() {
Web({ src: 'www.baidu.com', controller: this.webviewController })
}
}
}
}
更多关于HarmonyOS 鸿蒙Next CustomDialog通过路由打开页面出现的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙系统中,Next CustomDialog通过路由打开页面时可能会遇到一些问题。针对这一问题,常见的几个处理方向包括:
-
路由配置检查:确保目标页面已在路由表中正确注册,并且路径、参数等配置无误。
-
Dialog状态管理:在Dialog触发路由跳转时,注意Dialog的生命周期管理,避免在Dialog销毁后执行跳转操作。
-
上下文传递:确保传递给路由的上下文(Context)是有效的,并且适用于目标页面。若Dialog和目标页面属于不同的Ability,可能需要特别注意上下文传递的方式。
-
异步处理:如果Dialog中的操作是异步的(如网络请求),确保在结果返回后再执行路由跳转,且此时Dialog仍处于可见状态。
-
权限问题:检查目标页面是否需要特定的权限,且这些权限是否已被授予。
-
日志调试:查看系统日志,寻找可能的错误信息或异常堆栈,这有助于快速定位问题。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。客服团队将提供更专业的技术支持。