HarmonyOS 鸿蒙Next 在EntryAbility.ets中引用其他目录文件中的getMainWindow隐藏状态栏导航栏没有效果,直接使用getMainWindow方法则有效
HarmonyOS 鸿蒙Next 在EntryAbility.ets中引用其他目录文件中的getMainWindow隐藏状态栏导航栏没有效果,直接使用getMainWindow方法则有效 首先我把EntryAbility.ts的后缀改为了.ets (创建新项目默认为EntryAbility.ts), 然后在 EntryAbility.ets 中引用 WindowModel 类中的 setMainWindowImmersive 方法来隐藏状态栏和导航栏,这个没有效果!
然后,我在 EntryAbility.ts 中直接引用了 getMainWindow 方法来隐藏状态栏,导航栏, 好使!!! 代码如下
// EntryAbility.ets文件
onWindowStageCreate(windowStage: window.WindowStage) {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
let windowModel = WindowModel.getInstance();
windowModel.setMainWindowImmersive();
windowStage.loadContent('pages/Index', (err, data) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
});
}
// WindowModel.ets
import window from '@ohos.window';
import { GlobalContext } from './GlobalContext';
export default class WindowModel {
private windowStage?: window.WindowStage;
static getInstance(): WindowModel {
let instance: WindowModel = GlobalContext.getContext().getObject('windowModel') as WindowModel;
if (instance === undefined) {
instance = new WindowModel();
GlobalContext.getContext().setObject('windowModel', instance)
}
return instance;
}
setMainWindowImmersive(){
if (this.windowStage === undefined) {
console.error('windowStage is undefined.');
return;
}
this.windowStage.getMainWindow((err, windowClass: window.Window) => {
if (err.code) {
console.error(`Failed to obtain the main window. Code:${err.code}, message:${err.message}`);
return;
}
windowClass.setWindowLayoutFullScreen(true, (err) => {
if (err.code) {
console.error(`Failed to set full-screen mode. Code:${err.code}, message:${err.message}`);
return;
}
});
windowClass.setWindowSystemBarEnable([], (err) => {
if (err.code) {
console.error(`Failed to set the system bar to be invisible. Code:${err.code}, message:${err.message}`);
return;
}
});
});
}
}
// GlobalContext.ets
export class GlobalContext {
private constructor() {
}
private static instance: GlobalContext;
private _objects = new Map<string, Object>();
public static getContext(): GlobalContext {
if (!GlobalContext.instance) {
GlobalContext.instance = new GlobalContext();
}
return GlobalContext.instance;
}
getObject(value: string): Object | undefined {
return this._objects.get(value);
}
setObject(key: string, objectClass: Object): void {
this._objects.set(key, objectClass);
}
}
更多关于HarmonyOS 鸿蒙Next 在EntryAbility.ets中引用其他目录文件中的getMainWindow隐藏状态栏导航栏没有效果,直接使用getMainWindow方法则有效的实战教程也可以访问 https://www.itying.com/category-93-b0.html
感谢楼主分享,长知识了。
更多关于HarmonyOS 鸿蒙Next 在EntryAbility.ets中引用其他目录文件中的getMainWindow隐藏状态栏导航栏没有效果,直接使用getMainWindow方法则有效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
没事了,真想大白了,突然想起来,我之前还写过一种隐藏状态栏的办法,冲突了
注掉就好了
空项目, 找到个办法, 把windowStage做为参数传进去,但是我写完那个项目这样写没用, 状态栏时而全黑色的,时而无效
在HarmonyOS鸿蒙Next中,getMainWindow
方法用于获取当前窗口对象,并通过该对象可以设置窗口的属性,如隐藏状态栏和导航栏。如果在EntryAbility.ets
中引用其他目录文件中的getMainWindow
方法没有效果,而直接使用getMainWindow
方法则有效,可能是因为以下原因:
-
上下文不一致:
getMainWindow
方法依赖于当前的上下文环境。如果在其他目录文件中调用getMainWindow
,可能上下文环境与EntryAbility.ets
中的不一致,导致无法正确获取窗口对象。 -
作用域问题:在其他目录文件中定义的
getMainWindow
方法可能没有正确传递或绑定到当前的EntryAbility
实例,导致调用时无法正确执行。 -
导入路径问题:在
EntryAbility.ets
中引用其他目录文件时,可能导入路径不正确或文件未正确加载,导致引用的getMainWindow
方法无法正常执行。 -
生命周期问题:
getMainWindow
方法的调用时机可能不正确,尤其是在其他目录文件中调用时,可能窗口尚未初始化或已经销毁,导致方法无效。
直接使用getMainWindow
方法有效,说明该方法本身没有问题,问题可能出在引用方式或调用时机上。