如何修改Avalonia的FluentTheme主题的部分样式(如自定义背景色)
Avalonia FluentTheme 局部样式修改方案
你的修改不生效是因为FluentTheme内置资源的优先级高于你在独立Style节点中定义的资源,且你使用的资源键并非FluentTheme实际调用的全局背景资源键。
正确的覆写方式是直接在FluentTheme节点的Resources属性下定义要覆盖的资源值,参考实现如下:
<Application xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Avalonia.Test" x:Class="Avalonia.Test.App"> <Application.DataTemplates> <local:ViewLocator /> </Application.DataTemplates> <Application.Styles> <FluentTheme Mode="Dark"> <FluentTheme.Resources> <!-- 覆写全局基础背景色 --> <Color x:Key="SolidBackgroundFillColorBase">#CCFF0068</Color> <!-- 覆写窗口背景色 --> <Color x:Key="WindowBackgroundColor">#CCFF0068</Color> <SolidColorBrush x:Key="WindowBackgroundBrush" Color="{DynamicResource WindowBackgroundColor}"/> </FluentTheme.Resources> </FluentTheme> </Application.Styles> </Application>
注意事项
- 确保你的主窗口、根布局没有手动设置
Background属性,手动赋值的属性优先级高于主题资源,会覆盖主题配置 - 如需修改其他组件的默认样式,直接找到FluentTheme中对应组件的资源键,按上述方式放在
FluentTheme.Resources节点下覆写即可 - 如果修改后部分区域背景未生效,可以针对对应区域的组件单独覆写其用到的背景资源键
内容的提问来源于stack exchange,提问作者Hyding
相关产品推荐
相关产品推荐

