未验证 提交 fee7b202 编写于 作者: O openharmony_ci 提交者: Gitee

!23746 窗口ts示例代码 arkts 告警修复

Merge pull request !23746 from lixia/master
......@@ -41,12 +41,13 @@ onConnect(want: Want): void
**示例:**
```ts
export default class MyWindowExtensionAbility extends WindowExtensionAbility {
import WindowExtensionAbility from '@ohos.application.WindowExtensionAbility';
import Want from '@ohos.app.ability.Want';
onConnect(want) {
export default class MyWindowExtensionAbility extends WindowExtensionAbility {
onConnect(want: Want) {
console.info('WindowExtAbility onConnect, abilityName: ${want.abilityName}');
}
}
```
......@@ -68,12 +69,13 @@ onDisconnect(want: Want): void
**示例:**
```ts
export default class MyWindowExtensionAbility extends WindowExtensionAbility {
import WindowExtensionAbility from '@ohos.application.WindowExtensionAbility';
import Want from '@ohos.app.ability.Want';
onDisconnect(want) {
export default class MyWindowExtensionAbility extends WindowExtensionAbility {
onDisconnect(want: Want) {
console.info('WindowExtAbility onDisconnect, abilityName: ${want.abilityName}');
}
}
```
......@@ -95,16 +97,20 @@ onWindowReady(window: window.Window): void
**示例:**
```ts
import WindowExtensionAbility from '@ohos.application.WindowExtensionAbility';
import window from '@ohos.window';
let windowStage: window.WindowStage | null = null
export default class MyWindowExtensionAbility extends WindowExtensionAbility {
onWindowReady(windowClass: window.Window) {
onWindowReady(window) {
window.loadContent('WindowExtAbility/pages/index1').then(() => {
window.getProperties().then((pro) => {
windowStage!.loadContent('WindowExtAbility/pages/index1').then(() => {
windowClass.getProperties().then((pro: window.WindowProperties) => {
console.log('WindowExtension pro: ${JSON.stringify(pro)}');
});
window.show();
windowClass.show();
});
}
}
```
\ No newline at end of file
......@@ -2652,27 +2652,33 @@ getUIContext(): UIContext
```ts
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 {
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
// 为主窗口加载对应的目标页面。
windowStage.loadContent("pages/page2", (err) => {
if (err.code) {
windowStage.loadContent("pages/page2", (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in loading the content.');
// 获取应用主窗口。
let windowClass = null;
windowStage.getMainWindow((err, data) => {
if (err.code) {
let windowClass: window.Window | null = null;
windowStage.getMainWindow((err: BusinessError, data) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
return;
}
windowClass = data;
console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
// 获取UIContext实例。
globalThis.uiContext = windowClass.getUIContext();
let uiContext: UIContext | null = null;
uiContext = windowClass.getUIContext();
})
});
}
......@@ -2793,20 +2799,37 @@ loadContent(path: string, storage: LocalStorage, callback: AsyncCallback<void
**示例:**
```ts
let storage = new LocalStorage();
storage.setOrCreate('storageSimpleProp',121);
console.log('onWindowStageCreate');
try {
windowClass.loadContent('pages/page2', storage, (err) => {
if (err.code) {
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
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 {
(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));
return;
}
console.info('Succeeded in loading the content.');
});
} catch (exception) {
}
} catch (exception) {
console.error('Failed to load the content. Cause:' + JSON.stringify(exception));
}
}
}
};
```
### loadContent<sup>9+</sup>
......@@ -2844,19 +2867,35 @@ loadContent(path: string, storage: LocalStorage): Promise&lt;void&gt;
**示例:**
```ts
let storage = new LocalStorage();
storage.setOrCreate('storageSimpleProp',121);
console.log('onWindowStageCreate');
try {
let promise = windowClass.loadContent('pages/page2', storage);
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
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(() => {
console.info('Succeeded in loading the content.');
}).catch((err) => {
}).catch((err: BusinessError) => {
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));
}
}
}
};
```
### isWindowShowing<sup>9+</sup>
......@@ -6893,15 +6932,18 @@ getMainWindow(callback: AsyncCallback&lt;Window&gt;): void
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
let windowClass = null;
windowStage.getMainWindow((err, data) => {
if (err.code) {
let windowClass: window.Window | null = null;
windowStage.getMainWindow((err: BusinessError, data) => {
const errCode: number = err.code;
if (errCode) {
console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
return;
}
......@@ -6941,18 +6983,20 @@ getMainWindow(): Promise&lt;Window&gt;
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
let windowClass = null;
let windowClass: window.Window | null = null;
let promise = windowStage.getMainWindow();
promise.then((data) => {
windowClass = 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));
});
}
......@@ -6988,17 +7032,18 @@ getMainWindowSync(): Window
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
try {
let windowClass = windowStage.getMainWindowSync();
} catch (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
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
let windowClass = null;
let windowClass: window.Window | null = null;
try {
windowStage.createSubWindow('mySubWindow', (err, data) => {
if (err.code) {
windowStage.createSubWindow('mySubWindow', (err: BusinessError, data) => {
const errCode: number = err.code;
if (errCode) {
console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err));
return;
}
......@@ -7052,7 +7100,7 @@ export default class EntryAbility extends UIAbility {
});
} catch (exception) {
console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(exception));
};
}
}
};
```
......@@ -7091,24 +7139,26 @@ createSubWindow(name: string): Promise&lt;Window&gt;
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
let windowClass = null;
let windowClass: window.Window | null = null;
try {
let promise = windowStage.createSubWindow('mySubWindow');
promise.then((data) => {
windowClass = 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));
});
} catch (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
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
let windowClass = null;
windowStage.getSubWindow((err, data) => {
if (err.code) {
let windowClass: window.Window[] = [];
windowStage.getSubWindow((err: BusinessError, data) => {
const errCode: number = err.code;
if (errCode) {
console.error('Failed to obtain the subwindow. Cause: ' + JSON.stringify(err));
return;
}
......@@ -7187,18 +7240,20 @@ getSubWindow(): Promise&lt;Array&lt;Window&gt;&gt;
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
let windowClass = null;
let windowClass: window.Window[] = [];
let promise = windowStage.getSubWindow();
promise.then((data) => {
windowClass = 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));
})
}
......@@ -7235,18 +7290,21 @@ loadContent(path: string, storage: LocalStorage, callback: AsyncCallback&lt;void
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility {
// ...
storage : LocalStorage
onWindowStageCreate(windowStage) {
this.storage = new LocalStorage();
this.storage.setOrCreate('storageSimpleProp',121);
storage: LocalStorage = new LocalStorage();
onWindowStageCreate(windowStage: window.WindowStage) {
this.storage.setOrCreate('storageSimpleProp', 121);
console.log('onWindowStageCreate');
try {
windowStage.loadContent('pages/page2',this.storage,(err) => {
if (err.code) {
windowStage.loadContent('pages/page2', this.storage, (err: BusinessError) => {
const errCode: number = err.code;
if (errCode) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return;
}
......@@ -7254,7 +7312,7 @@ export default class EntryAbility extends UIAbility {
});
} catch (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;
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility {
// ...
storage : LocalStorage
onWindowStageCreate(windowStage) {
this.storage = new LocalStorage();
this.storage.setOrCreate('storageSimpleProp',121);
storage: LocalStorage = new LocalStorage();
onWindowStageCreate(windowStage: window.WindowStage) {
this.storage.setOrCreate('storageSimpleProp', 121);
console.log('onWindowStageCreate');
try {
let promise = windowStage.loadContent('pages/page2',this.storage);
let promise = windowStage.loadContent('pages/page2', this.storage);
promise.then(() => {
console.info('Succeeded in loading the content.');
}).catch((err) => {
}).catch((err: BusinessError) => {
console.error('Failed to load the content. Cause:' + JSON.stringify(err));
});
} catch (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
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
try {
windowStage.loadContent('pages/page2', (err) => {
if (err.code) {
windowStage.loadContent('pages/page2', (err: BusinessError) => {
const errCode: number = err.code;
if (errCode) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return;
}
......@@ -7364,7 +7428,7 @@ export default class EntryAbility extends UIAbility {
});
} catch (exception) {
console.error('Failed to load the content. Cause:' + JSON.stringify(exception));
};
}
}
};
```
......@@ -7399,11 +7463,12 @@ on(eventType: 'windowStageEvent', callback: Callback&lt;WindowStageEventType&gt;
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
try {
windowStage.on('windowStageEvent', (data) => {
......@@ -7413,7 +7478,7 @@ export default class EntryAbility extends UIAbility {
} catch (exception) {
console.error('Failed to enable the listener for window stage event changes. Cause:' +
JSON.stringify(exception));
};
}
}
};
```
......@@ -7448,18 +7513,19 @@ off(eventType: 'windowStageEvent', callback?: Callback&lt;WindowStageEventType&g
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
try {
windowStage.off('windowStageEvent');
} catch (exception) {
console.error('Failed to disable the listener for window stage event changes. Cause:' +
JSON.stringify(exception));
};
}
}
};
```
......@@ -7489,11 +7555,12 @@ disableWindowDecor(): void
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('disableWindowDecor');
windowStage.disableWindowDecor();
}
......@@ -7531,17 +7598,18 @@ setShowOnLockScreen(showOnLockScreen: boolean): void
```ts
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage: window.WindowStage) {
console.log('onWindowStageCreate');
try {
windowStage.setShowOnLockScreen(true);
} catch (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.
先完成此消息的编辑!
想要评论请 注册