##HarmonyOS Next实战##HarmonyOS SDK应用服务##教育##
参考资料:
在Stage模型下,管理应用窗口的典型场景有:
以下介绍获取窗口属性的方式:
第1步:获取Window类
getLastWindow(ctx: BaseContext): Promise<Window>
获取当前应用内最上层的子窗口,若无应用子窗口,则返回应用主窗口,使用Promise异步回调。
第2步:获取当前窗口的属性
getWindowProperties(): WindowProperties
获取当前窗口的属性,返回WindowProperties。
WindowProperties各属性解释
以下以获取窗口属性的宽高为例,实战代码如下:
import { common } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct GetWindowPropertiesPage {
@State windowWidth: number = 0
@State windowHeight: number = 0
aboutToAppear(): void {
try {
let context = getContext(this) as common.UIAbilityContext;
let promise = window.getLastWindow(context);
promise.then((data) => {
//获取窗口对象
let windowClass = data;
try {
//获取窗口属性
let properties = windowClass.getWindowProperties();
let rect = properties.windowRect;
//rect.width: 窗口宽度;rect.height: 窗口高度
this.windowWidth = px2vp(rect.width)
this.windowHeight = px2vp(rect.height)
} catch (exception) {
console.error('Failed to obtain the window properties. Cause: ' + JSON.stringify(exception));
}
console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
});
} catch (exception) {
console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
}
}
build() {
Column({ space: 10 }) {
Text('GetWindowProperties Page')
.fontSize(20)
.fontWeight(FontWeight.Bold)
Text(`windowWidth = ${this.windowWidth}`)
Text(`windowHeight = ${this.windowHeight}`)
}
.height('100%')
.width('100%')
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。