• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Interface (WindowStage)
2<!--Kit: ArkUI-->
3<!--Subsystem: Window-->
4<!--Owner: @waterwin-->
5<!--Designer: @nyankomiya-->
6<!--Tester: @qinliwen0417-->
7<!--Adviser: @ge-yafang-->
8
9> **说明:**
10>
11> - 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
12>
13> - 本Interface首批接口从API version 9开始支持。
14
15窗口管理器。管理各个基本窗口单元,即[Window](arkts-apis-window-Window.md)实例。
16
17下列API示例中都需在[onWindowStageCreate()](../apis-ability-kit/js-apis-app-ability-uiAbility.md#onwindowstagecreate)函数中使用WindowStage的实例调用对应方法。
18
19## 导入模块
20
21```ts
22import { window } from '@kit.ArkUI';
23```
24
25## getMainWindow<sup>9+</sup>
26
27getMainWindow(callback: AsyncCallback&lt;Window&gt;): void
28
29获取该WindowStage实例下的主窗口,使用callback异步回调。
30
31**模型约束:** 此接口仅可在Stage模型下使用。
32
33**系统能力:** SystemCapability.WindowManager.WindowManager.Core
34
35**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
36
37**参数:**
38
39| 参数名   | 类型                                   | 必填 | 说明                                          |
40| -------- | -------------------------------------- | ---- | --------------------------------------------- |
41| callback | AsyncCallback&lt;[Window](arkts-apis-window-Window.md)&gt; | 是   | 回调函数。返回当前WindowStage下的主窗口对象。 |
42
43**错误码:**
44
45以下错误码的详细介绍请参见[窗口错误码](errorcode-window.md)。
46
47| 错误码ID | 错误信息 |
48| ------- | ------------------------------ |
49| 1300002 | This window state is abnormal. |
50| 1300005 | This window stage is abnormal. |
51
52**示例:**
53
54```ts
55// EntryAbility.ets
56import { UIAbility } from '@kit.AbilityKit';
57import { BusinessError } from '@kit.BasicServicesKit';
58
59export default class EntryAbility extends UIAbility {
60  // ...
61
62  onWindowStageCreate(windowStage: window.WindowStage) {
63    console.info('onWindowStageCreate');
64    let windowClass: window.Window | undefined = undefined;
65    windowStage.getMainWindow((err: BusinessError, data) => {
66      const errCode: number = err.code;
67      if (errCode) {
68        console.error(`Failed to obtain the main window. Cause code: ${errCode}, message: ${err.message}`);
69        return;
70      }
71      windowClass = data;
72      console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
73    });
74  }
75};
76```
77
78## getMainWindow<sup>9+</sup>
79
80getMainWindow(): Promise&lt;Window&gt;
81
82获取该WindowStage实例下的主窗口,使用Promise异步回调。
83
84**模型约束:** 此接口仅可在Stage模型下使用。
85
86**系统能力:** SystemCapability.WindowManager.WindowManager.Core
87
88**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
89
90**返回值:**
91
92| 类型                             | 说明                                             |
93| -------------------------------- | ------------------------------------------------ |
94| Promise&lt;[Window](arkts-apis-window-Window.md)&gt; | Promise对象。返回当前WindowStage下的主窗口对象。 |
95
96**错误码:**
97
98以下错误码的详细介绍请参见[窗口错误码](errorcode-window.md)。
99
100| 错误码ID | 错误信息 |
101| ------- | ------------------------------ |
102| 1300002 | This window state is abnormal. |
103| 1300005 | This window stage is abnormal. |
104
105**示例:**
106
107```ts
108// EntryAbility.ets
109import { UIAbility } from '@kit.AbilityKit';
110import { BusinessError } from '@kit.BasicServicesKit';
111
112export default class EntryAbility extends UIAbility {
113  // ...
114
115  onWindowStageCreate(windowStage: window.WindowStage) {
116    console.info('onWindowStageCreate');
117    let windowClass: window.Window | undefined = undefined;
118    let promise = windowStage.getMainWindow();
119    promise.then((data) => {
120      windowClass = data;
121      console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
122    }).catch((err: BusinessError) => {
123      console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
124    });
125  }
126};
127```
128
129## getMainWindowSync<sup>9+</sup>
130
131getMainWindowSync(): Window
132
133获取该WindowStage实例下的主窗口。
134
135**模型约束:** 此接口仅可在Stage模型下使用。
136
137**系统能力:** SystemCapability.WindowManager.WindowManager.Core
138
139**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
140
141**返回值:**
142
143| 类型 | 说明 |
144| ----------------- | --------------------------------- |
145| [Window](arkts-apis-window-Window.md) | 返回当前WindowStage下的主窗口对象。 |
146
147**错误码:**
148
149以下错误码的详细介绍请参见[窗口错误码](errorcode-window.md)。
150
151| 错误码ID | 错误信息 |
152| ------- | ------------------------------ |
153| 1300002 | This window state is abnormal. |
154| 1300005 | This window stage is abnormal. |
155
156**示例:**
157<!--code_no_check-->
158```ts
159// EntryAbility.ets
160import { UIAbility } from '@kit.AbilityKit';
161
162export default class EntryAbility extends UIAbility {
163  // ...
164
165  onWindowStageCreate(windowStage: window.WindowStage) {
166    console.info('onWindowStageCreate');
167    try {
168      let windowClass = windowStage.getMainWindowSync();
169    } catch (exception) {
170      console.error(`Failed to obtain the main window. Cause code: ${exception.code}, message: ${exception.message}`);
171    }
172  }
173};
174```
175
176## createSubWindow<sup>9+</sup>
177
178createSubWindow(name: string, callback: AsyncCallback&lt;Window&gt;): void
179
180创建该WindowStage实例下的子窗口,使用callback异步回调。
181
182子窗口创建后默认是[沉浸式布局](../../windowmanager/window-terminology.md#沉浸式布局)。
183
184**模型约束:** 此接口仅可在Stage模型下使用。
185
186**系统能力:** SystemCapability.WindowManager.WindowManager.Core
187
188**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
189
190**参数:**
191
192| 参数名   | 类型                                   | 必填 | 说明                                          |
193| -------- | -------------------------------------- | ---- | --------------------------------------------- |
194| name     | string                                 | 是   | 子窗口的名字。                                |
195| callback | AsyncCallback&lt;[Window](arkts-apis-window-Window.md)&gt; | 是   | 回调函数。返回当前WindowStage下的子窗口对象。 |
196
197**错误码:**
198
199以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
200
201| 错误码ID | 错误信息 |
202| ------- | ------------------------------ |
203| 401     | Parameter error. Possible cause: Incorrect parameter types. |
204| 1300002 | This window state is abnormal. |
205
206**示例:**
207
208```ts
209// EntryAbility.ets
210import { UIAbility } from '@kit.AbilityKit';
211import { BusinessError } from '@kit.BasicServicesKit';
212
213export default class EntryAbility extends UIAbility {
214  // ...
215
216  onWindowStageCreate(windowStage: window.WindowStage) {
217    console.info('onWindowStageCreate');
218    let windowClass: window.Window | undefined = undefined;
219    try {
220      windowStage.createSubWindow('mySubWindow', (err: BusinessError, data) => {
221        const errCode: number = err.code;
222        if (errCode) {
223          console.error(`Failed to create the subwindow. Cause code: ${err.code}, message: ${err.message}`);
224          return;
225        }
226        windowClass = data;
227        console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
228        if (!windowClass) {
229          console.info('Failed to load the content. Cause: windowClass is null');
230        }
231        else {
232          windowClass.resize(500, 1000);
233        }
234      });
235    } catch (exception) {
236      console.error(`Failed to create the subwindow. Cause code: ${exception.code}, message: ${exception.message}`);
237    }
238  }
239};
240```
241
242## createSubWindow<sup>9+</sup>
243
244createSubWindow(name: string): Promise&lt;Window&gt;
245
246创建该WindowStage实例下的子窗口,使用Promise异步回调。
247
248子窗口创建后默认是[沉浸式布局](../../windowmanager/window-terminology.md#沉浸式布局)。
249
250**模型约束:** 此接口仅可在Stage模型下使用。
251
252**系统能力:** SystemCapability.WindowManager.WindowManager.Core
253
254**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
255
256**参数:**
257
258| 参数名 | 类型   | 必填 | 说明           |
259| ------ | ------ | ---- | -------------- |
260| name   | string | 是   | 子窗口的名字。 |
261
262**返回值:**
263
264| 类型                             | 说明                                             |
265| -------------------------------- | ------------------------------------------------ |
266| Promise&lt;[Window](arkts-apis-window-Window.md)&gt; | Promise对象。返回当前WindowStage下的子窗口对象。 |
267
268**错误码:**
269
270以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
271
272| 错误码ID | 错误信息 |
273| ------- | ------------------------------ |
274| 401     | Parameter error. Possible cause: Incorrect parameter types. |
275| 1300002 | This window state is abnormal. |
276
277**示例:**
278
279```ts
280// EntryAbility.ets
281import { UIAbility } from '@kit.AbilityKit';
282import { BusinessError } from '@kit.BasicServicesKit';
283
284export default class EntryAbility extends UIAbility {
285  // ...
286
287  onWindowStageCreate(windowStage: window.WindowStage) {
288    console.info('onWindowStageCreate');
289    let windowClass: window.Window | undefined = undefined;
290    try {
291      let promise = windowStage.createSubWindow('mySubWindow');
292      promise.then((data) => {
293        windowClass = data;
294        console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
295      }).catch((err: BusinessError) => {
296        console.error(`Failed to create the subwindow. Cause code: ${err.code}, message: ${err.message}`);
297      });
298    } catch (exception) {
299      console.error(`Failed to create the subwindow. Cause code: ${exception.code}, message: ${exception.message}`);
300    }
301  }
302};
303```
304
305## createSubWindowWithOptions<sup>11+</sup>
306
307createSubWindowWithOptions(name: string, options: SubWindowOptions): Promise&lt;Window&gt;
308
309创建该WindowStage实例下的子窗口,使用Promise异步回调。
310
311非[自由窗口](../../windowmanager/window-terminology.md#自由窗口)状态下,子窗口创建后默认是[沉浸式布局](../../windowmanager/window-terminology.md#沉浸式布局)。
312
313自由窗口状态下,子窗口参数[decorEnabled](arkts-apis-window-i.md#subwindowoptions11)为false时,子窗口创建后为沉浸式布局;子窗口参数decorEnabled为true,子窗口创建后为非沉浸式布局。
314
315**模型约束:** 此接口仅可在Stage模型下使用。
316
317**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
318
319**系统能力:** SystemCapability.Window.SessionManager
320
321**参数:**
322
323| 参数名 | 类型   | 必填 | 说明           |
324| ------ | ------ | ---- | -------------- |
325| name   | string | 是   | 子窗口的名字。 |
326| options  | [SubWindowOptions](arkts-apis-window-i.md#subwindowoptions11) | 是   | 子窗口参数。  |
327
328**返回值:**
329
330| 类型                             | 说明                                             |
331| -------------------------------- | ------------------------------------------------ |
332| Promise&lt;[Window](arkts-apis-window-Window.md)&gt; | Promise对象。返回当前WindowStage下创建的子窗口对象。 |
333
334**错误码:**
335
336以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
337
338| 错误码ID | 错误信息 |
339| ------- | ------------------------------ |
340| 401     | Parameter error. Possible cause: Incorrect parameter types. |
341| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
342| 1300002 | This window state is abnormal. |
343| 1300005 | This window stage is abnormal. |
344
345**示例:**
346
347```ts
348// EntryAbility.ets
349import { UIAbility } from '@kit.AbilityKit';
350import { BusinessError } from '@kit.BasicServicesKit';
351
352export default class EntryAbility extends UIAbility {
353  // ...
354
355  onWindowStageCreate(windowStage: window.WindowStage) {
356    console.info('onWindowStageCreate');
357    let windowClass: window.Window | undefined = undefined;
358    try {
359      let options : window.SubWindowOptions = {
360        title: 'title',
361        decorEnabled: true
362      };
363      let promise = windowStage.createSubWindowWithOptions('mySubWindow', options);
364      promise.then((data) => {
365        windowClass = data;
366        console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
367      }).catch((err: BusinessError) => {
368        console.error(`Failed to create the subwindow. Cause code: ${err.code}, message: ${err.message}`);
369      });
370    } catch (exception) {
371      console.error(`Failed to create the subwindow. Cause code: ${exception.code}, message: ${exception.message}`);
372    }
373  }
374};
375```
376
377## getSubWindow<sup>9+</sup>
378
379getSubWindow(callback: AsyncCallback&lt;Array&lt;Window&gt;&gt;): void
380
381获取该WindowStage实例下的所有子窗口,使用callback异步回调。
382
383**模型约束:** 此接口仅可在Stage模型下使用。
384
385**系统能力:** SystemCapability.WindowManager.WindowManager.Core
386
387**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
388
389**参数:**
390
391| 参数名   | 类型                                                | 必填 | 说明                                              |
392| -------- | --------------------------------------------------- | ---- | ------------------------------------------------- |
393| callback | AsyncCallback&lt;Array&lt;[Window](arkts-apis-window-Window.md)&gt;&gt; | 是   | 回调函数。返回当前WindowStage下的所有子窗口对象。 |
394
395**错误码:**
396
397以下错误码的详细介绍请参见[窗口错误码](errorcode-window.md)。
398
399| 错误码ID | 错误信息 |
400| ------- | ------------------------------ |
401| 1300002 | This window state is abnormal. |
402
403**示例:**
404<!--code_no_check-->
405```ts
406// EntryAbility.ets
407import { UIAbility } from '@kit.AbilityKit';
408import { BusinessError } from '@kit.BasicServicesKit';
409
410export default class EntryAbility extends UIAbility {
411  // ...
412
413  onWindowStageCreate(windowStage: window.WindowStage) {
414    console.info('onWindowStageCreate');
415    let windowClass: window.Window[] = [];
416    windowStage.getSubWindow((err: BusinessError, data) => {
417      const errCode: number = err.code;
418      if (errCode) {
419        console.error(`Failed to obtain the subwindow. Cause code: ${err.code}, message: ${err.message}`);
420        return;
421      }
422      windowClass = data;
423      console.info('Succeeded in obtaining the subwindow. Data: ' + JSON.stringify(data));
424    });
425  }
426};
427```
428
429## getSubWindow<sup>9+</sup>
430
431getSubWindow(): Promise&lt;Array&lt;Window&gt;&gt;
432
433获取该WindowStage实例下的所有子窗口,使用Promise异步回调。
434
435**模型约束:** 此接口仅可在Stage模型下使用。
436
437**系统能力:** SystemCapability.WindowManager.WindowManager.Core
438
439**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
440
441**返回值:**
442
443| 类型                                          | 说明                                                 |
444| --------------------------------------------- | ---------------------------------------------------- |
445| Promise&lt;Array&lt;[Window](arkts-apis-window-Window.md)&gt;&gt; | Promise对象。返回当前WindowStage下的所有子窗口对象。 |
446
447**错误码:**
448
449以下错误码的详细介绍请参见[窗口错误码](errorcode-window.md)。
450
451| 错误码ID | 错误信息 |
452| ------- | ------------------------------ |
453| 1300002 | This window state is abnormal. |
454
455**示例:**
456<!--code_no_check-->
457```ts
458// EntryAbility.ets
459import { UIAbility } from '@kit.AbilityKit';
460import { BusinessError } from '@kit.BasicServicesKit';
461
462export default class EntryAbility extends UIAbility {
463  // ...
464
465  onWindowStageCreate(windowStage: window.WindowStage) {
466    console.info('onWindowStageCreate');
467    let windowClass: window.Window[] = [];
468    let promise = windowStage.getSubWindow();
469    promise.then((data) => {
470      windowClass = data;
471      console.info('Succeeded in obtaining the subwindow. Data: ' + JSON.stringify(data));
472    }).catch((err: BusinessError) => {
473      console.error(`Failed to obtain the subwindow. Cause code: ${err.code}, message: ${err.message}`);
474    });
475  }
476};
477```
478
479## loadContent<sup>9+</sup>
480
481loadContent(path: string, storage: LocalStorage, callback: AsyncCallback&lt;void&gt;): void
482
483根据当前工程中指定的页面路径为WindowStage的主窗口加载具体页面内容,通过LocalStorage传递状态属性给加载的页面,使用callback异步回调。建议在UIAbility启动过程中调用该接口,重复调用将首先销毁旧的页面内容(即UIContent)再加载新页面内容,请谨慎使用。当前UI的执行上下文可能不明确,所以不建议在回调函数中做UI相关的操作。
484
485**模型约束:** 此接口仅可在Stage模型下使用。
486
487**系统能力:** SystemCapability.WindowManager.WindowManager.Core
488
489**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
490
491**参数:**
492
493| 参数名   | 类型                                            | 必填 | 说明                                                         |
494| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ |
495| path     | string                                          | 是   | 要加载到窗口中的页面内容的路径,该路径需添加到工程的main_pages.json文件中。  |
496| storage  | [LocalStorage](../../ui/state-management/arkts-localstorage.md) | 是   | 页面级UI状态存储单元,这里用于为加载到窗口的页面内容传递状态属性。 |
497| callback | AsyncCallback&lt;void&gt;                       | 是   | 回调函数。                                                   |
498
499**错误码:**
500
501以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
502
503| 错误码ID | 错误信息 |
504| ------- | ------------------------------ |
505| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Invalid path parameter.|
506| 1300002 | This window state is abnormal. |
507
508**示例:**
509
510```ts
511// EntryAbility.ets
512import { UIAbility } from '@kit.AbilityKit';
513import { BusinessError } from '@kit.BasicServicesKit';
514
515export default class EntryAbility extends UIAbility {
516  // ...
517
518  storage: LocalStorage = new LocalStorage();
519
520  onWindowStageCreate(windowStage: window.WindowStage) {
521    this.storage.setOrCreate('storageSimpleProp', 121);
522    console.info('onWindowStageCreate');
523    try {
524      windowStage.loadContent('pages/page2', this.storage, (err: BusinessError) => {
525        const errCode: number = err.code;
526        if (errCode) {
527          console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
528          return;
529        }
530        console.info('Succeeded in loading the content.');
531      });
532    } catch (exception) {
533      console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`);
534    }
535  }
536};
537```
538
539## loadContent<sup>9+</sup>
540
541loadContent(path: string, storage?: LocalStorage): Promise&lt;void&gt;
542
543根据当前工程中指定的页面路径为WindowStage的主窗口加载具体页面内容,通过LocalStorage传递状态属性给加载的页面,使用Promise异步回调。建议在UIAbility启动过程中调用该接口,重复调用将首先销毁旧的页面内容(即UIContent)再加载新页面内容,请谨慎使用。当前UI的执行上下文可能不明确,所以不建议在回调函数中做UI相关的操作。
544
545**模型约束:** 此接口仅可在Stage模型下使用。
546
547**系统能力:** SystemCapability.WindowManager.WindowManager.Core
548
549**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
550
551**参数:**
552
553| 参数名  | 类型                                            | 必填 | 说明                                                         |
554| ------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ |
555| path    | string                                          | 是   | 要加载到窗口中的页面内容的路径,该路径需添加到工程的main_pages.json文件中。 |
556| storage | [LocalStorage](../../ui/state-management/arkts-localstorage.md) | 否   | 页面级UI状态存储单元,这里用于为加载到窗口的页面内容传递状态属性。 |
557
558**返回值:**
559
560| 类型                | 说明                      |
561| ------------------- | ------------------------- |
562| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
563
564**错误码:**
565
566以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
567
568| 错误码ID | 错误信息 |
569| ------- | ------------------------------ |
570| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Invalid path parameter.|
571| 1300002 | This window state is abnormal. |
572
573**示例:**
574
575```ts
576// EntryAbility.ets
577import { UIAbility } from '@kit.AbilityKit';
578import { BusinessError } from '@kit.BasicServicesKit';
579
580export default class EntryAbility extends UIAbility {
581  // ...
582
583  storage: LocalStorage = new LocalStorage();
584
585  onWindowStageCreate(windowStage: window.WindowStage) {
586    this.storage.setOrCreate('storageSimpleProp', 121);
587    console.info('onWindowStageCreate');
588    try {
589      let promise = windowStage.loadContent('pages/page2', this.storage);
590      promise.then(() => {
591        console.info('Succeeded in loading the content.');
592      }).catch((err: BusinessError) => {
593        console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
594      });
595    } catch (exception) {
596      console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`);
597    }
598    ;
599  }
600};
601```
602
603## loadContent<sup>9+</sup>
604
605loadContent(path: string, callback: AsyncCallback&lt;void&gt;): void
606
607根据当前工程中指定的页面路径为WindowStage的主窗口加载具体页面内容,使用callback异步回调。建议在UIAbility启动过程中调用该接口,重复调用将首先销毁旧的页面内容(即UIContent)再加载新页面内容,请谨慎使用。当前UI的执行上下文可能不明确,所以不建议在回调函数中做UI相关的操作。
608
609**模型约束:** 此接口仅可在Stage模型下使用。
610
611**系统能力:** SystemCapability.WindowManager.WindowManager.Core
612
613**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
614
615**参数:**
616
617| 参数名   | 类型                      | 必填 | 说明                 |
618| -------- | ------------------------- | ---- | -------------------- |
619| path     | string                    | 是   | 要加载到窗口中的页面内容的路径,该路径需添加到工程的main_pages.json文件中。 |
620| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。           |
621
622**错误码:**
623
624以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
625
626| 错误码ID | 错误信息 |
627| ------- | ------------------------------ |
628| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Invalid path parameter.|
629| 1300002 | This window state is abnormal. |
630
631**示例:**
632
633```ts
634// EntryAbility.ets
635import { UIAbility } from '@kit.AbilityKit';
636import { BusinessError } from '@kit.BasicServicesKit';
637
638export default class EntryAbility extends UIAbility {
639  // ...
640
641  onWindowStageCreate(windowStage: window.WindowStage) {
642    console.info('onWindowStageCreate');
643    try {
644      windowStage.loadContent('pages/page2', (err: BusinessError) => {
645        const errCode: number = err.code;
646        if (errCode) {
647          console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
648          return;
649        }
650        console.info('Succeeded in loading the content.');
651      });
652    } catch (exception) {
653      console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`);
654    }
655  }
656};
657```
658
659## loadContentByName<sup>11+</sup>
660
661loadContentByName(name: string, storage: LocalStorage, callback: AsyncCallback&lt;void&gt;): void
662
663根据指定路由页面名称为当前WindowStage加载[命名路由](../../ui/arkts-routing.md#命名路由)页面,通过LocalStorage传递状态属性至加载页面,使用callback异步回调。建议在UIAbility启动过程中使用该接口,重复调用该接口将先销毁旧的页面内容(即UIContent)再加载新的页面内容,请谨慎使用。当前UI的执行上下文可能不明确,所以不建议在回调函数中做UI相关的操作。
664
665**模型约束:** 此接口仅可在Stage模型下使用。
666
667**系统能力:** SystemCapability.WindowManager.WindowManager.Core
668
669**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
670
671**参数:**
672
673| 参数名   | 类型                                                    | 必填 | 说明                                                         |
674| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
675| name     | string                                                  | 是   | 命名路由页面的名称。                                             |
676| storage  | [LocalStorage](../../ui/state-management/arkts-localstorage.md) | 是   | 页面级UI状态存储单元,这里用于为加载到窗口的页面内容传递状态属性。 |
677| callback | AsyncCallback&lt;void&gt;                               | 是   | 回调函数。                                                   |
678
679**错误码:**
680
681以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
682
683| 错误码ID | 错误信息                                      |
684| -------- | --------------------------------------------- |
685| 401      | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
686| 1300002  | This window state is abnormal.                |
687
688**示例:**
689
690<!--code_no_check-->
691```ts
692// EntryAbility.ets
693import { UIAbility } from '@kit.AbilityKit';
694import { BusinessError } from '@kit.BasicServicesKit';
695import * as Index from '../pages/Index'; // 导入命名路由页面
696
697export default class EntryAbility extends UIAbility {
698  // ...
699
700  storage: LocalStorage = new LocalStorage();
701
702  onWindowStageCreate(windowStage: window.WindowStage) {
703    console.info('onWindowStageCreate');
704    this.storage.setOrCreate('storageSimpleProp', 121);
705    try {
706      windowStage.loadContentByName(Index.entryName, this.storage, (err: BusinessError) => {
707        const errCode: number = err.code;
708        if (errCode) {
709          console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
710          return;
711        }
712        console.info('Succeeded in loading the content.');
713      });
714    } catch (exception) {
715      console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`);
716    }
717  }
718};
719```
720<!--code_no_check-->
721```ts
722// ets/pages/Index.ets
723export const entryName : string = 'Index';
724@Entry({routeName: entryName, useSharedStorage: true})
725@Component
726export struct Index {
727  @State message: string = 'Hello World'
728  @LocalStorageLink('storageSimpleProp') storageSimpleProp: number = 1;
729  build() {
730    Row() {
731      Column() {
732        Text(this.message)
733          .fontSize(50)
734          .fontWeight(FontWeight.Bold)
735      }
736      .width('100%')
737    }
738    .height('100%')
739  }
740}
741```
742
743## loadContentByName<sup>11+</sup>
744
745loadContentByName(name: string, callback: AsyncCallback&lt;void&gt;): void
746
747根据指定路由页面名称为当前WindowStage加载[命名路由](../../ui/arkts-routing.md#命名路由)页面,使用callback异步回调。建议在UIAbility启动过程中使用该接口,重复调用该接口将先销毁旧的页面内容(即UIContent)再加载新的页面内容,请谨慎使用。当前UI的执行上下文可能不明确,所以不建议在回调函数中做UI相关的操作。
748
749**模型约束:** 此接口仅可在Stage模型下使用。
750
751**系统能力:** SystemCapability.WindowManager.WindowManager.Core
752
753**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
754
755**参数:**
756
757| 参数名   | 类型                      | 必填 | 说明             |
758| -------- | ------------------------- | ---- | ---------------- |
759| name     | string                    | 是   | 命名路由页面的名称。 |
760| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。       |
761
762**错误码:**
763
764以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
765
766| 错误码ID | 错误信息                                      |
767| -------- | --------------------------------------------- |
768| 401      | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
769| 1300002  | This window state is abnormal.                |
770
771**示例:**
772
773<!--code_no_check-->
774```ts
775// EntryAbility.ets
776import { UIAbility } from '@kit.AbilityKit';
777import { BusinessError } from '@kit.BasicServicesKit';
778import * as Index from '../pages/Index'; // 导入命名路由页面
779
780export default class EntryAbility extends UIAbility {
781  // ...
782
783  onWindowStageCreate(windowStage: window.WindowStage) {
784    console.info('onWindowStageCreate');
785    try {
786      windowStage.loadContentByName(Index.entryName, (err: BusinessError) => {
787        const errCode: number = err.code;
788        if (errCode) {
789          console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
790          return;
791        }
792        console.info('Succeeded in loading the content.');
793      });
794    } catch (exception) {
795      console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`);
796    }
797  }
798};
799```
800<!--code_no_check-->
801```ts
802// ets/pages/Index.ets
803export const entryName : string = 'Index';
804@Entry({routeName: entryName})
805@Component
806export struct Index {
807  @State message: string = 'Hello World'
808  build() {
809    Row() {
810      Column() {
811        Text(this.message)
812          .fontSize(50)
813          .fontWeight(FontWeight.Bold)
814      }
815      .width('100%')
816    }
817    .height('100%')
818  }
819}
820```
821
822## loadContentByName<sup>11+</sup>
823
824loadContentByName(name: string, storage?: LocalStorage): Promise&lt;void&gt;
825
826根据指定路由页面名称为当前WindowStage加载[命名路由](../../ui/arkts-routing.md#命名路由)页面,通过LocalStorage传递状态属性至加载页面,使用promise异步回调。建议在UIAbility启动过程中使用该接口,重复调用该接口将先销毁旧的页面内容(即UIContent)再加载新的页面内容,请谨慎使用。当前UI的执行上下文可能不明确,所以不建议在回调函数中做UI相关的操作。
827
828**模型约束:** 此接口仅可在Stage模型下使用。
829
830**系统能力:** SystemCapability.WindowManager.WindowManager.Core
831
832**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
833
834**参数:**
835
836| 参数名  | 类型         | 必填 | 说明                                                         |
837| ------- | ------------ | ---- | ------------------------------------------------------------ |
838| name    | string       | 是   | 命名路由页面的名称。                                             |
839| storage | [LocalStorage](../../ui/state-management/arkts-localstorage.md) | 否   | 页面级UI状态存储单元,这里用于为加载到窗口的页面内容传递状态属性。 |
840
841**返回值:**
842
843| 类型                | 说明                      |
844| ------------------- | ------------------------- |
845| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
846
847**错误码:**
848
849以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
850
851| 错误码ID | 错误信息                                      |
852| -------- | --------------------------------------------- |
853| 401      | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
854| 1300002  | This window state is abnormal.                |
855
856**示例:**
857
858<!--code_no_check-->
859```ts
860// EntryAbility.ets
861import { UIAbility } from '@kit.AbilityKit';
862import { BusinessError } from '@kit.BasicServicesKit';
863import * as Index from '../pages/Index'; // 导入命名路由页面
864
865export default class EntryAbility extends UIAbility {
866  // ...
867
868  storage: LocalStorage = new LocalStorage();
869
870  onWindowStageCreate(windowStage: window.WindowStage) {
871    console.info('onWindowStageCreate');
872    this.storage.setOrCreate('storageSimpleProp', 121);
873    try {
874      let promise = windowStage.loadContentByName(Index.entryName, this.storage);
875      promise.then(() => {
876        console.info('Succeeded in loading the content.');
877      }).catch((err: BusinessError) => {
878        console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
879      });
880    } catch (exception) {
881      console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`);
882    }
883  }
884};
885```
886<!--code_no_check-->
887```ts
888// ets/pages/Index.ets
889export const entryName : string = 'Index';
890@Entry({routeName: entryName, useSharedStorage: true})
891@Component
892export struct Index {
893  @State message: string = 'Hello World'
894  @LocalStorageLink('storageSimpleProp') storageSimpleProp: number = 1;
895  build() {
896    Row() {
897      Column() {
898        Text(this.message)
899          .fontSize(50)
900          .fontWeight(FontWeight.Bold)
901      }
902      .width('100%')
903    }
904    .height('100%')
905  }
906}
907```
908
909## on('windowStageEvent')<sup>9+</sup>
910
911on(eventType: 'windowStageEvent', callback: Callback&lt;WindowStageEventType&gt;): void
912
913开启WindowStage生命周期变化的监听。
914
915**模型约束:** 此接口仅可在Stage模型下使用。
916
917**系统能力:** SystemCapability.WindowManager.WindowManager.Core
918
919**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
920
921**参数:**
922
923| 参数名   | 类型                                                         | 必填 | 说明                                                         |
924| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
925| eventType  | string                                                       | 是   | 监听事件,固定为'windowStageEvent',即WindowStage生命周期变化事件。 |
926| callback | Callback&lt;[WindowStageEventType](arkts-apis-window-e.md#windowstageeventtype9)&gt; | 是   | 回调函数。返回当前的WindowStage生命周期状态。                |
927
928**错误码:**
929
930以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
931
932| 错误码ID | 错误信息 |
933| ------- | ------------------------------ |
934| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
935| 1300002 | This window state is abnormal. |
936| 1300005 | This window stage is abnormal. |
937
938**示例:**
939
940```ts
941// EntryAbility.ets
942import { UIAbility } from '@kit.AbilityKit';
943
944export default class EntryAbility extends UIAbility {
945  // ...
946
947  onWindowStageCreate(windowStage: window.WindowStage) {
948    console.info('onWindowStageCreate');
949    try {
950      windowStage.on('windowStageEvent', (data) => {
951        console.info('Succeeded in enabling the listener for window stage event changes. Data: ' +
952        JSON.stringify(data));
953      });
954    } catch (exception) {
955      console.error(`Failed to enable the listener for window stage event changes. Cause code: ${exception.code}, message: ${exception.message}`);
956    }
957  }
958};
959```
960
961## off('windowStageEvent')<sup>9+</sup>
962
963off(eventType: 'windowStageEvent', callback?: Callback&lt;WindowStageEventType&gt;): void
964
965关闭WindowStage生命周期变化的监听。
966
967**模型约束:** 此接口仅可在Stage模型下使用。
968
969**系统能力:** SystemCapability.WindowManager.WindowManager.Core
970
971**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
972
973**参数:**
974
975| 参数名   | 类型                                                         | 必填 | 说明                                                         |
976| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
977| eventType  | string                                                       | 是   | 监听事件,固定为'windowStageEvent',即WindowStage生命周期变化事件。 |
978| callback | Callback&lt;[WindowStageEventType](arkts-apis-window-e.md#windowstageeventtype9)&gt; | 否   | 回调函数。返回当前的WindowStage生命周期状态。若传入参数,则关闭该监听。若未传入参数,则关闭所有WindowStage生命周期变化的监听。                |
979
980**错误码:**
981
982以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
983
984| 错误码ID | 错误信息 |
985| ------- | ------------------------------ |
986| 401     | Parameter error. Possible cause: 1. Incorrect parameter types; 2. Parameter verification failed. |
987| 1300002 | This window state is abnormal. |
988| 1300005 | This window stage is abnormal. |
989
990**示例:**
991
992```ts
993// EntryAbility.ets
994import { UIAbility } from '@kit.AbilityKit';
995
996export default class EntryAbility extends UIAbility {
997  // ...
998
999  onWindowStageCreate(windowStage: window.WindowStage) {
1000    console.info('onWindowStageCreate');
1001    const callback = (windowStageEventType: window.WindowStageEventType) => {
1002      // ...
1003    }
1004    try {
1005      windowStage.on('windowStageEvent', callback);
1006    } catch (exception) {
1007      console.error(`Failed to enable the listener for window stage event changes. Cause code: ${exception.code}, message: ${exception.message}`);
1008    }
1009    try {
1010      windowStage.off('windowStageEvent', callback);
1011      // 如果通过on开启多个callback进行监听,同时关闭所有监听:
1012      windowStage.off('windowStageEvent');
1013    } catch (exception) {
1014      console.error(`Failed to disable the listener for window stage event changes. Cause code: ${exception.code}, message: ${exception.message}`);
1015    }
1016  }
1017};
1018```
1019
1020## on('windowStageLifecycleEvent')<sup>20+</sup>
1021
1022on(eventType: 'windowStageLifecycleEvent', callback: Callback&lt;WindowStageLifecycleEventType&gt;): void
1023
1024开启WindowStage生命周期变化的监听。
1025
1026> **说明:**
1027>
1028>  [on('windowStageEvent')](#onwindowstageevent9)与本接口的区别:
1029>
1030> 1.前者无法保证状态切换间的顺序,对于状态间的顺序有要求的情况下不推荐使用,推荐使用本接口;
1031>
1032> 2.当前接口不提供WindowStage的获焦失焦状态监听,对于windowStage获焦失焦状态有监听需求的情况下,推荐使用[on('windowEvent')](arkts-apis-window-Window.md#onwindowevent10);
1033>
1034> 3.其他系统机制及其生命周期状态切换的详细说明,请参考[主窗口的生命周期](../../windowmanager/window-overview.md#主窗口的生命周期)。
1035
1036**模型约束:** 此接口仅可在Stage模型下使用。
1037
1038**系统能力:** SystemCapability.Window.SessionManager
1039
1040**参数:**
1041
1042| 参数名   | 类型                                                         | 必填 | 说明                                                         |
1043| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1044| eventType  | string                                                       | 是   | 监听事件,固定为'windowStageLifecycleEvent',即WindowStage生命周期变化事件。 |
1045| callback | Callback&lt;[WindowStageLifecycleEventType](arkts-apis-window-e.md#windowstagelifecycleeventtype20)&gt; | 是   | 回调函数。返回当前的WindowStage生命周期状态。                |
1046
1047**错误码:**
1048
1049以下错误码的详细介绍请参见[窗口错误码](errorcode-window.md)。
1050
1051| 错误码ID | 错误信息 |
1052| ------- | ------------------------------ |
1053| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
1054| 1300002 | This window state is abnormal. |
1055| 1300005 | This window stage is abnormal. |
1056
1057**示例:**
1058
1059```ts
1060// EntryAbility.ets
1061import { UIAbility } from '@kit.AbilityKit';
1062
1063export default class EntryAbility extends UIAbility {
1064  // ...
1065
1066  onWindowStageCreate(windowStage: window.WindowStage) {
1067    console.info('onWindowStageCreate');
1068    const callback = (data: window.WindowStageLifecycleEventType) => {
1069      console.info('Succeeded in enabling the listener for window stage event changes. Data: ' +
1070        JSON.stringify(data));
1071      // 根据事件状态类型选择进行具体的处理
1072      if (data === window.WindowStageLifecycleEventType.SHOWN) {
1073        console.info('current window stage event is SHOWN');
1074        // ...
1075      } else if (data === window.WindowStageLifecycleEventType.RESUMED) {
1076        console.info('current window stage event is RESUMED');
1077        // ...
1078      } else if (data === window.WindowStageLifecycleEventType.PAUSED) {
1079        console.info('current window stage event is PAUSED');
1080        // ...
1081      } else if (data === window.WindowStageLifecycleEventType.HIDDEN) {
1082        console.info('current window stage event is HIDDEN');
1083        // ...
1084      }
1085      // ...
1086    }
1087    try {
1088      windowStage.on('windowStageLifecycleEvent', callback);
1089    } catch (exception) {
1090      console.error(`Failed to enable the listener for window stage event changes. Cause code: ${exception.code}, message: ${exception.message}`);
1091    }
1092  }
1093};
1094```
1095
1096## off('windowStageLifecycleEvent')<sup>20+</sup>
1097
1098off(eventType: 'windowStageLifecycleEvent', callback?: Callback&lt;WindowStageLifecycleEventType&gt;): void
1099
1100关闭WindowStage生命周期变化的监听。
1101
1102**模型约束:** 此接口仅可在Stage模型下使用。
1103
1104**系统能力:** SystemCapability.Window.SessionManager
1105
1106**参数:**
1107
1108| 参数名   | 类型                                                         | 必填 | 说明                                                         |
1109| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1110| eventType  | string                                                       | 是   | 监听事件,固定为'windowStageLifecycleEvent',即WindowStage生命周期变化事件。 |
1111| callback | Callback&lt;[WindowStageLifecycleEventType](arkts-apis-window-e.md#windowstagelifecycleeventtype20)&gt; | 否   | 回调函数。返回当前的WindowStage生命周期状态。若传入参数,则关闭该监听。若未传入参数,则关闭所有WindowStage生命周期变化的监听。                |
1112
1113**错误码:**
1114
1115以下错误码的详细介绍请参见[窗口错误码](errorcode-window.md)。
1116
1117| 错误码ID | 错误信息 |
1118| ------- | ------------------------------ |
1119| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
1120| 1300002 | This window state is abnormal. |
1121| 1300005 | This window stage is abnormal. |
1122
1123**示例:**
1124
1125```ts
1126// EntryAbility.ets
1127import { UIAbility } from '@kit.AbilityKit';
1128
1129export default class EntryAbility extends UIAbility {
1130  // ...
1131
1132  onWindowStageCreate(windowStage: window.WindowStage) {
1133    console.info('onWindowStageCreate');
1134    const callback = (windowStageLifecycleEvent: window.WindowStageLifecycleEventType) => {
1135      // ...
1136    }
1137    try {
1138      windowStage.on('windowStageLifecycleEvent', callback);
1139    } catch (exception) {
1140      console.error(`Failed to enable the listener for window stage event changes. Cause code: ${exception.code}, message: ${exception.message}`);
1141    }
1142    try {
1143      windowStage.off('windowStageLifecycleEvent', callback);
1144      // 如果通过on开启多个callback进行监听,同时关闭所有监听:
1145      windowStage.off('windowStageLifecycleEvent');
1146    } catch (exception) {
1147      console.error(`Failed to disable the listener for window stage event changes. Cause code: ${exception.code}, message: ${exception.message}`);
1148    }
1149  }
1150};
1151```
1152
1153## on('windowStageClose')<sup>14+</sup>
1154
1155on(eventType: 'windowStageClose', callback: Callback&lt;void&gt;): void
1156
1157该接口仅在[自由窗口](../../windowmanager/window-terminology.md#自由窗口)状态下生效,用于开启点击主窗三键区的关闭按钮监听事件。点击主窗口的三键区域的关闭键时触发该回调函数,将不执行注册的[UIAbility.onPrepareToTerminate](../apis-ability-kit/js-apis-app-ability-uiAbility.md#onpreparetoterminate10)生命周期回调函数。
1158
1159当重复注册窗口关闭事件的监听时,最后一次注册成功的监听事件生效。
1160
1161触发的回调函数是同步执行,主窗口的异步关闭事件监听参考[on('windowWillClose')](arkts-apis-window-Window.md#onwindowwillclose15)方法。
1162
1163如果存在[on('windowWillClose')](arkts-apis-window-Window.md#onwindowwillclose15)监听事件,只响应[on('windowWillClose')](arkts-apis-window-Window.md#onwindowwillclose15)接口。
1164
1165**模型约束:** 此接口仅可在Stage模型下使用。
1166
1167**系统能力:** SystemCapability.Window.SessionManager
1168
1169**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。
1170
1171**设备行为差异:** 该接口在2in1设备、Tablet设备中可正常调用,在其他设备中返回801错误码。
1172
1173**参数:**
1174
1175| 参数名   | 类型                                                         | 必填 | 说明                                                         |
1176| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1177| eventType  | string                                                       | 是   | 监听事件,固定为'windowStageClose',即开启主窗三键区的关闭按钮监听。|
1178| callback | Callback&lt;void&gt; | 是   | 回调函数。当点击主窗口右上角关闭按钮事件发生时的回调。该回调函数不返回任何参数。回调函数内部逻辑需要有boolean类型的返回值,该返回值决定当前主窗是否继续关闭,true表示不关闭,false表示关闭。|
1179
1180**错误码:**
1181
1182以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
1183
1184| 错误码ID | 错误信息 |
1185| ------- | ------------------------------ |
1186| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1187| 801 | Capability not supported. Failed to call the API due to limited device capabilities. |
1188| 1300002 | This window state is abnormal. |
1189
1190**示例:**
1191
1192```ts
1193// EntryAbility.ets
1194import { UIAbility } from '@kit.AbilityKit';
1195import { window } from '@kit.ArkUI';
1196
1197export default class EntryAbility extends UIAbility {
1198  // ...
1199
1200  onWindowStageCreate(windowStage: window.WindowStage) {
1201    console.info('onWindowStageCreate');
1202    try {
1203      windowStage.on('windowStageClose', () => {
1204        console.info('Succeeded in enabling the listener for window stage close event.');
1205        return false;
1206      });
1207    } catch (exception) {
1208      console.error(`Failed to enable the listener for window stage close event. Cause code: ${exception.code}, message: ${exception.message}`);
1209    }
1210  }
1211};
1212```
1213
1214## off('windowStageClose')<sup>14+</sup>
1215
1216off(eventType: 'windowStageClose', callback?: Callback&lt;void&gt;): void
1217
1218该接口仅在[自由窗口](../../windowmanager/window-terminology.md#自由窗口)状态下生效,用于关闭主窗口关闭事件的监听。
1219
1220**模型约束:** 此接口仅可在Stage模型下使用。
1221
1222**系统能力:** SystemCapability.Window.SessionManager
1223
1224**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。
1225
1226**设备行为差异:** 该接口在2in1设备、Tablet设备中可正常调用,在其他设备中返回801错误码。
1227
1228**参数:**
1229
1230| 参数名   | 类型                                                         | 必填 | 说明                                                         |
1231| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1232| eventType  | string                                                       | 是   | 监听事件,固定为'windowStageClose',即关闭主窗口关闭事件的监听。 |
1233| callback | Callback&lt;void&gt; | 否   | 回调函数。当点击主窗口右上角关闭按钮事件发生时的回调。该回调函数不返回任何参数。回调函数内部逻辑需要有boolean类型的返回值,该返回值决定当前主窗是否继续关闭,true表示不关闭,false表示关闭。如果传入参数,则关闭该监听。如果未传入参数,则关闭所有主窗口关闭的监听。 |
1234
1235**错误码:**
1236
1237以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
1238
1239| 错误码ID | 错误信息 |
1240| ------- | ------------------------------ |
1241| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1242| 801 | Capability not supported. Failed to call the API due to limited device capabilities. |
1243| 1300002 | This window state is abnormal. |
1244
1245**示例:**
1246
1247```ts
1248// EntryAbility.ets
1249import { UIAbility } from '@kit.AbilityKit';
1250import { window } from '@kit.ArkUI';
1251
1252export default class EntryAbility extends UIAbility {
1253  // ...
1254
1255  onWindowStageCreate(windowStage: window.WindowStage) {
1256    console.info('onWindowStageCreate');
1257    const callback = () => {
1258      // ...
1259      return false;
1260    }
1261    try {
1262      windowStage.on('windowStageClose', callback);
1263      windowStage.off('windowStageClose', callback);
1264      windowStage.off('windowStageClose');
1265    } catch (exception) {
1266      console.error(`Failed to disable the listener for window stage close changes. Cause code: ${exception.code}, message: ${exception.message}`);
1267    }
1268  }
1269};
1270```
1271
1272## setDefaultDensityEnabled<sup>12+</sup>
1273
1274setDefaultDensityEnabled(enabled: boolean): void
1275
1276设置应用主窗口是否使用系统默认Density,子窗和系统窗口会跟随主窗生效。调用此接口前,需先调用[WindowStage.loadContent()](#loadcontent9)初始化布局,确保接口调用时序正确。
1277
1278不调用此接口进行设置,则表示不使用系统默认Density。
1279
1280不使用系统默认Density时,若调用过[setCustomDensity()](#setcustomdensity15),则窗口会跟随用户自定义的显示大小变化重新布局,否则跟随系统显示大小变化重新布局。
1281
1282**模型约束:** 此接口仅可在Stage模型下使用。
1283
1284**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
1285
1286**系统能力:** SystemCapability.Window.SessionManager
1287
1288**参数:**
1289
1290| 参数名           | 类型    | 必填 | 说明                         |
1291| ---------------- | ------- | ---- | ---------------------------- |
1292| enabled | boolean | 是   | 是否设置应用使用系统默认Density。true表示使用系统默认Density,窗口不跟随系统显示大小变化重新布局;false表示不使用系统默认Density,窗口跟随系统显示大小变化重新布局。 |
1293
1294**错误码:**
1295
1296以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
1297
1298| 错误码ID | 错误信息 |
1299| ------- | ------------------------------ |
1300| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1301| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
1302| 1300002 | This window state is abnormal. |
1303| 1300005 | This window stage is abnormal. |
1304
1305**示例:**
1306
1307```ts
1308// EntryAbility.ets
1309import { UIAbility } from '@kit.AbilityKit';
1310import { window } from '@kit.ArkUI';
1311import { BusinessError } from '@kit.BasicServicesKit'
1312
1313export default class EntryAbility extends UIAbility {
1314  // ...
1315
1316  onWindowStageCreate(windowStage: window.WindowStage) {
1317      windowStage.loadContent("pages/page2", (err: BusinessError) => {
1318        let errCode: number = err.code;
1319        if (errCode) {
1320          console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
1321          return;
1322        }
1323        console.info('onWindowStageCreate');
1324      try {
1325        windowStage.setDefaultDensityEnabled(true);
1326        console.info('Succeeded in loading the content.');
1327      } catch (exception) {
1328        console.error(`Failed to set default density enabled. Cause code: ${exception.code}, message: ${exception.message}`);
1329      }
1330    });
1331  }
1332};
1333```
1334
1335## setCustomDensity<sup>15+</sup>
1336
1337setCustomDensity(density: number): void
1338
1339支持应用主窗口自定义其显示大小缩放系数。
1340
1341已创建的子窗和系统窗口不会立即跟随主窗的customDensity变化重新布局,而是在子窗或系统窗口下一次位置、大小、系统缩放大小等窗口布局信息变化时跟随主窗的customDensity变化重新布局。
1342
1343当存在同时使用该接口和[setDefaultDensityEnabled(true)](#setdefaultdensityenabled12)的情况时,以最后调用的设置效果为准。
1344
1345**模型约束:** 此接口仅可在Stage模型下使用。
1346
1347**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
1348
1349**系统能力:** SystemCapability.Window.SessionManager
1350
1351**参数:**
1352
1353| 参数名           | 类型    | 必填 | 说明                         |
1354| ---------------- | ------- | ---- | ---------------------------- |
1355| density | number | 是   | 自定义显示大小缩放系数。该参数为浮点数,取值范围为[0.5, 4.0]或-1.0。4.0表示窗口可显示的最大显示大小缩放系数,-1.0表示窗口使用系统显示大小缩放系数。 |
1356
1357**错误码:**
1358
1359以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
1360
1361| 错误码ID | 错误信息 |
1362| ------- | ------------------------------ |
1363| 401     | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1364| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
1365| 1300002 | This window state is abnormal. |
1366| 1300005 | This window stage is abnormal. |
1367
1368**示例:**
1369
1370```ts
1371// EntryAbility.ets
1372import { UIAbility } from '@kit.AbilityKit';
1373
1374export default class EntryAbility extends UIAbility {
1375  // ...
1376
1377  onWindowStageCreate(windowStage: window.WindowStage) {
1378    console.info('onWindowStageCreate');
1379    try {
1380      windowStage.setCustomDensity(-1.0);
1381    } catch (exception) {
1382      console.error(`Failed to set custom density. Cause code: ${exception.code}, message: ${exception.message}`);
1383    }
1384  }
1385};
1386```
1387
1388## setCustomDensity<sup>20+</sup>
1389
1390setCustomDensity(density: number, applyToSubWindow?: boolean): void
1391
1392支持应用主窗口自定义显示大小缩放系数,并设置已创建的子窗和系统窗口跟随主窗重新布局的生效时机。
1393
1394当存在同时使用该接口和[setDefaultDensityEnabled(true)](#setdefaultdensityenabled12)的情况时,以最后调用的设置效果为准。
1395
1396**模型约束:** 此接口仅可在Stage模型下使用。
1397
1398**系统能力:** SystemCapability.Window.SessionManager
1399
1400**参数:**
1401
1402| 参数名           | 类型    | 必填 | 说明                         |
1403| ---------------- | ------- | ---- | ---------------------------- |
1404| density | number | 是   | 自定义显示大小缩放系数。该参数为浮点数,取值范围为[0.5, 4.0]或-1.0。4.0表示窗口可显示的最大显示大小缩放系数,-1.0表示窗口使用系统显示大小缩放系数。 |
1405| applyToSubWindow | boolean | 否   | 设置当前已创建的子窗和系统窗口是否跟随立即跟随主窗口更新customDensity并重新布局。设置为true时,表示立即跟随主窗生效;设置为false时,表示不会立即跟随主窗生效,而是在子窗或系统窗口下一次位置、大小、系统缩放大小等窗口布局信息变化时跟随主窗的customDensity变化重新布局。默认值为false。 |
1406
1407**错误码:**
1408
1409以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
1410
1411| 错误码ID | 错误信息 |
1412| ------- | ------------------------------ |
1413| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
1414| 1300002 | This window state is abnormal. |
1415| 1300005 | This window stage is abnormal. |
1416
1417**示例:**
1418
1419```ts
1420// EntryAbility.ets
1421import { UIAbility } from '@kit.AbilityKit';
1422
1423export default class EntryAbility extends UIAbility {
1424  // ...
1425
1426  onWindowStageCreate(windowStage: window.WindowStage) {
1427    console.info('onWindowStageCreate');
1428    try {
1429      windowStage.setCustomDensity(2.0);
1430      windowStage.setCustomDensity(3.0, true);
1431      windowStage.setCustomDensity(-1.0, false);
1432    } catch (exception) {
1433      console.error(`Failed to set custom density. Cause code: ${exception.code}, message: ${exception.message}`);
1434    }
1435  }
1436};
1437```
1438
1439## setWindowModal<sup>14+</sup>
1440
1441setWindowModal(isModal: boolean): Promise&lt;void&gt;
1442
1443该接口仅在[自由窗口](../../windowmanager/window-terminology.md#自由窗口)状态下生效,用于设置主窗的模态属性是否启用,使用Promise异步回调。
1444
1445主窗口调用该接口时,设置主窗口模态属性是否启用。启用主窗口模态属性后,其相同应用进程下的其他主窗口以及其他主窗口的子窗口不能响应用户操作,直到该主窗口关闭或者主窗口的模态属性被禁用。
1446
1447**模型约束:** 此接口仅可在Stage模型下使用。
1448
1449**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。
1450
1451**系统能力:** SystemCapability.Window.SessionManager
1452
1453**设备行为差异:** 该接口在2in1设备、Tablet设备中可正常调用,在其他设备中返回801错误码。
1454
1455**参数:**
1456
1457| 参数名    | 类型    | 必填 | 说明                                          |
1458| --------- | ------- | ---- | --------------------------------------------- |
1459| isModal | boolean | 是   | 设置主窗口模态属性是否启用,true为启用,false为不启用。 |
1460
1461
1462**返回值:**
1463
1464| 类型 | 说明 |
1465| ------------------- | ------------------------ |
1466| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1467
1468**错误码:**
1469
1470以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
1471
1472| 错误码ID | 错误信息                       |
1473| -------- | ------------------------------ |
1474| 401      | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1475| 801      | Capability not supported. Failed to call the API due to limited device capabilities. |
1476| 1300002  | This window state is abnormal. |
1477| 1300003  | This window manager service works abnormally. |
1478| 1300005 | This window stage is abnormal. |
1479
1480**示例:**
1481
1482```ts
1483// EntryAbility.ets
1484import { UIAbility } from '@kit.AbilityKit';
1485import { BusinessError } from '@kit.BasicServicesKit';
1486
1487export default class EntryAbility extends UIAbility {
1488  // ...
1489  onWindowStageCreate(windowStage: window.WindowStage): void {
1490    console.info('onWindowStageCreate');
1491    try {
1492      let promise = windowStage.setWindowModal(true);
1493      promise.then(() => {
1494        console.info('Succeeded in setting window modal');
1495      }).catch((err: BusinessError) => {
1496        console.error(`Failed to set window modal. Cause code: ${err.code}, message: ${err.message}`);
1497      });
1498    } catch (exception) {
1499      console.error(`Failed to set window modal. Cause code: ${exception.code}, message: ${exception.message}`);
1500    }
1501  }
1502}
1503```
1504
1505## removeStartingWindow<sup>14+</sup>
1506
1507removeStartingWindow(): Promise&lt;void&gt;
1508
1509支持应用控制启动页消失时机。
1510
1511此接口只对应用主窗口生效,且需要在module.json5配置文件[abilities](../../quick-start/module-configuration-file.md#abilities标签)标签中的metadata标签下配置"enable.remove.starting.window"为"true"才会生效。
1512
1513在标签配置为"true"的情况下,系统提供了启动页超时保护机制,若5s内未调用此接口,系统将自动移除启动页。
1514
1515若标签配置为"false"或未配置标签,则此接口不生效,启动页将会在应用首帧渲染完成后自动移除。
1516
1517**模型约束:** 此接口仅可在Stage模型下使用。
1518
1519**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。
1520
1521**系统能力:** SystemCapability.Window.SessionManager
1522
1523**返回值:**
1524
1525| 类型                | 说明                      |
1526| ------------------- | ------------------------- |
1527| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1528
1529**错误码:**
1530
1531以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
1532
1533| 错误码ID | 错误信息 |
1534| ------- | ------------------------------ |
1535| 801     | Capability not supported. Failed to call the API due to limited device capabilities. |
1536| 1300002 | This window state is abnormal. |
1537| 1300003 | This window manager service works abnormally. |
1538
1539**示例:**
1540
1541```ts
1542// EntryAbility.ets
1543import { UIAbility } from '@kit.AbilityKit';
1544import { BusinessError } from '@kit.BasicServicesKit';
1545
1546export default class EntryAbility extends UIAbility {
1547  // ...
1548
1549  onWindowStageCreate(windowStage: window.WindowStage) {
1550    console.info('onWindowStageCreate');
1551    windowStage.removeStartingWindow().then(() => {
1552      console.info('Succeeded in removing starting window.');
1553    }).catch((err: BusinessError) => {
1554        console.error(`Failed to remove starting window. Cause code: ${err.code}, message: ${err.message}`);
1555    });
1556  }
1557};
1558```
1559
1560## setWindowRectAutoSave<sup>14+</sup>
1561
1562setWindowRectAutoSave(enabled: boolean): Promise&lt;void&gt;
1563
1564设置是否启用最后关闭的主窗尺寸的记忆功能,使用Promise异步回调。
1565
1566启用记忆功能后,在同一个UIAbility下,记忆最后关闭的主窗口的尺寸;此主窗口再次启动时,以记忆的尺寸按照规则进行打开。
1567
1568层叠规则: 1、当前实例是自由窗口时,打开下一实例窗口层叠时,大小要跟随。2、当前实例是最大化或全屏窗口时,打开下一个实例窗口层叠时,保持最大化。
1569
1570记忆规则:
1571|上一次窗口状态|记忆规则|
1572|-------------|-------|
1573|自由窗口|保留自由窗口的大小/位置,超出工作区回弹|
1574|二分屏窗口|保留二分屏之前自由窗口的大小/位置|
1575|最大化窗口|保留最大化|
1576|沉浸式窗口|保留沉浸式之前自由窗口的大小/位置|
1577|最小化窗口|保留最小化之前自由窗口的大小/位置|
1578
1579**模型约束:** 此接口仅可在Stage模型下使用。
1580
1581**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。
1582
1583**系统能力:** SystemCapability.Window.SessionManager
1584
1585**设备行为差异:** 该接口在2in1设备中可正常调用,在其他设备中返回801错误码。
1586
1587**参数:**
1588
1589| 参数名    | 类型    | 必填 | 说明                                          |
1590| --------- | ------- | ---- | --------------------------------------------- |
1591| enabled | boolean | 是   | 设置是否启用主窗尺寸的记忆功能,true为启用,false为不启用。 |
1592
1593
1594**返回值:**
1595
1596| 类型 | 说明 |
1597| ------------------- | ------------------------ |
1598| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1599
1600**错误码:**
1601
1602以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
1603
1604| 错误码ID | 错误信息                       |
1605| -------- | ------------------------------ |
1606| 401      | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1607| 801      | Capability not supported. Failed to call the API due to limited device capabilities. |
1608| 1300002  | This window state is abnormal. |
1609| 1300003  | This window manager service works abnormally. |
1610
1611**示例:**
1612
1613```ts
1614// EntryAbility.ets
1615import { UIAbility } from '@kit.AbilityKit';
1616import { BusinessError } from '@kit.BasicServicesKit';
1617
1618export default class EntryAbility extends UIAbility {
1619  // ...
1620  onWindowStageCreate(windowStage: window.WindowStage): void {
1621    console.info('onWindowStageCreate');
1622    try {
1623      let promise = windowStage.setWindowRectAutoSave(true);
1624      promise.then(() => {
1625        console.info('Succeeded in setting window rect auto-save');
1626      }).catch((err: BusinessError) => {
1627        console.error(`Failed to set window rect auto-save. Cause code: ${err.code}, message: ${err.message}`);
1628      });
1629    } catch (exception) {
1630      console.error(`Failed to set window rect auto-save. Cause code: ${exception.code}, message: ${exception.message}`);
1631    }
1632  }
1633}
1634```
1635
1636## setWindowRectAutoSave<sup>17+</sup>
1637
1638setWindowRectAutoSave(enabled: boolean, isSaveBySpecifiedFlag: boolean): Promise&lt;void&gt;
1639
1640设置是否启用主窗的尺寸记忆功能,使用Promise异步回调。
1641
1642在同一个UIAbility下,可记忆最后关闭的主窗口尺寸,也可针对每个主窗口尺寸单独进行记忆。只有在UIAbility启动模式为specified,且isSaveBySpecifiedFlag设置为true时,才能针对每个主窗口尺寸进行单独记忆。
1643
1644启用记忆功能后,记忆主窗口关闭时的尺寸;对应主窗口再次启动时,以记忆的尺寸按照规则进行打开。
1645
1646记忆规则:
1647|上一次窗口状态|记忆规则|
1648|-------------|-------|
1649|自由窗口|保留自由窗口的大小/位置,超出工作区回弹。|
1650|二分屏窗口|保留二分屏之前自由窗口的大小/位置。|
1651|最大化窗口|保留最大化。|
1652|沉浸式窗口|保留沉浸式之前自由窗口的大小/位置。|
1653|最小化窗口|保留最小化之前自由窗口的大小/位置。|
1654
1655**模型约束:** 此接口仅可在Stage模型下使用。
1656
1657**原子化服务API:** 从API version 17开始,该接口支持在原子化服务中使用。
1658
1659**系统能力:** SystemCapability.Window.SessionManager
1660
1661**设备行为差异:** 该接口在2in1设备中可正常调用,在其他设备中返回801错误码。
1662
1663**参数:**
1664
1665| 参数名    | 类型    | 必填 | 说明                                          |
1666| --------- | ------- | ---- | --------------------------------------------- |
1667| enabled | boolean | 是   | 设置是否启用主窗的尺寸记忆功能,true为启用,false为不启用。 |
1668| isSaveBySpecifiedFlag | boolean | 是   | 设置specified模式下是否启用对窗口进行单独记忆,true为启用,false为不启用。 |
1669
1670
1671**返回值:**
1672
1673| 类型 | 说明 |
1674| ------------------- | ------------------------ |
1675| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1676
1677**错误码:**
1678
1679以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
1680
1681| 错误码ID | 错误信息                       |
1682| -------- | ------------------------------ |
1683| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1684| 801      | Capability not supported. Function setWindowRectAutoSave can not work correctly due to limited device capabilities. |
1685| 1300002  | This window state is abnormal. |
1686| 1300003  | This window manager service works abnormally. |
1687
1688**示例:**
1689
1690```ts
1691// EntryAbility.ets
1692import { UIAbility } from '@kit.AbilityKit';
1693import { BusinessError } from '@kit.BasicServicesKit';
1694
1695export default class EntryAbility extends UIAbility {
1696  // ...
1697  onWindowStageCreate(windowStage: window.WindowStage): void {
1698    console.info('onWindowStageCreate');
1699    try {
1700      let promise = windowStage.setWindowRectAutoSave(true, true);
1701      promise.then(() => {
1702        console.info('Succeeded in setting window rect auto-save');
1703      }).catch((err: BusinessError) => {
1704        console.error(`Failed to set window rect auto-save. Cause code: ${err.code}, message: ${err.message}`);
1705      });
1706    } catch (exception) {
1707      console.error(`Failed to set window rect auto-save. Cause code: ${exception.code}, message: ${exception.message}`);
1708    }
1709  }
1710}
1711```
1712
1713## isWindowRectAutoSave<sup>14+</sup>
1714
1715isWindowRectAutoSave(): Promise&lt;boolean&gt;
1716
1717判断当前主窗口是否已经启用尺寸记忆,使用Promise异步回调。
1718
1719**模型约束:** 此接口仅可在Stage模型下使用。
1720
1721**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。
1722
1723**系统能力:** SystemCapability.Window.SessionManager
1724
1725**设备行为差异:** 该接口在2in1设备中可正常调用,在其他设备中返回801错误码。
1726
1727**返回值:**
1728
1729| 类型 | 说明 |
1730| ---------------------- | ------------------------------------------------------------------------------------ |
1731| Promise&lt;boolean&gt; | Promise对象。返回true表示当前窗口启用尺寸记忆,返回false表示当前窗口禁用尺寸记忆。 |
1732
1733**错误码:**
1734
1735以下错误码的详细介绍请参见[窗口错误码](errorcode-window.md)。
1736
1737| 错误码ID | 错误信息 |
1738| ------- | ------------------------------ |
1739| 801      | Capability not supported. Failed to call the API due to limited device capabilities. |
1740| 1300002 | This window state is abnormal. |
1741| 1300003 | This window manager service works abnormally. |
1742
1743**示例:**
1744
1745```ts
1746// EntryAbility.ets
1747import { UIAbility } from '@kit.AbilityKit';
1748import { BusinessError } from '@kit.BasicServicesKit';
1749
1750export default class EntryAbility extends UIAbility {
1751  // ...
1752  onWindowStageCreate(windowStage: window.WindowStage): void {
1753    console.info('onWindowStageCreate');
1754    try {
1755      let promise = windowStage.isWindowRectAutoSave();
1756      promise.then((data) => {
1757        console.info(`Succeeded in checking whether the window support the rect auto-save. Data: ${data}`);
1758      }).catch((err: BusinessError) => {
1759        console.error(`Failed to check whether the window support the rect auto-save. Cause code: ${err.code}, message: ${err.message}`);
1760      });
1761    } catch (exception) {
1762      console.error(`Failed to check whether the window support the rect auto-save. Cause code: ${exception.code}, message: ${exception.message}`);
1763    }
1764  }
1765}
1766```
1767
1768## setSupportedWindowModes<sup>15+</sup>
1769
1770setSupportedWindowModes(supportedWindowModes: Array<bundleManager.SupportWindowMode>): Promise&lt;void&gt;
1771
1772该接口仅在[自由窗口](../../windowmanager/window-terminology.md#自由窗口)状态下生效,用于设置主窗的窗口支持模式,使用Promise异步回调。
1773
1774**模型约束:** 此接口仅可在Stage模型下使用。
1775
1776**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
1777
1778**系统能力:** SystemCapability.Window.SessionManager
1779
1780**设备行为差异:** 该接口在2in1设备、Tablet设备中可正常调用,在其他设备中返回801错误码。
1781
1782**参数:**
1783
1784| 参数名    | 类型    | 必填 | 说明                                          |
1785| --------- | ------- | ---- | --------------------------------------------- |
1786| supportedWindowModes | Array&lt;[bundleManager.SupportWindowMode](../apis-ability-kit/js-apis-bundleManager.md#supportwindowmode)&gt; | 是   | 设置主窗的窗口支持模式。<br>- FULL_SCREEN:支持全屏模式。<br>- FLOATING:支持悬浮窗模式。<br>- SPLIT:支持分屏模式。需要配合FULL_SCREEN或FLOATING一起使用,不支持仅配置SPLIT。<br> 注:数组中SupportWindowMode字段取值不应该与该UIAbility对应的[module.json5配置文件](../../quick-start/module-configuration-file.md)中[abilities标签](../../quick-start/module-configuration-file.md#abilities标签)的supportWindowMode字段取值或者[StartOptions](../apis-ability-kit/js-apis-app-ability-startOptions.md)中[属性](../apis-ability-kit/js-apis-app-ability-startOptions.md#startoptions)的supportWindowModes字段取值冲突。当取值冲突时,最终以该参数设置的窗口支持模式为准。|
1787
1788**返回值:**
1789
1790| 类型 | 说明 |
1791| ------------------- | ------------------------ |
1792| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1793
1794**错误码:**
1795
1796以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
1797
1798| 错误码ID | 错误信息                       |
1799| -------- | ------------------------------ |
1800| 401      | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1801| 801      | Capability not supported. Failed to call the API due to limited device capabilities. |
1802| 1300002  | This window state is abnormal. |
1803| 1300003  | This window manager service works abnormally. |
1804
1805**示例:**
1806
1807```ts
1808// EntryAbility.ets
1809import { UIAbility, bundleManager } from '@kit.AbilityKit';
1810import { BusinessError } from '@kit.BasicServicesKit';
1811
1812export default class EntryAbility extends UIAbility {
1813  // ...
1814  onWindowStageCreate(windowStage: window.WindowStage): void {
1815    console.info('onWindowStageCreate');
1816    try {
1817      let promise = windowStage.setSupportedWindowModes([
1818        bundleManager.SupportWindowMode.FULL_SCREEN,
1819        bundleManager.SupportWindowMode.SPLIT,
1820        bundleManager.SupportWindowMode.FLOATING
1821      ]);
1822      promise.then(() => {
1823        console.info('Succeeded in setting window support modes');
1824      }).catch((err: BusinessError) => {
1825        console.error(`Failed to set window support modes. Cause code: ${err.code}, message: ${err.message}`);
1826      });
1827    } catch (exception) {
1828      console.error(`Failed to set window support modes. Cause code: ${exception.code}, message: ${exception.message}`);
1829    }
1830  }
1831}
1832```
1833
1834## setSupportedWindowModes<sup>20+</sup>
1835
1836setSupportedWindowModes(supportedWindowModes: Array<bundleManager.SupportWindowMode>, grayOutMaximizeButton: boolean): Promise&lt;void&gt;
1837
1838该接口仅在[自由窗口](../../windowmanager/window-terminology.md#自由窗口)状态下生效,用于设置主窗的窗口支持模式,并提供最大化按钮置灰功能,使用Promise异步回调。
1839
1840**模型约束:** 此接口仅可在Stage模型下使用。
1841
1842**系统能力:** SystemCapability.Window.SessionManager
1843
1844**设备行为差异:** 该接口在2in1设备、Tablet设备中可正常调用,在其他设备中返回801错误码。
1845
1846**参数:**
1847
1848| 参数名    | 类型    | 必填 | 说明                                          |
1849| --------- | ------- | ---- | --------------------------------------------- |
1850| supportedWindowModes | Array&lt;[bundleManager.SupportWindowMode](../apis-ability-kit/js-apis-bundleManager.md#supportwindowmode)&gt; | 是   | 设置主窗的窗口支持模式。<br>- FULL_SCREEN:支持全屏模式。<br>- FLOATING:支持悬浮窗模式。<br>- SPLIT:支持分屏模式。需要配合FULL_SCREEN或FLOATING一起使用,不支持仅配置SPLIT。<br> 注:数组中SupportWindowMode字段取值不应该与该UIAbility对应的[module.json5配置文件](../../quick-start/module-configuration-file.md)中[abilities标签](../../quick-start/module-configuration-file.md#abilities标签)的supportWindowMode字段取值或者[StartOptions](../apis-ability-kit/js-apis-app-ability-startOptions.md#startoptions)中属性的supportWindowModes字段取值冲突。当取值冲突时,最终以该参数设置的窗口支持模式为准。|
1851| grayOutMaximizeButton | boolean | 是 | 是否显示并将主窗口的最大化按钮置灰。true表示显示并将主窗口的最大化按钮置灰,此时最大化按钮不可用;false表示不显示主窗口的最大化按钮。此参数配置仅在supportedWindowModes不支持FULL_SCREEN时生效。 |
1852
1853**返回值:**
1854
1855| 类型 | 说明 |
1856| ------------------- | ------------------------ |
1857| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1858
1859**错误码:**
1860
1861以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
1862
1863| 错误码ID | 错误信息                       |
1864| -------- | ------------------------------ |
1865| 801      | Capability not supported. Function setSupportedWindowModes can not work correctly due to limited device capabilities. |
1866| 1300002  | This window state is abnormal. |
1867| 1300003  | This window manager service works abnormally. |
1868| 1300016  | Parameter error. Possible cause: 1. Invalid parameter range. 2. Invalid parameter length. 3. Incorrect parameter format. |
1869
1870**示例:**
1871
1872```ts
1873// EntryAbility.ets
1874import { UIAbility, bundleManager } from '@kit.AbilityKit';
1875import { BusinessError } from '@kit.BasicServicesKit';
1876
1877export default class EntryAbility extends UIAbility {
1878  // ...
1879  onWindowStageCreate(windowStage: window.WindowStage): void {
1880    console.info('onWindowStageCreate');
1881    try {
1882      let promise = windowStage.setSupportedWindowModes([
1883        bundleManager.SupportWindowMode.FULL_SCREEN,
1884        bundleManager.SupportWindowMode.SPLIT,
1885        bundleManager.SupportWindowMode.FLOATING
1886      ], true);
1887      promise.then(() => {
1888        console.info('Succeeded in setting window support modes');
1889      }).catch((err: BusinessError) => {
1890        console.error(`Failed to set window support modes. Cause code: ${err.code}, message: ${err.message}`);
1891      });
1892    } catch (exception) {
1893      console.error(`Failed to set window support modes. Cause code: ${exception.code}, message: ${exception.message}`);
1894    }
1895  }
1896}
1897```