From 0248c2ff95479e5aa4693fab32c5cb2f9264b0b5 Mon Sep 17 00:00:00 2001 From: ly Date: Wed, 7 May 2025 09:09:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E5=BA=9F=E5=BC=83API=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/entryability/EntryAbility.ets | 27 +++++++++++-------- entry/src/main/ets/pages/ForEachListPage.ets | 6 ++++- .../main/ets/pages/LazyForEachListPage.ets | 6 ++++- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 5aab374..3114d99 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { common } from '@kit.AbilityKit'; import { UIAbility , Want ,AbilityConstant } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit' @@ -32,8 +33,13 @@ export default class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability Logger.info(TAG, 'EntryAbility onWindowStageCreate'); - requestFullScreen(windowStage, this.context); - windowStage.loadContent('pages/Index'); + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + return; + } + }); + let uiContext: UIContext = windowStage.getMainWindowSync().getUIContext(); + requestFullScreen(windowStage, uiContext); } onWindowStageDestroy(): void { @@ -51,12 +57,9 @@ export default class EntryAbility extends UIAbility { Logger.info(TAG, 'MainAbility onBackground'); } - - - } -function requestFullScreen(windowStage: window.WindowStage, context: Context): void { +function requestFullScreen(windowStage: window.WindowStage, UIContext: UIContext): void { windowStage.getMainWindow((err: BusinessError, data: window.Window) => { if (err.code) { Logger.error(TAG, 'Failed to obtain the main window. Cause: ' + JSON.stringify(err)); @@ -71,7 +74,7 @@ function requestFullScreen(windowStage: window.WindowStage, context: Context): v let type = window.AvoidAreaType.TYPE_SYSTEM; // Get status bar height. let area: window.AvoidArea = windowClass.getWindowAvoidArea(type); - getDeviceSize(context, area); + getDeviceSize(UIContext, area); let promise: Promise = windowClass.setWindowLayoutFullScreen(isLayoutFullScreen); promise.then(() => { windowStage.getMainWindowSync().setWindowBackgroundColor('#F1F3F5'); @@ -85,14 +88,16 @@ function requestFullScreen(windowStage: window.WindowStage, context: Context): v }); } -function getDeviceSize(context: Context, area: window.AvoidArea): void { +function getDeviceSize(UIContext: UIContext, area: window.AvoidArea): void { + let context = UIContext.getHostContext() as common.UIAbilityContext; + // Get device height. window.getLastWindow(context).then((data: window.Window) => { let properties = data.getWindowProperties(); if (area.topRect.height > 0) { - AppStorage.setOrCreate('statusBarHeight', px2vp(area.topRect.height)); + AppStorage.setOrCreate('statusBarHeight', UIContext.px2vp(area.topRect.height)); } - AppStorage.setOrCreate('deviceHeight', px2vp(properties.windowRect.height)); - AppStorage.setOrCreate('deviceWidth', px2vp(properties.windowRect.width)); + AppStorage.setOrCreate('deviceHeight', UIContext.px2vp(properties.windowRect.height)); + AppStorage.setOrCreate('deviceWidth', UIContext.px2vp(properties.windowRect.width)); }); } \ No newline at end of file diff --git a/entry/src/main/ets/pages/ForEachListPage.ets b/entry/src/main/ets/pages/ForEachListPage.ets index c4bc300..fcf2350 100644 --- a/entry/src/main/ets/pages/ForEachListPage.ets +++ b/entry/src/main/ets/pages/ForEachListPage.ets @@ -13,6 +13,7 @@ * limitations under the License. */ +import { common } from '@kit.AbilityKit'; import { LearningResource } from '../model/LearningResource'; import { ObservedArray } from '../utils/ObservedArray'; import { ArticleCardView } from '../components/ArticleCardView'; @@ -32,6 +33,9 @@ function bufferToString(buffer: ArrayBufferLike): string { @Entry @Component export struct ForEachListPage { + UIContext = this.getUIContext() + context = this.UIContext.getHostContext() as common.UIAbilityContext; + @State articleList: LearningResource[] = []; @State collectedIds: ObservedArray = ['1', '2', '3', '4', '5', '6']; @State likedIds: ObservedArray = ['1', '2', '3', '4', '5', '6']; @@ -46,7 +50,7 @@ export struct ForEachListPage { } getArticleModelObjFromJSON() { - getContext(this).resourceManager.getRawFileContent('article1000.json').then(value => { + this.context.resourceManager.getRawFileContent('article1000.json').then(value => { let jsonObj = JSON.parse(bufferToString(value.buffer)) as LearningResource[]; let resources: LearningResource[] = []; jsonObj.forEach((res: LearningResource) => { diff --git a/entry/src/main/ets/pages/LazyForEachListPage.ets b/entry/src/main/ets/pages/LazyForEachListPage.ets index 302fb59..82c2cc2 100644 --- a/entry/src/main/ets/pages/LazyForEachListPage.ets +++ b/entry/src/main/ets/pages/LazyForEachListPage.ets @@ -13,6 +13,7 @@ * limitations under the License. */ +import { common } from '@kit.AbilityKit'; import { LearningResource } from '../model/LearningResource'; import ArticleListData from '../model/ArticleListData'; import { ObservedArray } from '../utils/ObservedArray'; @@ -34,6 +35,9 @@ function bufferToString(buffer: ArrayBufferLike): string { @Entry @Component export struct LazyForEachListPage { + UIContext = this.getUIContext() + context = this.UIContext.getHostContext() as common.UIAbilityContext; + @State collectedIds: ObservedArray = ['1', '2', '3', '4', '5', '6']; @State likedIds: ObservedArray = ['1', '2', '3', '4', '5', '6']; @State isListReachEnd: boolean = false; @@ -48,7 +52,7 @@ export struct LazyForEachListPage { } getArticleModelObjFromJSON() { - getContext(this).resourceManager.getRawFileContent('article1000.json').then(value => { + this.context.resourceManager.getRawFileContent('article1000.json').then(value => { let jsonObj = JSON.parse(bufferToString(value.buffer)) as LearningResource[]; let resources: LearningResource[] = []; jsonObj.forEach((res: LearningResource) => { -- Gitee From 869a64a2065adaa312f3e15bb9117d02b1e6ddfc Mon Sep 17 00:00:00 2001 From: ly Date: Wed, 7 May 2025 09:28:21 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E5=BA=9F=E5=BC=83API=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/entryability/EntryAbility.ets | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 3114d99..7ce1ae2 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -38,8 +38,14 @@ export default class EntryAbility extends UIAbility { return; } }); - let uiContext: UIContext = windowStage.getMainWindowSync().getUIContext(); - requestFullScreen(windowStage, uiContext); + windowStage.getMainWindow((err, data) => { + if (err.code) { + return; + } + let windowClass: window.Window = data; + let uiContext: UIContext = windowClass.getUIContext(); + requestFullScreen(windowStage, uiContext); + }); } onWindowStageDestroy(): void { -- Gitee