From 7766373b61afdca40be66e65ebf0e5827f6a39d2 Mon Sep 17 00:00:00 2001 From: zhangxCode Date: Fri, 13 Jun 2025 17:35:53 +0800 Subject: [PATCH] =?UTF-8?q?FAQ=E5=BA=9F=E5=BC=83API=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entry/src/main/ets/pages/StartAbility.ets | 43 +++++++++++++++---- .../EntryAbilityImmersiveStatusBar.ets | 18 ++------ .../EntryAbilityImmersiveWindow.ets | 21 ++------- .../ChangeTheFontColorOfTheStatusBar.ets | 7 +-- .../src/main/ets/pages/FontResources.ets | 2 +- .../ImplementPageSwitchingAnimation_One.ets | 4 +- .../ImplementPageSwitchingAnimation_Two.ets | 4 +- .../main/ets/pages/SaveWebPictureToAlbum.ets | 4 +- 8 files changed, 50 insertions(+), 53 deletions(-) diff --git a/AbilityKit/entry/src/main/ets/pages/StartAbility.ets b/AbilityKit/entry/src/main/ets/pages/StartAbility.ets index 8eebd91..e213a72 100644 --- a/AbilityKit/entry/src/main/ets/pages/StartAbility.ets +++ b/AbilityKit/entry/src/main/ets/pages/StartAbility.ets @@ -33,12 +33,37 @@ let wantInfo: Want = { } // [End request_want] -// [Start start_ability] -// Call the startAbility interface to open files -let context = getContext(this) as common.UIAbilityContext; -context.startAbility(wantInfo).then(() => { - // ... -}).catch((err: BusinessError) => { - // ... -}) -// [End start_ability] \ No newline at end of file +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + aboutToAppear(): void { + // [Start start_ability] + // Call the startAbility interface to open files + let context = this.getUIContext().getHostContext() as common.UIAbilityContext; + context.startAbility(wantInfo).then(() => { + // ... + }).catch((err: BusinessError) => { + // ... + }) + // [End start_ability] + } + + build() { + RelativeContainer() { + Text(this.message) + .id('HelloWorld') + .fontSize($r('app.float.page_text_font_size')) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }) + .onClick(() => { + this.message = 'Welcome'; + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/ArkUI/entry/src/main/ets/entryability/EntryAbilityImmersiveStatusBar.ets b/ArkUI/entry/src/main/ets/entryability/EntryAbilityImmersiveStatusBar.ets index ebfed50..dac0026 100644 --- a/ArkUI/entry/src/main/ets/entryability/EntryAbilityImmersiveStatusBar.ets +++ b/ArkUI/entry/src/main/ets/entryability/EntryAbilityImmersiveStatusBar.ets @@ -38,20 +38,12 @@ export default class EntryAbility extends UIAbility { // 2.Realize immersive effects. Method 1: Set the navigation bar and status bar to not display. let names = []; - windowClass.setWindowSystemBarEnable(names, (err) => { - if (err.code) { - console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err)); - return; - } + windowClass.setWindowSystemBarEnable(names).then(() => { console.info('Succeeded in setting the system bar to be visible.'); }); // 2.Realize immersive effects. Method 2: Set the window to a full screen layout, and coordinate with the transparency, background/text color, and highlighted icons of the navigation bar and status bar to maintain consistency with the main window display. let isLayoutFullScreen = true; - windowClass.setWindowLayoutFullScreen(isLayoutFullScreen, (err) => { - if (err.code) { - console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err)); - return; - } + windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(() => { console.info('Succeeded in setting the window layout to full-screen mode.'); }); let sysBarProps: window.SystemBarProperties = { @@ -61,11 +53,7 @@ export default class EntryAbility extends UIAbility { statusBarContentColor: '#ffffff', navigationBarContentColor: '#ffffff' }; - windowClass.setWindowSystemBarProperties(sysBarProps, (err) => { - if (err.code) { - console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err)); - return; - } + windowClass.setWindowSystemBarProperties(sysBarProps).then(() => { console.info('Succeeded in setting the system bar properties.'); }); }) diff --git a/ArkUI/entry/src/main/ets/entryability/EntryAbilityImmersiveWindow.ets b/ArkUI/entry/src/main/ets/entryability/EntryAbilityImmersiveWindow.ets index 4636d2e..401abe1 100644 --- a/ArkUI/entry/src/main/ets/entryability/EntryAbilityImmersiveWindow.ets +++ b/ArkUI/entry/src/main/ets/entryability/EntryAbilityImmersiveWindow.ets @@ -37,22 +37,12 @@ export default class EntryAbility extends UIAbility { // 2.Realize immersive effects. Method 1: Set the navigation bar and status bar to not display. let names: Array<'status' | 'navigation'> = []; - windowClass.setWindowSystemBarEnable(names, (err: BusinessError) => { - let errCode: number = err.code; - if (errCode) { - console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err)); - return; - } + windowClass.setWindowSystemBarEnable(names).then(() => { console.info('Succeeded in setting the system bar to be visible.'); }); // 2.Realize immersive effects. Method 2: Set the window to a full screen layout, and coordinate with the transparency, background/text color, and highlighted icons of the navigation bar and status bar to maintain consistency with the main window display. let isLayoutFullScreen = true; - windowClass.setWindowLayoutFullScreen(isLayoutFullScreen, (err: BusinessError) => { - let errCode: number = err.code; - if (errCode) { - console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err)); - return; - } + windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(() => { console.info('Succeeded in setting the window layout to full-screen mode.'); }); let sysBarProps: window.SystemBarProperties = { @@ -61,12 +51,7 @@ export default class EntryAbility extends UIAbility { statusBarContentColor: '#ffffff', navigationBarContentColor: '#ffffff' }; - windowClass.setWindowSystemBarProperties(sysBarProps, (err: BusinessError) => { - let errCode: number = err.code; - if (errCode) { - console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err)); - return; - } + windowClass.setWindowSystemBarProperties(sysBarProps).then(() => { console.info('Succeeded in setting the system bar properties.'); }); }) diff --git a/ArkUI/entry/src/main/ets/pages/ChangeTheFontColorOfTheStatusBar.ets b/ArkUI/entry/src/main/ets/pages/ChangeTheFontColorOfTheStatusBar.ets index 9e7393b..e61693b 100644 --- a/ArkUI/entry/src/main/ets/pages/ChangeTheFontColorOfTheStatusBar.ets +++ b/ArkUI/entry/src/main/ets/pages/ChangeTheFontColorOfTheStatusBar.ets @@ -32,12 +32,7 @@ struct ChangeStatusBar { statusBarColor: '#000000', statusBarContentColor: '#ffffff' }; - this.mainWin.setWindowSystemBarProperties(sysBarProps, (err: BusinessError) => { - let errCode: number = err.code; - if (errCode) { - console.error('[StaticUtils] Failed to set the system bar properties. Cause: ' + JSON.stringify(err)); - return; - } + this.mainWin.setWindowSystemBarProperties(sysBarProps).then(() => { console.info('[StaticUtils] Succeeded in setting the system bar properties.'); }); } diff --git a/ArkUI/entry/src/main/ets/pages/FontResources.ets b/ArkUI/entry/src/main/ets/pages/FontResources.ets index 6db9295..69dd5d1 100644 --- a/ArkUI/entry/src/main/ets/pages/FontResources.ets +++ b/ArkUI/entry/src/main/ets/pages/FontResources.ets @@ -9,7 +9,7 @@ struct Index { @State message: string = 'Hello World'; aboutToAppear(): void { // [Start set_font] - font.registerFont({ + this.getUIContext().getFont().registerFont({ familyName: 'Gealova', familySrc: $rawfile('font/gealova.otf') }) diff --git a/ArkUI/entry/src/main/ets/pages/ImplementPageSwitchingAnimation_One.ets b/ArkUI/entry/src/main/ets/pages/ImplementPageSwitchingAnimation_One.ets index 6d8b626..a2298aa 100644 --- a/ArkUI/entry/src/main/ets/pages/ImplementPageSwitchingAnimation_One.ets +++ b/ArkUI/entry/src/main/ets/pages/ImplementPageSwitchingAnimation_One.ets @@ -22,9 +22,11 @@ @Entry @Component struct PageTransition1 { + pageInfos: NavPathStack = new NavPathStack(); + build() { Stack({ alignContent: Alignment.Bottom }) { - Navigator({ target: 'pages/Page1'}) { + Navigation(this.pageInfos) { Image($r('app.media.ic_banner01')).width('100%').height(200) // The image is stored in the media folder } }.height('100%').width('100%') diff --git a/ArkUI/entry/src/main/ets/pages/ImplementPageSwitchingAnimation_Two.ets b/ArkUI/entry/src/main/ets/pages/ImplementPageSwitchingAnimation_Two.ets index d051b0a..a1fd524 100644 --- a/ArkUI/entry/src/main/ets/pages/ImplementPageSwitchingAnimation_Two.ets +++ b/ArkUI/entry/src/main/ets/pages/ImplementPageSwitchingAnimation_Two.ets @@ -22,9 +22,11 @@ @Entry @Component struct PageTransition2 { + pageInfos: NavPathStack = new NavPathStack(); + build() { Stack({ alignContent: Alignment.Bottom }) { - Navigator({ target: 'pages/Index'}) { + Navigation(this.pageInfos) { Image($r('app.media.ic_banner02')).width('100%').height(200) // The image is stored in the media folder } }.height('100%').width('100%') diff --git a/ImageKit/entry/src/main/ets/pages/SaveWebPictureToAlbum.ets b/ImageKit/entry/src/main/ets/pages/SaveWebPictureToAlbum.ets index 4d40dee..1595444 100644 --- a/ImageKit/entry/src/main/ets/pages/SaveWebPictureToAlbum.ets +++ b/ImageKit/entry/src/main/ets/pages/SaveWebPictureToAlbum.ets @@ -78,7 +78,7 @@ struct SaveImage { let file = await fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); // 写入文件 await fs.write(file.fd, buffer); - this.getUIContext().promptAction.showToast({ message: '已保存至相册!' }); + this.getUIContext().getPromptAction().showToast({ message: '已保存至相册!' }); // 关闭文件 await fs.close(file.fd); } catch (error) { @@ -107,7 +107,7 @@ struct SaveImage { if (result === SaveButtonOnClickResult.SUCCESS) { this.loadImageWithUrl('https://agc-storage-drcn.platform.dbankcloud.cn/v0/test-rqcjj/test.png'); } else { - this.getUIContext().promptAction.showToast({ message: '设置权限失败!' }); + this.getUIContext().getPromptAction().showToast({ message: '设置权限失败!' }); } }) } -- Gitee