HarmonyOS 鸿蒙Next 使用SubWindow 创建的全局 Loading 框 好像不是调用时立即出现
HarmonyOS 鸿蒙Next 使用SubWindow 创建的全局 Loading 框 好像不是调用时立即出现 使用 SubWindow 创建全局Loading 框,按道理是会拦截点击掉MainWindow的点击事件的。
但是实际测试的时候发现 调用 window.showWindow() ,窗口并没有立即被展现出来。
可以参考
import window from '@ohos.window';
import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
import { entryName } from './MainPage';
export class CommonWindow {
private storage: LocalStorage | null = null;
private subWindow: window.Window | null = null;
private windowStage1: window.WindowStage | null = null
private context: common.UIAbilityContext | null = null;
private init() {
this.context = this as common.UIAbilityContext;
let data: Data = { subWindowStage: null, storage: null };
this.context.eventHub.emit("createWindow", data);
this.windowStage1 = data.subWindowStage;
this.storage = data.storage;
console.log("aboutToAppear end createWindowStage");
this.context.eventHub.on("closeWindow", (data: Data) => {
this.destroySubWindow();
})
}
showWindow() {
this.init();
if (this.subWindow) {
console.log("subWindow is already exist");
return;
}
try {
if (!this.windowStage1) {
console.error("this.windowStage1 is null");
return;
}
this.windowStage1.createSubWindow('mySubWindow', (err: BusinessError, data) => {
const errCode: number = err.code;
if (errCode) {
console.error('Failed to create the subWindow. Cause: ' + JSON.stringify(err));
return;
}
this.subWindow = (data as window.Window);
console.info('Succeeded in creating the subWindow. Data: ' + JSON.stringify(data));
if (!this.subWindow) {
console.info('Failed to load the content. Cause: windowClass is null');
} else {
let names: Array<'status' | 'navigation'> = [];
this.subWindow.setWindowSystemBarEnable(names);
this.subWindow.setWindowTouchable(false); //设置是否可以点击
this.loadContent(entryName);
this.showSubWindow();
}
});
} catch (exception) {
console.error('Failed to create the window. Cause: ' + JSON.stringify(exception));
}
}
private showSubWindow() {
if (this.subWindow) {
this.subWindow.showWindow((err: BusinessError) => {
const errCode: number = err.code;
if (errCode) {
console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in showing the window.');
});
} else {
console.info('showSubWindow subWindow not created.');
}
}
destroySubWindow() {
if (this.subWindow) {
this.subWindow.destroyWindow((err) => {
const errCode: number = err.code;
if (errCode) {
console.error('Failed to destroy the window. Cause:' + JSON.stringify(err));
return;
}
this.subWindow = null
});
} else {
console.info('showSubWindow subWindow not created.');
}
}
private loadContent(path: string) {
if (this.subWindow) {
let that = this;
let para: Record<string, number> = { 'PropA': 66 };
that.storage = new LocalStorage(para);
if (that.storage != null && this.subWindow != null) {
that.storage.setOrCreate("windowObj", this.subWindow)
}
this.subWindow.loadContentByName(path, this.storage, (err: BusinessError) => {
const errCode: number = err.code;
if (errCode) {
return;
}
if (this.subWindow) {
this.subWindow.setWindowBackgroundColor('#cc000e03')
}
});
} else {
console.info('loadContent subWindow not created.');
}
}
}
export interface Data {
subWindowStage: window.WindowStage | null,
storage: LocalStorage | null
}
import { CommonWindow } from '../uitls/CommonWindow';
@Entry
@Component
struct Index {
testSubWindowDialog() {
let window = new CommonWindow()
window.showWindow();
setTimeout(() => {
window.destroySubWindow()
}, 2000)
}
build() {
Row() {
Column() {
Button("子窗口弹窗")
.margin({ top: 20 })
.onClick(() => {
this.testSubWindowDialog();
})
}
.width('100%')
}
.height('100%')
}
}
subWindowStage: window.WindowStage | null = null;
onWindowStageCreate(windowStage: window.WindowStage): void {
...
this.subWindowStage = windowStage;
const that: EntryAbility = this
this.context.eventHub.on("createWindow", (data: Data) => {
if (that.subWindowStage != undefined) {
data.subWindowStage = that.subWindowStage
}
else {
hilog.info(0x0000, 'testTag', '%{public}s', 'that.subWindowStage == undefined');
}
})
...
}
更多关于HarmonyOS 鸿蒙Next 使用SubWindow 创建的全局 Loading 框 好像不是调用时立即出现的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next系统中,使用SubWindow创建的全局Loading框若未能立即显示,可能是由于以下几个原因:
-
异步加载问题:SubWindow的创建和显示可能是在异步线程中处理的,如果主线程(UI线程)在SubWindow完全创建并显示之前继续执行其他操作,可能会导致Loading框看似“延迟”出现。确保SubWindow的创建和显示逻辑被正确同步到UI线程。
-
动画或过渡效果:如果Loading框设置了动画或过渡效果,这些效果可能需要一定的时间来初始化,从而导致Loading框不是立即可见。检查是否有此类效果,并考虑是否需要调整其持续时间。
-
资源加载:如果Loading框包含复杂的UI元素或需要从资源文件中加载图像等,这些资源的加载时间也会影响Loading框的显示速度。优化资源加载过程或简化UI元素可能有助于改善此问题。
-
系统或应用状态:系统资源紧张或应用处于后台状态时,可能会影响UI元素的即时显示。确保应用有足够的系统资源,并检查应用是否处于前台活动状态。
如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html,