想在UIAbilitiy中配置自己的页面,一直不能显示,折腾了一下,终于显示出来,记录下
可以在UIAbility的onWindowStageCreate()生命周期回调中,通过WindowStage对象的loadContent()方法设置启动页面。
默认的页面是页面是pages/Index,
import UIAbility from '@ohos.app.ability.UIAbility';
import Window from '@ohos.window';
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: Window.WindowStage) {
// Main window is created, set main page for this ability
windowStage.loadContent('pages/Index', (err, data) => {
// ...
});
}
// ...
}
如果想想换成其它的页面,如在pages下面新建 read目录,在read目录创建新的页面Hello.ets,test.etc
![](https://img.haomeiwen.com/i5241373/14534e6a10e34798.png)
需要在 资源里添加配置
![](https://img.haomeiwen.com/i5241373/8329d16e93ccbf99.png)
在 loadCotent修改配置即可
onWindowStageCreate(windowStage: window.WindowStage) {
console.info(TAG, "onWindowStageCreate");
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/read/Hello', (err, data) => {
if (err.code) {
console.info('testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
console.info('testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
});
}
网友评论