HarmonyOS 鸿蒙Next createWindow无法显示
HarmonyOS 鸿蒙Next createWindow无法显示
利用window?.createWindow进行创建一个窗口,当做Dialog使用,但是loadContentByName无法显示出页面,详见如下代码:
class SharedDialog {
message(ctx: common.BaseContext, message: string = '', onConfirm: () => void, onCancel?: () => void) {
Logger.error(`onConfirm:${onConfirm} onCancel:${onCancel}\nmessage1:${message}`, TAG)
try {
this.createDialog(ctx, message, onConfirm, onCancel)
} catch (error) {
Logger.error(`error:${JSON.stringify(error)}`, TAG)
}
}
dismiss(onDismiss?: () => void) {
window?.findWindow('dialog_window')?.destroyWindow((error) => {
Logger.info(`dismiss:${JSON.stringify(error)}`, TAG)
if (error && !error.code && onDismiss) onDismiss()
})
}
private createDialog(context: common.BaseContext, message: string = '', onConfirm: () => void, onCancel?: () => void) {
window?.createWindow({
name: 'dialog_window',
windowType: window.WindowType.TYPE_DIALOG,
ctx: context
}, async (_, win) => {
if (win) {
AppStorage.setOrCreate('storage_message', message)
let localStorage = new LocalStorage()
localStorage.setOrCreate('storage_message', message)
if (onConfirm) localStorage.setOrCreate('storage_confirm', onConfirm)
if (onCancel) localStorage.setOrCreate('storage_cancel', onCancel)
await win.loadContentByName('DialogPage', localStorage)
win.setWindowLayoutFullScreen(true)
win.setWindowBackgroundColor('#50000000')
let d = display.getDefaultDisplaySync()
await win.resize(d.width, d.height)
win.showWindow()
}
})
}
}
export default new SharedDialog()
page页面代码:
@Entry({ routeName: 'DialogPage', storage })
@Component
struct DialogPage {
private context = getContext(this) as common.UIAbilityContext;
autoDismiss: boolean = true; //点击按钮是否自动关闭Dialog
mode: Mode = Mode.TXT
title: string = ''; //弹框标题
editHint: string = ''; //输入框的默认占位值
editValue: string = ''; //输入框的值,可以利用@Link获取
@StorageProp('event_message') message: string = ''; //文本弹框内容
cancelTxt?: string = '取消' //取消按钮文字
confirmTxt?: string = '确定' //确定按钮文字
onCancel?: () => void = () => {
}; //取消按钮
@StorageProp('storage_confirm') onConfirm?: () => void = () => {
}; //确定按钮
onShare?: () => void = () => {
}; //微信分享按钮
onInputChange: (value: string) => void = () => {
}; //输入框时的输入内容改变回调
scroller: Scroller = new Scroller()
build() {
Stack({ alignContent: Alignment.Center }) {
Column() {
//标题(不为空才显示)
if (this.title) Text(this.title)
.fontSize(18)
.maxLines(1)
.fontColor(Color.Black)
.align(Alignment.Center)
.textAlign(TextAlign.Center)
.margin({ top: 16, bottom: 6, left: 20, right: 20 })
if (this.mode == Mode.LIST) {
} else if (this.mode == Mode.INPUT) {
TextInput({ placeholder: this.editHint, text: this.editValue })
.width('90%')
.enableAutoFill(false)
.selectAll(true)
.onChange((value: string) => {
this.onInputChange(value)
})
.margin({ top: 20, bottom: 16 })
} else {
Scroll(this.scroller) {
Text(this.message)
.fontSize(17)
.fontColor($r('app.color.text_black'))
.padding({ top: 20, bottom: 16, left: 18, right: 18 })
}.width('100%').constraintSize({ maxHeight: '60%' }).margin({ top: 16, bottom: 16 })
.scrollBar(BarState.Off)
}
//底部按钮(相应按钮根据传入的按钮值决定是否显示;且不为Mode.LOADING时才显示)
Flex({ justifyContent: FlexAlign.SpaceAround }) {
if (this.cancelTxt) {
Button(this.cancelTxt)
.onClick(() => {
if (this.autoDismiss) SharedDialog.dismiss()
if (this.onCancel) this.onCancel?.()
})
.fontSize(17)
.width('30%')
.backgroundColor(0xffffff)
.fontWeight(FontWeight.Bold)
.fontColor($r('app.color.text_hint'))
}
if (this.mode == Mode.SHARE) {
Button('微信分发')
.onClick(() => {
if (this.autoDismiss) SharedDialog.dismiss()
this.onShare?.()
})
.fontSize(17)
.width('30%')
.fontWeight(FontWeight.Bold)
.backgroundColor(0xffffff)
.fontColor($r('app.color.theme_color'))
}
if (this.confirmTxt) {
Button(this.confirmTxt)
.onClick(() => {
if (this.autoDismiss) SharedDialog.dismiss()
if (this.onConfirm) this.onConfirm?.()
})
.fontSize(17)
.width('30%')
.fontWeight(FontWeight.Bold)
.backgroundColor(0xffffff)
.fontColor(this.mode == Mode.ALERT ? $r('app.color.error_color') : $r('app.color.theme_color'))
}
}.margin({ bottom: 10 })
}
.width('86%')
.borderRadius(10)
.justifyContent(FlexAlign.Center)
.backgroundColor(Color.White)
}
.width('100%')
.height('100%')
.backgroundColor('#50000000')
}
}
更多关于HarmonyOS 鸿蒙Next createWindow无法显示的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
1 回复