1# @ohos.uiAppearance (用户界面外观) 2 3用户界面外观提供管理系统外观的一些基础能力,目前仅包括深浅色模式配置。 4 5> **说明:** 6> 7> 从API Version 10开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8> 9> 本模块接口为系统接口。 10 11 12## 导入模块 13 14```ts 15import uiAppearance from '@ohos.uiAppearance' 16``` 17 18 19## DarkMode 20 21深色模式枚举。 22 23 24**系统能力:** SystemCapability.ArkUI.UiAppearance 25 26| 名称 | 值 | 说明 | 27| -- | -- | -- | 28| ALWAYS_DARK | 0 | 系统始终为深色。 | 29| ALWAYS_LIGHT | 1 | 系统始终为浅色。 | 30 31 32## uiAppearance.setDarkMode 33 34setDarkMode(mode: DarkMode, callback: AsyncCallback\<void>): void 35 36设置系统深色模式。 37 38**需要权限:** ohos.permission.UPDATE_CONFIGURATION 39 40**系统能力:** SystemCapability.ArkUI.UiAppearance 41 42**参数:** 43 44| 参数名 | 类型 | 必填 | 说明 | 45| -- | -- | -- | -- | 46| mode | [DarkMode](#darkmode) | 是 | 指定系统的深色模式配置 | 47| callback | AsyncCallback\<void>| 是 | 配置深色模式的异步回调 | 48 49**错误码:** 50 51错误码详细介绍请参考[errcode-uiappearance](../errorcodes/errorcode-uiappearance.md)。 52 53| 错误码ID | 错误信息 | 54| -- | -- | 55| 500001 | Internal error. | 56 57**示例:** 58 59 ```ts 60import uiAppearance from '@ohos.uiAppearance' 61import { BusinessError } from '@ohos.base'; 62try { 63 uiAppearance.setDarkMode(uiAppearance.DarkMode.ALWAYS_DARK, (error) => { 64 if (error) { 65 console.error('Set dark-mode failed, ' + error.message); 66 } else { 67 console.info('Set dark-mode successfully.'); 68 } 69 }) 70} catch (error) { 71 let message = (error as BusinessError).message; 72 console.error('Set dark-mode failed, ' + message); 73} 74 ``` 75 76 77## uiAppearance.setDarkMode 78 79setDarkMode(mode: DarkMode): Promise\<void>; 80 81设置系统深色模式。 82 83**需要权限:** ohos.permission.UPDATE_CONFIGURATION 84 85**系统能力:** SystemCapability.ArkUI.UiAppearance 86 87**参数:** 88 89| 参数名 | 类型 | 必填 | 说明 | 90| -- | -- | -- | -- | 91| mode | [DarkMode](#darkmode) | 是 | 指定系统深色模式配置 | 92 93**返回值:** 94 95| 类型 | 说明 | 96| ------ | ------------------------------ | 97| Promise\<void> | Promise对象。无返回结果的Promise对象。| 98 99**错误码:** 100 101错误码详细介绍请参考[errcode-uiappearance](../errorcodes/errorcode-uiappearance.md)。 102 103| 错误码ID | 错误信息 | 104| -- | -- | 105| 500001 | Internal error. | 106 107**示例:** 108 109 ```ts 110import uiAppearance from '@ohos.uiAppearance' 111import { BusinessError } from '@ohos.base'; 112try { 113 uiAppearance.setDarkMode(uiAppearance.DarkMode.ALWAYS_DARK).then(() => { 114 console.info('Set dark-mode successfully.'); 115 }).catch((error:Error) => { 116 console.error('Set dark-mode failed, ' + error.message); 117 }); 118} catch (error) { 119 let message = (error as BusinessError).message; 120 console.error('Set dark-mode failed, ' + message); 121} 122 ``` 123 124 125## uiAppearance.getDarkMode 126 127getDarkMode(): DarkMode; 128 129获取当前的深色模式配置。 130 131**需要权限:** ohos.permission.UPDATE_CONFIGURATION 132 133**系统能力:** SystemCapability.ArkUI.UiAppearance 134 135**返回值:** 136 137| 类型 | 说明 | 138| -- | -- | 139|[DarkMode](#darkmode) | 系统当前的深色模式配置 | 140 141**错误码:** 142 143错误码详细介绍请参考[errcode-uiappearance](../errorcodes/errorcode-uiappearance.md)。 144 145| 错误码ID | 错误信息 | 146| -- | -- | 147| 500001 | Internal error. | 148 149**示例:** 150 151 ```ts 152import uiAppearance from '@ohos.uiAppearance' 153import { BusinessError } from '@ohos.base'; 154try { 155 let darkMode = uiAppearance.getDarkMode(); 156 console.info('Get dark-mode ' + darkMode); 157} catch (error) { 158 let message = (error as BusinessError).message; 159 console.error('Get dark-mode failed, ' + message); 160} 161 ```