From d914531b7903423f17a956389b5995383980eb22 Mon Sep 17 00:00:00 2001 From: liujiahui Date: Wed, 7 May 2025 16:59:49 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=BA=9F=E5=BC=83=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/EditFile.ets | 16 ++++++++-------- entry/src/main/ets/pages/Index.ets | 6 +++--- entry/src/main/ets/pages/ViewMedia.ets | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/entry/src/main/ets/pages/EditFile.ets b/entry/src/main/ets/pages/EditFile.ets index 9f87fe4..37ca6b9 100644 --- a/entry/src/main/ets/pages/EditFile.ets +++ b/entry/src/main/ets/pages/EditFile.ets @@ -43,7 +43,7 @@ struct EditFile { @StorageLink('editable') editable: Boolean = false; @StorageLink('myFileSize') myFileSize: number = 0; @StorageLink('myFileContent') myFileContent: string = ''; - @State myContext: Context = getContext(this) as common.UIAbilityContext; + @State myContext: Context = this.getUIContext().getHostContext() as common.UIAbilityContext; @State myUri: string = ''; @State opacityValue: number = OPACITY_VALUE; @State uriSave: string = ''; @@ -62,8 +62,8 @@ struct EditFile { this.myUri = this.loadUri; Logger.info(TAG, 'The count of getFileInfo is myFileContent ' + this.myFileContent); } else { - this.myUri = (router.getParams() as myParams).myUri; - this.myFileName = (router.getParams() as myParams).fileName; + this.myUri = (this.getUIContext().getRouter().getParams() as myParams).myUri; + this.myFileName = (this.getUIContext().getRouter().getParams() as myParams).fileName; this.myFileContent = this.mediaFileUri.readFileContent(this.myUri); this.myFileSize = this.mediaFileUri.myGetFileSize(this.myUri, fileIo.OpenMode.READ_ONLY); Logger.info(TAG, 'The count of getFileInfo is myFileName is: ' + this.myFileName); @@ -125,10 +125,10 @@ struct EditFile { .onClick(() => { if (this.loadFlag) { Logger.info(TAG, 'end page'); - let context = getContext(this); + let context = this.getUIContext().getHostContext(); terminateSelf(context); } else { - router.back(); + this.getUIContext().getRouter().back(); } }) } @@ -206,7 +206,7 @@ struct EditFile { AppStorage.setOrCreate('editable', this.editable); Logger.info(TAG, 'EditFile caretPosition length = ' + this.myFileContent.length); this.controller.caretPosition(this.myFileContent.length); - promptAction.showToast({ message: $r('app.string.editable') }); + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.editable') }); }) Image($r('app.media.ic_save')) @@ -248,7 +248,7 @@ struct EditFile { flage = false; Logger.info(`save data to file failed with error: ${JSON.stringify(err)}: ${JSON.stringify(err?.message)}`); - promptAction.showToast({ + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.show_toast_message'), duration: 6500 }) @@ -257,7 +257,7 @@ struct EditFile { if (flage) { this.editable = false; AppStorage.setOrCreate('editable', this.editable); - promptAction.showToast({ message: $r('app.string.saved') }); + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.saved') }); } } }) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 7663e78..e1135d4 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -90,7 +90,7 @@ struct Index { } async getFilenameByUriForMedia(myUris: string[]) { - router.pushUrl({ + this.getUIContext().getRouter().pushUrl({ url: 'pages/ViewMedia', params: { uris: myUris @@ -100,7 +100,7 @@ struct Index { async getFilenameByUri(myUri: string): Promise { this.filename = (myUri.split('/').pop()) as string; - router.pushUrl({ + this.getUIContext().getRouter().pushUrl({ url: 'pages/EditFile', params: { fileName: this.filename, @@ -288,7 +288,7 @@ struct Index { .onClick(() => { Logger.info(TAG, 'fileAsset.displayName fileName item: ' + item); if (index !== undefined) { - router.pushUrl({ + this.getUIContext().getRouter().pushUrl({ url: 'pages/EditFile', params: { fileName: item, diff --git a/entry/src/main/ets/pages/ViewMedia.ets b/entry/src/main/ets/pages/ViewMedia.ets index d176d5e..d1c75cc 100644 --- a/entry/src/main/ets/pages/ViewMedia.ets +++ b/entry/src/main/ets/pages/ViewMedia.ets @@ -31,13 +31,13 @@ interface myParams extends Object { @Entry @Component struct ViewMedia { - @State myContext: Context = getContext(this) as common.UIAbilityContext; + @State myContext: Context = this.getUIContext().getHostContext() as common.UIAbilityContext; @State myFileSizes: number[] = []; @State myFileNames: string[] = []; @State myFileTypes: number[] = []; @StorageLink('myFileName') myFileName: string = ''; @StorageLink('myFileSize') myFileSize: number = 0; - @State myUris: string[] = (router.getParams() as myParams).uris; + @State myUris: string[] = (this.getUIContext().getRouter().getParams() as myParams).uris; @State uri: string = 'Hello World'; @StorageLink('showPauses') showPauses: Array = []; mediaFileUri: MediaFileUri = new MediaFileUri(); @@ -86,7 +86,7 @@ struct ViewMedia { .align(Alignment.Start) .id('back2Index') .onClick(() => { - router.back(); + this.getUIContext().getRouter().back(); }) } .width(Constants.BACK_WIDTH) -- Gitee From d6c600cbc372f3520310d0990c2549742a86661f Mon Sep 17 00:00:00 2001 From: liujiahui Date: Thu, 8 May 2025 10:52:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=BA=9F=E5=BC=83=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=9B=BF=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 | 3 +++ entry/src/main/ets/pages/EditFile.ets | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 4dc3e18..f131619 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -98,6 +98,9 @@ export default class EntryAbility extends UIAbility { return; } Logger.info(TAG, 'Succeeded in loading the content. Data: ' + JSON.stringify(data) ?? ''); + + let uiContext: UIContext | undefined = windowStage.getMainWindowSync().getUIContext() + AppStorage.setOrCreate('uiContext', uiContext); }); } diff --git a/entry/src/main/ets/pages/EditFile.ets b/entry/src/main/ets/pages/EditFile.ets index 37ca6b9..d0d2a53 100644 --- a/entry/src/main/ets/pages/EditFile.ets +++ b/entry/src/main/ets/pages/EditFile.ets @@ -21,9 +21,9 @@ import MediaFileUri from '../media/MediaFileUri'; import Logger from '../common/Logger'; import { terminateSelf } from '../utils/utils'; import { Constants } from '../common/Constants'; - +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); const TAG = 'EditFile: '; -let storage = LocalStorage.getShared(); +let storage = uiContext!.getSharedLocalStorage(); const OPACITY_VALUE = 0.6; interface myParams extends Object { -- Gitee