onWindowStageCreate(windowStage: window.WindowStage): void {
AppStorage.setOrCreate('windowStage', windowStage);
// Main window is created, set main page for this ability
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
} import { display, UIContext, window } from '@kit.ArkUI';
export function func(value: string, value2: string) {
let uiContext: UIContext = new UIContext;
let screenWidth: number = 0;
let screenHeight: number = 0;
let storage: LocalStorage = new LocalStorage();
let newValue: string = value;
let newValue2: string = value2;
storage.setOrCreate('storageSimpleProp', newValue);
storage.setOrCreate('storageSimpleProp2', newValue2);
let windowStage = AppStorage.get('windowStage') as window.WindowStage;
windowStage.createSubWindow('SubWindow', (err, windowClass) => {
// 获取屏幕宽高以设置子窗口坐标
let displayClass = display.getPrimaryDisplaySync();
screenWidth = displayClass.width;
screenHeight = displayClass.height;
if (err.code > 0) {
console.error(`failed to create subWindow Cause: ${err.message}`);
return;
}
try {
// 设置子窗口加载页
windowClass.loadContentByName('page1', storage);
// 设置子窗口左上角坐标
windowClass.moveWindowTo((screenWidth - uiContext.vp2px(300)) / 2, (screenHeight - uiContext.vp2px(300)) / 2);
// 设置子窗口大小
windowClass.resize(uiContext.vp2px(300), uiContext.vp2px(300));
// 设置子窗口圆角
windowClass.setWindowCornerRadius(16);
// 展示子窗口
windowClass.showWindow();
} catch (err) {
console.error(`failed to create subWindow Cause:${err}`);
}
});
return 'har func';
} import { window } from '@kit.ArkUI';
@Entry({ routeName: 'page1', useSharedStorage: true })
@Component
export struct Page1 {
@LocalStorageLink('storageSimpleProp') storageSimpleProp: string = '';
@LocalStorageLink('storageSimpleProp2') storageSimpleProp2: string = '';
build() {
Column() {
Text('HAR页面')
.width('100%')
.padding({ top: 8, bottom: 8, left: 16 });
Column({ space: 16 }) {
Text(this.storageSimpleProp);
Button('关闭子窗口')
.onClick(() => {
let windowClass: window.Window | undefined = undefined;
try {
windowClass = window.findWindow('SubWindow');
windowClass.destroyWindow((err) => {
const errCode: number = err.code;
if (errCode) {
console.error(`Failed to destroy the window. Cause code: ${err.code}, message: ${err.message}`);
return;
}
console.info('Succeeded in destroying the window.');
});
} catch (exception) {
console.error(`Failed to find the Window. Cause code: ${exception.code}, message: ${exception.message}`);
}
});
Image($r(this.storageSimpleProp2))
.width(100)
.height(100);
}
.justifyContent(FlexAlign.Center)
.borderRadius(16)
.backgroundColor('#E5E5EA')
.height('100%')
.width('100%');
}
.backgroundColor('#F1F3F5')
.height('100%')
.width('100%');
}
} export { Page1 } from './src/main/ets/components/MainPage';
export { func } from './src/main/ets/func'; {
"name": "entry",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
"hara": "file:../hara"
}
} import { func } from 'hara';
import 'hara/src/main/ets/components/MainPage';
@Entry
@Component
struct Index {
build() {
Column() {
Text('Index页面')
.fontSize(18);
Blank()
.height(16);
Button('打开子窗口')
.onClick(() => {
// 调用HAR模块方法
func('来自Index', 'app.media.startIcon');
});
}
.justifyContent(FlexAlign.Center)
.width('100%')
.height('100%');
}
}
本文中使用的HAP模块指的是创建项目时的entry模块。