HarmonyOS鸿蒙Next中class类中怎么优雅的获取上下文
HarmonyOS鸿蒙Next中class类中怎么优雅的获取上下文
PreferencesUtil.preference = dataPreferences.getPreferencesSync(getContext(PreferencesUtil), options);
目前getContext()已经废弃了,我看有的是用AppStorage存起来,有的是通过其他方式传入这个类,
有没有更优雅的更方便的可以支持在class类里面使用上下文
上下文最好是全局统一一个获取方法,可以定义一个AppUtil ,在onCreate中init获取到上下文
export class AppUtil {
private static context: common.UIAbilityContext; //common.UIAbilityContext,上下文
static isPrivacyAgree: boolean = false
private constructor() {
}
/**
* 初始化方法,缓存全局变量,在UIAbility的onCreate方法中初始化该方法。
* @param windowStage 窗口管理器
*/
static init(context: common.UIAbilityContext) {
AppUtil.context = context;
SysPrivacyUtil.init()
AppUtil.isPrivacyAgree = SysPrivacyUtil.queryPrivacyAgree()
}
}
初始化
AppUtil.init(this.context)
这样能尽量保证对上下文的处理都是同一个
更多关于HarmonyOS鸿蒙Next中class类中怎么优雅的获取上下文的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
感谢提供新思路,
通过UIContext动态获取(适用于鸿蒙NEXT(API19+)及支持UIContext的版本)
// 类中获取UIContext实例
const uiContext = AppStorageV2.connect(UIContext, 'uiContext', () => new UIContext());
const context = uiContext?.getHostContext(); // 最终上下文
此方式通过AppStorageV2
与UIContext
解耦,无需手动存储上下文引用。
找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17
获取应用上下文使用 application.getApplicationContext 获取。获取ability上下文就自己拿工具类缓存起来咯。
在UIAbility中获取:
onWindowStageCreate(windowStage: window.WindowStage): void {
AppStorage.setOrCreate("globalUIContext", windowStage.getMainWindowSync().getUIContext());
}
我目前用的api18,用这个写法启动会报错,您有使用过其他的吗
Error name: Error
Error message: This window state is abnormal.
Error code:
Stacktrace:
- at onWindowStageCreate entry (entry/src/main/ets/entryability/EntryAbility.ets:21:52)
我发现AppStorageV2不支持直接写字符串了,你看这样行不
调用的类中这样写
private static context: UIContextModel | null =
AppStorageV2.connect(UIContextModel, 'globalUIContext', () => new UIContextModel(null))!;
EntryAbility类中这样写
AppStorageV2.connect(UIContextModel, 'globalUIContext', () => new UIContextModel(this.context));
再定义一个类UIContextModel接受
import { Context } from '@ohos.arkui.UIContext';
export class UIContextModel {
private context: Context | null = null;
constructor(context: Context | null) {
this.context = context
}
getUIContext(): Context | null {
return this.context;
}
}
在HarmonyOS Next中,获取上下文可通过Context
模块实现。在类中推荐使用依赖注入方式获取,或通过AbilityContext
传递。具体方法如下:
- 在Ability中直接使用
this.context
获取 - 在UI组件中使用
getContext()
方法 - 对于ServiceAbility等场景,可通过
featureAbility.getContext()
获取
注意避免全局静态存储Context。ArkTS中可通过@State
或@Provide
装饰器管理上下文引用。
在HarmonyOS Next中,获取上下文的推荐方式有以下几种:
- 依赖注入方式(推荐): 通过构造函数或方法参数将Context传入类中,这是最清晰和可测试的方式:
class PreferencesUtil {
private context: Context;
constructor(context: Context) {
this.context = context;
}
getPreferences() {
return dataPreferences.getPreferencesSync(this.context, options);
}
}
- 使用AbilityContext: 如果是在Ability中,可以直接使用AbilityContext:
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
class MyClass {
private context: abilityAccessCtrl.Context;
init(context: abilityAccessCtrl.Context) {
this.context = context;
}
}
- 对于UI组件: 使用组件自带的context属性:
@Component
struct MyComponent {
private context = getContext(this);
build() {
// 使用this.context
}
}
- 全局上下文(谨慎使用): 可以在应用启动时将Context保存到全局变量或AppStorage:
// 应用启动时
AppStorage.setOrCreate('appContext', context);
// 使用时
const context = AppStorage.get('appContext');
注意:避免滥用全局Context,优先考虑依赖注入方式,这样代码更易于维护和测试。