未验证 提交 512db816 编写于 作者: L lixia 提交者: Gitee

update zh-cn/application-dev/reference/apis/js-apis-window.md.

js-apis-window.md窗口ts示例代码 arkts 告警修复
Signed-off-by: Nlixia <lixia110@huawei.com>
上级 d355b984
...@@ -2652,27 +2652,33 @@ getUIContext(): UIContext ...@@ -2652,27 +2652,33 @@ getUIContext(): UIContext
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
import { UIContext } from '@ohos.arkui.UIContext';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
// 为主窗口加载对应的目标页面。 // 为主窗口加载对应的目标页面。
windowStage.loadContent("pages/page2", (err) => { windowStage.loadContent("pages/page2", (err: BusinessError) => {
if (err.code) { let errCode: number = err.code;
if (errCode) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err)); console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return; return;
} }
console.info('Succeeded in loading the content.'); console.info('Succeeded in loading the content.');
// 获取应用主窗口。 // 获取应用主窗口。
let windowClass = null; let windowClass: window.Window | null = null;
windowStage.getMainWindow((err, data) => { windowStage.getMainWindow((err: BusinessError, data) => {
if (err.code) { let errCode: number = err.code;
if (errCode) {
console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
return; return;
} }
windowClass = data; windowClass = data;
console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
// 获取UIContext实例。 // 获取UIContext实例。
globalThis.uiContext = windowClass.getUIContext(); let uiContext: UIContext | null = null;
uiContext = windowClass.getUIContext();
}) })
}); });
} }
...@@ -2793,20 +2799,37 @@ loadContent(path: string, storage: LocalStorage, callback: AsyncCallback&lt;void ...@@ -2793,20 +2799,37 @@ loadContent(path: string, storage: LocalStorage, callback: AsyncCallback&lt;void
**示例:** **示例:**
```ts ```ts
let storage = new LocalStorage(); import UIAbility from '@ohos.app.ability.UIAbility';
storage.setOrCreate('storageSimpleProp',121); import window from '@ohos.window';
console.log('onWindowStageCreate'); import { BusinessError } from '@ohos.base';
try {
windowClass.loadContent('pages/page2', storage, (err) => { export default class EntryAbility extends UIAbility {
if (err.code) { // ...
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
let windowClass: window.Window | null = null;
let storage: LocalStorage = new LocalStorage();
storage.setOrCreate('storageSimpleProp', 121);
try {
if (!windowClass) {
console.info('Failed to load the content. Cause: windowClass is null');
}
else {
(windowClass as window.Window).loadContent('pages/page2', storage, (err: BusinessError) => {
const errCode: number = err.code;
if (errCode) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err)); console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return; return;
} }
console.info('Succeeded in loading the content.'); console.info('Succeeded in loading the content.');
}); });
} catch (exception) { }
} catch (exception) {
console.error('Failed to load the content. Cause:' + JSON.stringify(exception)); console.error('Failed to load the content. Cause:' + JSON.stringify(exception));
} }
}
};
``` ```
### loadContent<sup>9+</sup> ### loadContent<sup>9+</sup>
...@@ -2844,19 +2867,35 @@ loadContent(path: string, storage: LocalStorage): Promise&lt;void&gt; ...@@ -2844,19 +2867,35 @@ loadContent(path: string, storage: LocalStorage): Promise&lt;void&gt;
**示例:** **示例:**
```ts ```ts
let storage = new LocalStorage(); import UIAbility from '@ohos.app.ability.UIAbility';
storage.setOrCreate('storageSimpleProp',121); import window from '@ohos.window';
console.log('onWindowStageCreate'); import { BusinessError } from '@ohos.base';
try {
let promise = windowClass.loadContent('pages/page2', storage); export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
let windowClass: window.Window | null = null;
let storage: LocalStorage = new LocalStorage();
storage.setOrCreate('storageSimpleProp', 121);
try {
if (!windowClass) {
console.info('Failed to load the content. Cause: windowClass is null');
}
else {
let promise = (windowClass as window.Window).loadContent('pages/page2', storage);
promise.then(() => { promise.then(() => {
console.info('Succeeded in loading the content.'); console.info('Succeeded in loading the content.');
}).catch((err) => { }).catch((err: BusinessError) => {
console.error('Failed to load the content. Cause:' + JSON.stringify(err)); console.error('Failed to load the content. Cause:' + JSON.stringify(err));
}); });
} catch (exception) { }
} catch (exception) {
console.error('Failed to load the content. Cause:' + JSON.stringify(exception)); console.error('Failed to load the content. Cause:' + JSON.stringify(exception));
} }
}
};
``` ```
### isWindowShowing<sup>9+</sup> ### isWindowShowing<sup>9+</sup>
...@@ -6893,15 +6932,18 @@ getMainWindow(callback: AsyncCallback&lt;Window&gt;): void ...@@ -6893,15 +6932,18 @@ getMainWindow(callback: AsyncCallback&lt;Window&gt;): void
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
let windowClass = null; let windowClass: window.Window | null = null;
windowStage.getMainWindow((err, data) => { windowStage.getMainWindow((err: BusinessError, data) => {
if (err.code) { const errCode: number = err.code;
if (errCode) {
console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
return; return;
} }
...@@ -6941,18 +6983,20 @@ getMainWindow(): Promise&lt;Window&gt; ...@@ -6941,18 +6983,20 @@ getMainWindow(): Promise&lt;Window&gt;
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
let windowClass = null; let windowClass: window.Window | null = null;
let promise = windowStage.getMainWindow(); let promise = windowStage.getMainWindow();
promise.then((data) => { promise.then((data) => {
windowClass = data; windowClass = data;
console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
}).catch((err) => { }).catch((err: BusinessError) => {
console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
}); });
} }
...@@ -6988,17 +7032,18 @@ getMainWindowSync(): Window ...@@ -6988,17 +7032,18 @@ getMainWindowSync(): Window
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
try { try {
let windowClass = windowStage.getMainWindowSync(); let windowClass = windowStage.getMainWindowSync();
} catch (exception) { } catch (exception) {
console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(exception)); console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(exception));
}; }
} }
}; };
``` ```
...@@ -7033,16 +7078,19 @@ createSubWindow(name: string, callback: AsyncCallback&lt;Window&gt;): void ...@@ -7033,16 +7078,19 @@ createSubWindow(name: string, callback: AsyncCallback&lt;Window&gt;): void
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
let windowClass = null; let windowClass: window.Window | null = null;
try { try {
windowStage.createSubWindow('mySubWindow', (err, data) => { windowStage.createSubWindow('mySubWindow', (err: BusinessError, data) => {
if (err.code) { const errCode: number = err.code;
if (errCode) {
console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err)); console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err));
return; return;
} }
...@@ -7052,7 +7100,7 @@ export default class EntryAbility extends UIAbility { ...@@ -7052,7 +7100,7 @@ export default class EntryAbility extends UIAbility {
}); });
} catch (exception) { } catch (exception) {
console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(exception)); console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(exception));
}; }
} }
}; };
``` ```
...@@ -7091,24 +7139,26 @@ createSubWindow(name: string): Promise&lt;Window&gt; ...@@ -7091,24 +7139,26 @@ createSubWindow(name: string): Promise&lt;Window&gt;
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
let windowClass = null; let windowClass: window.Window | null = null;
try { try {
let promise = windowStage.createSubWindow('mySubWindow'); let promise = windowStage.createSubWindow('mySubWindow');
promise.then((data) => { promise.then((data) => {
windowClass = data; windowClass = data;
console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data)); console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
}).catch((err) => { }).catch((err: BusinessError) => {
console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err)); console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err));
}); });
} catch (exception) { } catch (exception) {
console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(exception)); console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(exception));
}; }
} }
}; };
``` ```
...@@ -7141,15 +7191,18 @@ getSubWindow(callback: AsyncCallback&lt;Array&lt;Window&gt;&gt;): void ...@@ -7141,15 +7191,18 @@ getSubWindow(callback: AsyncCallback&lt;Array&lt;Window&gt;&gt;): void
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
let windowClass = null; let windowClass: window.Window[] = [];
windowStage.getSubWindow((err, data) => { windowStage.getSubWindow((err: BusinessError, data) => {
if (err.code) { const errCode: number = err.code;
if (errCode) {
console.error('Failed to obtain the subwindow. Cause: ' + JSON.stringify(err)); console.error('Failed to obtain the subwindow. Cause: ' + JSON.stringify(err));
return; return;
} }
...@@ -7187,18 +7240,20 @@ getSubWindow(): Promise&lt;Array&lt;Window&gt;&gt; ...@@ -7187,18 +7240,20 @@ getSubWindow(): Promise&lt;Array&lt;Window&gt;&gt;
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
let windowClass = null; let windowClass: window.Window[] = [];
let promise = windowStage.getSubWindow(); let promise = windowStage.getSubWindow();
promise.then((data) => { promise.then((data) => {
windowClass = data; windowClass = data;
console.info('Succeeded in obtaining the subwindow. Data: ' + JSON.stringify(data)); console.info('Succeeded in obtaining the subwindow. Data: ' + JSON.stringify(data));
}).catch((err) => { }).catch((err: BusinessError) => {
console.error('Failed to obtain the subwindow. Cause: ' + JSON.stringify(err)); console.error('Failed to obtain the subwindow. Cause: ' + JSON.stringify(err));
}) })
} }
...@@ -7235,18 +7290,21 @@ loadContent(path: string, storage: LocalStorage, callback: AsyncCallback&lt;void ...@@ -7235,18 +7290,21 @@ loadContent(path: string, storage: LocalStorage, callback: AsyncCallback&lt;void
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
storage : LocalStorage storage: LocalStorage = new LocalStorage();
onWindowStageCreate(windowStage) {
this.storage = new LocalStorage(); onWindowStageCreate(windowStage: window.WindowStage) {
this.storage.setOrCreate('storageSimpleProp',121); this.storage.setOrCreate('storageSimpleProp', 121);
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
try { try {
windowStage.loadContent('pages/page2',this.storage,(err) => { windowStage.loadContent('pages/page2', this.storage, (err: BusinessError) => {
if (err.code) { const errCode: number = err.code;
if (errCode) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err)); console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return; return;
} }
...@@ -7254,7 +7312,7 @@ export default class EntryAbility extends UIAbility { ...@@ -7254,7 +7312,7 @@ export default class EntryAbility extends UIAbility {
}); });
} catch (exception) { } catch (exception) {
console.error('Failed to load the content. Cause:' + JSON.stringify(exception)); console.error('Failed to load the content. Cause:' + JSON.stringify(exception));
}; }
} }
}; };
``` ```
...@@ -7295,25 +7353,28 @@ loadContent(path: string, storage?: LocalStorage): Promise&lt;void&gt; ...@@ -7295,25 +7353,28 @@ loadContent(path: string, storage?: LocalStorage): Promise&lt;void&gt;
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
storage : LocalStorage storage: LocalStorage = new LocalStorage();
onWindowStageCreate(windowStage) {
this.storage = new LocalStorage(); onWindowStageCreate(windowStage: window.WindowStage) {
this.storage.setOrCreate('storageSimpleProp',121); this.storage.setOrCreate('storageSimpleProp', 121);
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
try { try {
let promise = windowStage.loadContent('pages/page2',this.storage); let promise = windowStage.loadContent('pages/page2', this.storage);
promise.then(() => { promise.then(() => {
console.info('Succeeded in loading the content.'); console.info('Succeeded in loading the content.');
}).catch((err) => { }).catch((err: BusinessError) => {
console.error('Failed to load the content. Cause:' + JSON.stringify(err)); console.error('Failed to load the content. Cause:' + JSON.stringify(err));
}); });
} catch (exception) { } catch (exception) {
console.error('Failed to load the content. Cause:' + JSON.stringify(exception)); console.error('Failed to load the content. Cause:' + JSON.stringify(exception));
}; }
;
} }
}; };
``` ```
...@@ -7348,15 +7409,18 @@ loadContent(path: string, callback: AsyncCallback&lt;void&gt;): void ...@@ -7348,15 +7409,18 @@ loadContent(path: string, callback: AsyncCallback&lt;void&gt;): void
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
try { try {
windowStage.loadContent('pages/page2', (err) => { windowStage.loadContent('pages/page2', (err: BusinessError) => {
if (err.code) { const errCode: number = err.code;
if (errCode) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err)); console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return; return;
} }
...@@ -7364,7 +7428,7 @@ export default class EntryAbility extends UIAbility { ...@@ -7364,7 +7428,7 @@ export default class EntryAbility extends UIAbility {
}); });
} catch (exception) { } catch (exception) {
console.error('Failed to load the content. Cause:' + JSON.stringify(exception)); console.error('Failed to load the content. Cause:' + JSON.stringify(exception));
}; }
} }
}; };
``` ```
...@@ -7399,11 +7463,12 @@ on(eventType: 'windowStageEvent', callback: Callback&lt;WindowStageEventType&gt; ...@@ -7399,11 +7463,12 @@ on(eventType: 'windowStageEvent', callback: Callback&lt;WindowStageEventType&gt;
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
try { try {
windowStage.on('windowStageEvent', (data) => { windowStage.on('windowStageEvent', (data) => {
...@@ -7413,7 +7478,7 @@ export default class EntryAbility extends UIAbility { ...@@ -7413,7 +7478,7 @@ export default class EntryAbility extends UIAbility {
} catch (exception) { } catch (exception) {
console.error('Failed to enable the listener for window stage event changes. Cause:' + console.error('Failed to enable the listener for window stage event changes. Cause:' +
JSON.stringify(exception)); JSON.stringify(exception));
}; }
} }
}; };
``` ```
...@@ -7448,18 +7513,19 @@ off(eventType: 'windowStageEvent', callback?: Callback&lt;WindowStageEventType&g ...@@ -7448,18 +7513,19 @@ off(eventType: 'windowStageEvent', callback?: Callback&lt;WindowStageEventType&g
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
try { try {
windowStage.off('windowStageEvent'); windowStage.off('windowStageEvent');
} catch (exception) { } catch (exception) {
console.error('Failed to disable the listener for window stage event changes. Cause:' + console.error('Failed to disable the listener for window stage event changes. Cause:' +
JSON.stringify(exception)); JSON.stringify(exception));
}; }
} }
}; };
``` ```
...@@ -7489,11 +7555,12 @@ disableWindowDecor(): void ...@@ -7489,11 +7555,12 @@ disableWindowDecor(): void
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
console.log('disableWindowDecor'); console.log('disableWindowDecor');
windowStage.disableWindowDecor(); windowStage.disableWindowDecor();
} }
...@@ -7531,17 +7598,18 @@ setShowOnLockScreen(showOnLockScreen: boolean): void ...@@ -7531,17 +7598,18 @@ setShowOnLockScreen(showOnLockScreen: boolean): void
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
// ... // ...
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate'); console.log('onWindowStageCreate');
try { try {
windowStage.setShowOnLockScreen(true); windowStage.setShowOnLockScreen(true);
} catch (exception) { } catch (exception) {
console.error('Failed to show on lockscreen. Cause:' + JSON.stringify(exception)); console.error('Failed to show on lockscreen. Cause:' + JSON.stringify(exception));
}; }
} }
}; };
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册