From e481c53b33cdc80826bae9b568472a51bff9356b Mon Sep 17 00:00:00 2001 From: WX1377357 Date: Thu, 5 Jun 2025 10:01:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BA=9F=E5=BC=83api=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=B8=ADuiContext=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/common/constants/StyleConstants.ets | 5 +++-- entry/src/main/ets/entryability/EntryAbility.ts | 1 - entry/src/main/ets/pages/CanvasPage.ets | 9 ++------- entry/src/main/ets/viewmodel/DrawModel.ets | 8 ++++---- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/entry/src/main/ets/common/constants/StyleConstants.ets b/entry/src/main/ets/common/constants/StyleConstants.ets index 13059b6..9b25ce8 100644 --- a/entry/src/main/ets/common/constants/StyleConstants.ets +++ b/entry/src/main/ets/common/constants/StyleConstants.ets @@ -17,7 +17,6 @@ * Style constants for all Component. */ -const uiContext: UIContext | undefined = AppStorage.get('uiContext'); export default class StyleConstants { /** @@ -48,5 +47,7 @@ export default class StyleConstants { /** * Arc text size. */ - static readonly ARC_TEXT_SIZE: number = uiContext!.fp2px(14); + static getARCTextSize(uiContext: UIContext): number { + return uiContext.fp2px(14) + } } \ No newline at end of file diff --git a/entry/src/main/ets/entryability/EntryAbility.ts b/entry/src/main/ets/entryability/EntryAbility.ts index abea97b..1a37a35 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ts +++ b/entry/src/main/ets/entryability/EntryAbility.ts @@ -37,7 +37,6 @@ export default class EntryAbility extends UIAbility { return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); - AppStorage.setOrCreate('uiContext', windowStage.getMainWindowSync().getUIContext()); }); } diff --git a/entry/src/main/ets/pages/CanvasPage.ets b/entry/src/main/ets/pages/CanvasPage.ets index 3f53eb5..afea0f1 100644 --- a/entry/src/main/ets/pages/CanvasPage.ets +++ b/entry/src/main/ets/pages/CanvasPage.ets @@ -20,11 +20,6 @@ import PrizeDialog from '../view/PrizeDialog'; import PrizeData from '../viewmodel/PrizeData'; import StyleConstants from '../common/constants/StyleConstants'; import CommonConstants from '../common/constants/CommonConstants'; -import { Context } from '@kit.AbilityKit'; - -// Get context. -const uiContext: UIContext | undefined = AppStorage.get('uiContext'); -let context: Context = uiContext!.getHostContext()!; @Entry @Component @@ -48,7 +43,7 @@ struct CanvasPage { aboutToAppear() { // Obtains the width and height of the screen, excluding the height of the navigation view. - window.getLastWindow(context) + window.getLastWindow(this.getUIContext().getHostContext()) .then((windowClass: window.Window) => { windowClass.setWindowLayoutFullScreen(true); let windowProperties = windowClass.getWindowProperties(); @@ -66,7 +61,7 @@ struct CanvasPage { .width(StyleConstants.FULL_PERCENT) .height(StyleConstants.FULL_PERCENT) .onReady(() => { - this.drawModel.draw(this.canvasContext, this.screenWidth, this.screenHeight); + this.drawModel.draw(this.canvasContext, this.screenWidth, this.screenHeight,this.getUIContext()); }) .rotate({ x: 0, diff --git a/entry/src/main/ets/viewmodel/DrawModel.ets b/entry/src/main/ets/viewmodel/DrawModel.ets index 9da3e46..9df0432 100644 --- a/entry/src/main/ets/viewmodel/DrawModel.ets +++ b/entry/src/main/ets/viewmodel/DrawModel.ets @@ -39,7 +39,7 @@ export default class DrawModel { * @param screenWidth screenWidth. * @param screenHeight screenHeight. */ - draw(canvasContext: CanvasRenderingContext2D, screenWidth: number, screenHeight: number) { + draw(canvasContext: CanvasRenderingContext2D, screenWidth: number, screenHeight: number, uiContext:UIContext) { if (CheckEmptyUtils.isEmptyObj(canvasContext)) { Logger.error('[DrawModel][draw] canvasContext is empty.'); return; @@ -59,7 +59,7 @@ export default class DrawModel { // Draw the interior fan-shaped raffle area. this.drawInnerArc(); // Draw text in the internal fan area. - this.drawArcText(); + this.drawArcText(uiContext); // Draw the picture corresponding to the prize in the internal fan area. this.drawImage(); this.canvasContext.translate(-this.screenWidth / CommonConstants.TWO, @@ -160,12 +160,12 @@ export default class DrawModel { /** * Draw text in the internal fan area. */ - drawArcText() { + drawArcText(uiContext:UIContext) { if (this.canvasContext !== undefined) { this.canvasContext.textAlign = CommonConstants.TEXT_ALIGN; this.canvasContext.textBaseline = CommonConstants.TEXT_BASE_LINE; this.canvasContext.fillStyle = ColorConstants.TEXT_COLOR; - this.canvasContext.font = StyleConstants.ARC_TEXT_SIZE + CommonConstants.CANVAS_FONT; + this.canvasContext.font = StyleConstants.getARCTextSize(uiContext) + CommonConstants.CANVAS_FONT; } let textArrays = [ $r('app.string.text_smile'), -- Gitee