解决方法
在stage模型中要开启全屏一共有两种方法
方法一:
在onWindowStageCreate函数中,通过windowStage.getMainWindow()获取到窗口对象
然后调用
data.setFullScreen(true, (err, data) => {
if (err.code) {
console.error('Failed to enable the full-screen mode. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in enabling the full-screen mode. Data: ' + JSON.stringify(data));
});
完整代码
onWindowStageCreate(windowStage: window.WindowStage) {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
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) ?? '');
});
// 设置全屏
windowStage.getMainWindow().then((data) => {
data.setFullScreen(true, (err, data) => {
if (err.code) {
console.error('Failed to enable the full-screen mode. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in enabling the full-screen mode. Data: ' + JSON.stringify(data));
});
})
}
方法二
在子页面中同样也是获取到window对象,然后调用window.getLastWindow(this.context),即可获取到windowclass,调用windowclass中的setWindowSystemBarEnable([])即可设置全屏
完整代码
// 获取UIAbility上下文
context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext
async setSystemBar() {
let windowClass = await window.getLastWindow(this.context)
//设置导航栏,状态栏不可见
await windowClass.setWindowSystemBarEnable([])
}
aboutToAppear() {
this.setSystemBar()
}
最后附上API10的所有窗口函数
实例名 | 接口名 | 描述 |
---|---|---|
WindowStage | getMainWindow(callback: AsyncCallback<Window>): void | 获取WindowStage 实例下的主窗口。此接口仅可在 Stage 模型下使用。 |
WindowStage | loadContent(path: string, callback: AsyncCallback<void>): void | 为当前WindowStage 的主窗口加载具体页面。此接口仅可在 Stage 模型下使用。 |
WindowStage | createSubWindow(name: string, callback: AsyncCallback<Window>): void | 创建子窗口。 此接口仅可在 Stage 模型下使用。 |
window静态方法 | createWindow(config: Configuration, callback: AsyncCallback<Window>): void | 创建系统窗口。 - config :创建窗口时的参数。 |
Window | setUIContent(path: string, callback: AsyncCallback<void>): void | 为当前窗口加载具体页面。 |
Window | setWindowBackgroundColor(color: string, callback: AsyncCallback<void>): void | 设置窗口的背景色。 |
Window | setWindowBrightness(brightness: number, callback: AsyncCallback<void>): void | 设置屏幕亮度值。 |
Window | setWindowTouchable(isTouchable: boolean, callback: AsyncCallback<void>): void | 设置窗口是否为可触状态。 |
Window | moveWindowTo(x: number, y: number, callback: AsyncCallback<void>): void | 移动当前窗口位置。 |
Window | resize(width: number, height: number, callback: AsyncCallback<void>): void | 改变当前窗口大小。 |
Window | setWindowLayoutFullScreen(isLayoutFullScreen: boolean, callback: AsyncCallback<void>): void | 设置窗口布局是否为全屏布局。 |
Window | setWindowSystemBarEnable(names: Array<‘status’|‘navigation’>): Promise<void> | 设置导航栏、状态栏是否显示。 |
Window | setWindowSystemBarProperties(systemBarProperties: SystemBarProperties, callback: AsyncCallback<void>): void | 设置窗口内导航栏、状态栏属性。systemBarProperties :导航栏、状态栏的属性集合。 |
Window | showWindow(callback: AsyncCallback<void>): void | 显示当前窗口。 |
Window | on(type: ‘touchOutside’, callback: Callback<void>): void | 开启本窗口区域外的点击事件的监听。 |
Window | destroyWindow(callback: AsyncCallback<void>): void | 销毁当前窗口。 |