42 lines
2.8 KiB
Markdown
42 lines
2.8 KiB
Markdown
# 皮肤、主题与资源
|
|
|
|
## 静态字典方案
|
|
|
|
`SkinDefault.xaml`、`SkinDark.xaml`、`SkinViolet.xaml` 是皮肤字典;`Theme.xaml` 提供控件主题。先合并一个皮肤,再合并主题,应用自己的覆盖资源置于其后。通过 `DynamicResource` 引用需要随皮肤变化的颜色和画刷,例如 `PrimaryBrush`、`RegionBrush`;显式样式键通常用 `StaticResource`。
|
|
|
|
```xml
|
|
<ResourceDictionary.MergedDictionaries>
|
|
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDark.xaml"/>
|
|
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
|
|
</ResourceDictionary.MergedDictionaries>
|
|
```
|
|
|
|
这是现有 ResourceDictionary 内的片段,不应额外嵌套 Application.Resources。运行时更换静态皮肤时替换由应用持有的那一个皮肤字典,保留其他字典和顺序,不清空全部 MergedDictionaries。
|
|
|
|
## Theme 对象方案
|
|
|
|
当前源码还提供 `HandyControl.Themes.Theme : ResourceDictionary`,其 `Skin` 为 `HandyControl.Data.SkinType`;可以用 Theme 对象管理皮肤。采用此方案时先读取 `Themes/Theme.cs` 的构造和更新逻辑,避免同时再加载一套静态主题字典。
|
|
|
|
```xml
|
|
<ResourceDictionary.MergedDictionaries>
|
|
<hc:Theme Skin="Dark"/>
|
|
</ResourceDictionary.MergedDictionaries>
|
|
```
|
|
|
|
`hc` 对应 `https://handyorg.github.io/handycontrol`。修改 Theme 实例的 Skin 需要保留对该实例的引用;自动跟随系统、强调色等能力以实际版本的 Theme 源码属性为准。
|
|
|
|
## 调整外观
|
|
|
|
先找到控件对应的 `Themes/Styles/<控件>.xaml`,核对 x:Key、TargetType、BasedOn 和 TemplateBinding。能通过公开属性或附加属性完成的改动优先使用这些接口;确需重写模板时保留必需的 PART_* 名称。
|
|
|
|
本仓库 `Themes/Theme.xaml` 由 `XamlCombine.exe` 和 `Theme_GE45.txt` / `Theme_40.txt` 合并。修改相应样式源文件及必要的清单,再按项目构建流程生成;不要把合并文件当作唯一维护入口。使用方项目通常只需要覆盖自己的资源。
|
|
|
|
先检查工作区已有修改,尤其颜色和主题文件,避免覆盖用户的定制。验证浅色、深色切换时检查文本对比度、禁用态、弹出层及局部资源覆盖;无法运行 UI 时说明限制。
|
|
|
|
## 源码依据
|
|
|
|
- [Theme 实现](https://github.com/HandyOrg/HandyControl/blob/master/src/Shared/HandyControl_Shared/Themes/Theme.cs)
|
|
- [皮肤枚举](https://github.com/HandyOrg/HandyControl/blob/master/src/Shared/HandyControl_Shared/Data/Enum/SkinType.cs)
|
|
- [样式源目录](https://github.com/HandyOrg/HandyControl/tree/master/src/Shared/HandyControl_Shared/Themes/Styles)
|
|
- [主题合并清单](https://github.com/HandyOrg/HandyControl/blob/master/src/Shared/HandyControl_Shared/Themes/Theme_GE45.txt)
|