diff --git a/entry/src/main/ets/common/utils/KVStore.ets b/entry/src/main/ets/common/utils/KVStore.ets index 42e25aafece38eff88563fb201bb9a4d4a37ddc0..320f003f13bb992b309d3c8c0622091dfe1da18a 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 8854973de23b08887f157a1d1fb0f44d7ebc4283..e6843cb31c0db0c2429b824aa1185b93e6dab2dd 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 2a8a014fd48ca7294009d3dc4a7c39d8b06e56d8..87af8df55dba607910eee4dac4f012c0bf049a9f 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