OpenHarmony开发者论坛
标题:
stage模型,@Component组件内如何获取UIAbilityContext
[打印本页]
作者:
汐之蓝
时间:
2023-9-7 17:19
标题:
stage模型,@Component组件内如何获取UIAbilityContext
[md]我在使用首选项相关接口时,需要传参context,context是一个UIAbilityContext类型,以下是官网的描述,没看懂如何获取,在stage模型,@Component组件内如何获取UIAbilityContext?
![问题图片.png](data/attachment/forum/202309/07/170846k3tksusnxpf5zthu.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "问题图片.png")
【运行环境】
硬件:rk3568; ROM: 3.2 Release; API 9
[/md]
作者:
hyacinth养花人
时间:
2023-9-7 17:20
可以参考如下代码,在aboutToAppear中通过UIAbility. Context获取。
import common from '@ohos.app.ability.common';
@Entry
@Component
struct AbilityContextTest {
// abilityContext
@State UIAbilityInfo: string = '获取 abilityInfo'
UIAbilityContext: common.UIAbilityContext
aboutToAppear() {
// getContext获取Context,转为abilityContext
this.UIAbilityContext = getContext(this) as common.UIAbilityContext
}
build() {
Row() {
Column({ space: 20 }) {
Text(this.UIAbilityInfo)
.fontSize(20)
.onClick(() => {
this.UIAbilityInfo = JSON.stringify(this.UIAbilityContext.abilityInfo)
console.log(`ContextDemo abilityInfo = ${this.UIAbilityInfo}`)
})
}
.width('100%')
}
.height('100%')
}
}
复制代码
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5