
一、在EntryAbility的onCreate()函数中添加下面代码:
AppStorage.setOrCreate<ConfigurationConstant.ColorMode>('currentColorMode', this.context.config.colorMode);
二、在EntryAbility中添加函数
onConfigurationUpdate(newConfig: Configuration): void {const currentColorMode: ConfigurationConstant.ColorMode | undefined = AppStorage.get('currentColorMode');if (currentColorMode !== newConfig.colorMode) {AppStorage.setOrCreate<ConfigurationConstant.ColorMode>('currentColorMode', newConfig.colorMode);}}
添加上面函数后,需要引入import { Configuration } from '@kit.AbilityKit';否则会报错。
三、在需要的页面添加下面代码:
@StorageProp('currentColorMode') @Watch('onCurrentColorModeChange') currentColorMode: ConfigurationConstant.ColorMode =ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET;private isSystemDark: boolean = false;
四、添加函数
onCurrentColorModeChange(): void {if (this.currentColorMode === ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT) {this.isSystemDark = false;} else if (this.currentColorMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK) {this.isSystemDark = true;}}
五、解释
isSystemDark 是我定义的变量。
评论 (7)
鸿蒙和安卓区别大吗:heo-被打:
@ 秫米是的,我也老是忘记,看来得弄个自动续期了:heo-被打:
@ 我是军爸是的哈,感谢提醒:heo-出家人:
@ zeruns.入门的话还是比较容易的
:heo-被打: 只从免费证书又缩短时效后,我频繁提醒,大佬!你证书过期了!!!
你的SSL证书到期了
鸿蒙开发入门难吗