Xaml 如何在UWP应用程序中覆盖用户重音颜色
我使用应用程序为我的应用程序生成主题资源 我的深色方案是黑色/灰色,带有橙色调 当我在Windows10设置中将重音颜色设置为绿色时(见下图),这种重音颜色在某些地方会出现 由于绿色和橙色不能很好地搭配在一起,这看起来真的很糟糕。我如何确保这种情况不会发生 其他类似的问题,所以有答案,不适合我(请不要标记为重复) 这就是我所做的 在资源字典中,我为我的“黑暗”主题定义了橙色口音。这是由Fluent XAML主题编辑器生成的(重音和覆盖均为橙色):Xaml 如何在UWP应用程序中覆盖用户重音颜色,xaml,uwp,themes,Xaml,Uwp,Themes,我使用应用程序为我的应用程序生成主题资源 我的深色方案是黑色/灰色,带有橙色调 当我在Windows10设置中将重音颜色设置为绿色时(见下图),这种重音颜色在某些地方会出现 由于绿色和橙色不能很好地搭配在一起,这看起来真的很糟糕。我如何确保这种情况不会发生 其他类似的问题,所以有答案,不适合我(请不要标记为重复) 这就是我所做的 在资源字典中,我为我的“黑暗”主题定义了橙色口音。这是由Fluent XAML主题编辑器生成的(重音和覆盖均为橙色): 根据generic.xaml(在C:\Prog
根据generic.xaml
(在C:\Program Files(x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.19041.0\generic
中,AccentButtonStyle
将以下内容用于悬停背景:
AccentButtonBackgroundPointerOver
这是一个使用SystemControlForegroundAccentBrush
的资源,它反过来使用SystemAccentColor
。这是您需要覆盖的资源,以避免系统强调色通过,例如:
<Color x:Key="SystemAccentColor">#FFFF00</Color>
和App.xaml
:
<Grid>
<Button Style="{StaticResource AccentButtonStyle}" />
</Grid>
<Application
x:Class="App8.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App8">
<Application.Resources>
<Color x:Key="SystemAccentColor">#FF0000</Color>
</Application.Resources>
</Application>
#FF0000
解决此问题的一种方法是自定义控件模板
我首先从以下位置复制标准控件模板:
C:\Program Files(x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.19041.0\Generic\themeresources.xaml
进入我的资源字典
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ThemeDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
然后我煞费苦心地修改模板,以消除不合适的颜色。大概是这样的:
<Button Style="{StaticResource AccentButtonStyle}" Content="Start"/>
<VisualState x:Name="PointerOver">
<VisualState.Setters>
<Setter Target="RootGrid.(RevealBrush.State)" Value="PointerOver" />
<Setter Target="RootGrid.Background" Value="Transparent" />
<Setter Target="ContentPresenter.BorderBrush" Value="{ThemeResource SystemBaseLowColor}" />
<Setter Target="ContentPresenter.Foreground" Value="{ThemeResource SystemAccentColor}" />
</VisualState.Setters>
这真的是一项枯燥而不必要的工作,我不知道为什么微软的人都不参与其中。这绝对不仅仅是我的问题,这发生在微软的官方Fluent XAML编辑器应用程序中。我发现UseSystemFocusVisuals属性似乎与此相关
根据这一点,默认设置为false,但不是
如果控件使用系统绘制的焦点视觉效果,则为true;如果控件使用ControlTemplate中定义的焦点视觉效果,则为false。默认值为false;见备注
如果我查看默认控件模板,这实际上设置为true:
<x:Boolean x:Key="UseSystemFocusVisuals">True</x:Boolean>
True
我尝试将其设置为false,但似乎没有任何区别:
<Setter Property="UseSystemFocusVisuals" Value="False" />
找到了问题
在我的app.xaml
中,我有一个WinUI控件:
<Application>
<Application.Resources>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
</Application.Resources>
</Application>
在每一页我都有一个颜色主题作为资源字典
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ThemeDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
由于某些原因,这不能正常工作
当我在app.xaml中放入并删除页面资源时,重音颜色的奇怪问题消失了
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<ResourceDictionary Source="ThemeDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
我现在在ContentDialog
上遇到问题,但这是另一篇SO帖子。此资源合并似乎有问题…谢谢。我不能添加SystemAccentColor,它说它已经在这个范围内定义了。我想就是这样:@那下很好奇。我无法复制这个问题。你能制作一个小的空白应用程序来演示这个问题并把它放到GitHub上吗?我想看一看问题似乎出在NavigationView控件上。我不能用简单的项目复制它,只能在导航视图页面中复制。我在这里上传了这个项目:你有机会在GitHub上看一下这个项目吗?@啊,对不起,还没有。我今天会尝试这样做,但如果没有,一定要再次提醒我:-D。
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<ResourceDictionary Source="ThemeDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>