【问题标题】:How to override user accent color in UWP app如何在 UWP 应用中覆盖用户强调色
【发布时间】:2020-10-11 03:28:34
【问题描述】:

我使用Fluent XAML Theme Editor app 为我的应用生成主题资源。

我的深色方案是带有橙色调的黑色/灰色。

当我在 Windows 10 设置中将强调色设置为绿色时(见下图),这种强调色会在某些地方出现。

由于绿色和橙色不协调,这看起来很糟糕。如何确保不会发生这种情况?

关于 SO 的其他类似问题的答案对我不起作用(请不要标记为重复)。

这就是我所做的。

在资源字典中,我为我的“黑暗”主题定义了橙色口音。这是由 Fluent XAML 主题编辑器生成的(重音和覆盖都是橙色阴影):

<Windows10version1809:ColorPaletteResources Accent="#FFCC4D11"...
<!-- Override system generated accent colors -->
<Color x:Key="SystemAccentColorDark1">#FFD4632D</Color>
<Color x:Key="SystemAccentColorDark2">#FFDC7949</Color>
<Color x:Key="SystemAccentColorDark3">#FFE58E66</Color>
<Color x:Key="SystemAccentColorLight1">#FFB93E0E</Color>
<Color x:Key="SystemAccentColorLight2">#FFA62F0A</Color>
<Color x:Key="SystemAccentColorLight3">#FF932107</Color>

我还按照 SO 其他地方的建议添加了这个:

<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="#FFCC4D11" />

但是,这些都不起作用,Windows 设置绿色仍然通过。例如,重音按钮在鼠标悬停时显示为绿色。鼠标悬停时,绿色也会出现在组合框和单选按钮中。

按钮定义如下:

  <Button Style="{StaticResource AccentButtonStyle}" Content="Start"/>

这是没有悬停和悬停时的样子。您不需要成为平面设计师就知道这是一个糟糕的外观。我希望悬停时出现不同的橙色阴影。这些阴影在资源字典中定义为SystemAccentColorDark1 - SystemAccentColorLight3,但由于某种原因它们似乎被忽略了。

如何始终如一地执行我的强调色?显然我不想重新设置每个控件的样式,我只是希望资源字典中的颜色能够始终如一地使用。

更新

即使在 Fluent XAML 主题编辑器应用程序本身中也会出现系统强调色,尽管不是用于“强调按钮”,而是用于“复选框”和其他一些控件。查看图片,当鼠标悬停在复选框上时,石灰突出显示可见。

【问题讨论】:

    标签: xaml uwp themes


    【解决方案1】:

    找到问题了。

    在我的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 帖子。这个资源合并好像有点不对劲……

    【讨论】:

      【解决方案2】:

      根据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>
      

      如果您将此资源放在全局位置(如在Application.xaml 中),它应该覆盖所有的强调色。

      我仍然不确定为什么 Fluent Theme 编辑器生成的强调色没有被应用。

      我已经在一个简单的空白应用程序上进行了测试 - MainPage.xaml:

      <Grid>
         <Button Style="{StaticResource AccentButtonStyle}" />
      </Grid>
      

      还有App.xaml:

      <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>
      

      【讨论】:

      • 谢谢。我无法添加 SystemAccentColor,它表示它已在此范围内定义。我认为就是这样:
      • @under 那很好奇。我无法复制问题。你能做一个小的空白应用程序来演示这个问题并将它放在 GitHub 上吗?我很想看看
      • 问题出在 NavigationView 控件上。我不能用一个简单的项目来复制它,只能在导航视图页面中。我在这里上传了项目:github.com/under3415/TestThemeAccent2
      • 您有机会在 GitHub 上查看该项目吗?
      • @under 啊,抱歉,还没有。我今天会尝试这样做,但如果没有,请务必再次提醒我:-D。
      【解决方案3】:

      解决此问题的一种方法是自定义控件模板。

      我首先从以下位置复制标准控件模板:

      C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.19041.0\Generic\themeresources.xaml

      进入我的资源字典。

      然后我煞费苦心地修改模板以消除令人反感的颜色。像这样的:

                             <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>
      

      这确实是一项乏味且不必要的工作,我不知道为什么没有来自 MS 的人参与其中。这对我来说绝对不是问题,这发生在 MS 的官方 Fluent XAML 编辑器应用程序中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-17
        相关资源
        最近更新 更多