From 4481a42f9db3680117933641af4235adfcad45af Mon Sep 17 00:00:00 2001 From: ly Date: Tue, 6 May 2025 13:48:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BA=9F=E5=BC=83API=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/common/utils/KVStore.ets | 11 ++++++++--- entry/src/main/ets/pages/Index.ets | 4 +++- entry/src/main/ets/pages/ReadWritePage.ets | 4 +++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/entry/src/main/ets/common/utils/KVStore.ets b/entry/src/main/ets/common/utils/KVStore.ets index 42e25aa..320f003 100644 --- a/entry/src/main/ets/common/utils/KVStore.ets +++ b/entry/src/main/ets/common/utils/KVStore.ets @@ -13,23 +13,28 @@ * limitations under the License. */ +import { common } from '@kit.AbilityKit'; import { distributedKVStore } from '@kit.ArkData'; import { bundleManager } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { KVStoreType } from '../../viewmodel/KVStoreType'; +let UIContext: UIContext + export default class KVStore { private kvManager: distributedKVStore.KVManager | undefined = undefined; - private context = getContext(this); private kvStore: distributedKVStore.SingleKVStore | undefined = undefined; private bundleName: string = ''; - constructor(encrypt: boolean, securityLevel: distributedKVStore.SecurityLevel) { + constructor(encrypt: boolean, securityLevel: distributedKVStore.SecurityLevel, UIContext: UIContext) { let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT; + UIContext = UIContext + let hContext = UIContext.getHostContext() as common.UIAbilityContext; + bundleManager.getBundleInfoForSelf(bundleFlags, (err, data) => { this.bundleName = data.name; const kvManagerConfig: distributedKVStore.KVManagerConfig = { - context: this.context, + context: hContext, bundleName: this.bundleName }; this.kvManager = distributedKVStore.createKVManager(kvManagerConfig); diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 8854973..e6843cb 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -29,6 +29,8 @@ struct Index { @StorageLink('kvStore') kvStore: KVStore | undefined = undefined; @StorageLink('kvStoreAll') kvStoreAll: KVStoreType[] = []; + UIContext = this.getUIContext(); + @Builder pageMap(name: string) { if(name === 'readWritePage'){ @@ -132,7 +134,7 @@ struct Index { .onClick(async () => { this.kvStoreAll = []; this.kvStore = undefined; - this.kvStore = new KVStore(this.encrypted, this.securityLevel); + this.kvStore = new KVStore(this.encrypted, this.securityLevel, this.UIContext); setTimeout(() => { this.pageInfos.pushPath(new NavPathInfo('readWritePage', [])); }, 200) diff --git a/entry/src/main/ets/pages/ReadWritePage.ets b/entry/src/main/ets/pages/ReadWritePage.ets index 2a8a014..87af8df 100644 --- a/entry/src/main/ets/pages/ReadWritePage.ets +++ b/entry/src/main/ets/pages/ReadWritePage.ets @@ -49,6 +49,8 @@ export struct ReadWrite { private inputKey: string = ''; private inputValue: Uint8Array | string | number | boolean = ''; + UIContext = this.getUIContext(); + @Builder menuBuilder(kvStore: KVStoreType) { Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { @@ -417,7 +419,7 @@ export struct ReadWrite { } .width('100%') .height('100%') - .title(getContext(this).resourceManager.getStringSync($r('app.string.destination_title'))) + .title(this.UIContext.getHostContext()?.resourceManager.getStringSync($r('app.string.destination_title'))) .backgroundColor('#F1F3F5') } } \ No newline at end of file -- Gitee