xaml - UWP/XAML : What is the difference between theming in a page's Resources, 在单独的 ResourceDictionary 文件中,还是在 App.xaml 中?

标签 xaml uwp uwp-xaml

我有一个简单的 UWP 应用程序,它只有一个页面。在尝试覆盖 SystemAccentColor(参见其他问题,例如 this one)时,我发现在添加代码的 3 个有效位置中的每一个我都会得到不同的结果:

  • 在页面本身的资源部分。 (什么都不做)
  • 在单独的 ResourceDictionary 文件中。 (仅部分有效)
  • 在 App.xaml 文件中。 (作品)

  • 这是我的单个页面的 Page.Resources,我的自定义样式包含在 Dictionary.xaml 中:
    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Page.Resources>
    

    这是我在 3 个位置中的每一个中插入的 SystemAccentColor 代码,以下称为//THE CODE//:
    <Color x:Key="SystemAccentColor">Red</Color>
    
    <SolidColorBrush x:Key="SystemControlBackgroundAccentBrush" Color="{ThemeResource SystemAccentColor}" />
    <SolidColorBrush x:Key="SystemControlDisabledAccentBrush" Color="{ThemeResource SystemAccentColor}" />
    <SolidColorBrush x:Key="SystemControlForegroundAccentBrush" Color="{ThemeResource SystemAccentColor}" />
    <SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="{ThemeResource SystemAccentColor}" />
    <SolidColorBrush x:Key="SystemControlHighlightAltAccentBrush" Color="{ThemeResource SystemAccentColor}" />
    <SolidColorBrush x:Key="SystemControlHighlightAltListAccentHighBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.9" />
    <SolidColorBrush x:Key="SystemControlHighlightAltListAccentLowBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.6" />
    <SolidColorBrush x:Key="SystemControlHighlightAltListAccentMediumBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.8" />
    <SolidColorBrush x:Key="SystemControlHighlightListAccentHighBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.9" />
    <SolidColorBrush x:Key="SystemControlHighlightListAccentLowBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.6" />
    <SolidColorBrush x:Key="SystemControlHighlightListAccentMediumBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.8" />
    <SolidColorBrush x:Key="SystemControlHyperlinkTextBrush" Color="{ThemeResource SystemAccentColor}" />
    <SolidColorBrush x:Key="ContentDialogBorderThemeBrush" Color="{ThemeResource SystemAccentColor}" />
    <SolidColorBrush x:Key="JumpListDefaultEnabledBackground" Color="{ThemeResource SystemAccentColor}" />
    

    页面的资源部分

    将//THE CODE//插入Page.Resources 中没有任何作用。
    <ResourceDictionary>
    
        <ResourceDictionary.ThemeDictionaries>
            //THE CODE//
        </ResourceDictionary.ThemeDictionaries>
    
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    
    </ResourceDictionary>
    

    指定这应该应用于 Light 主题也没有任何作用。
    <ResourceDictionary>
    
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Light">
                //THE CODE//
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    
    </ResourceDictionary>
    

    资源字典

    将//THE CODE//插入Dictionary.xaml 也没有任何作用。
    <ResourceDictionary.ThemeDictionaries>
        //THE CODE//
    </ResourceDictionary.ThemeDictionaries>
    

    但是,指定 Light/Dark 主题会导致颜色覆盖应用,但仅适用于我的自定义样式,这些样式也在 Dictionary.xaml 中定义。但是,它仅适用于 Windows 浅色/深色主题匹配的情况。
    <ResourceDictionary.ThemeDictionaries>
        <ResourceDictionary x:Key="Light">
            //THE CODE//
        </ResourceDictionary>
    </ResourceDictionary.ThemeDictionaries>
    

    应用程序.xaml

    在 App.xaml 中插入//THE CODE//就可以了。
    <Application.Resources>
        <ResourceDictionary>
            //THE CODE//
        </ResourceDictionary>
    </Application.Resources>
    

    编辑:我还可以将暗/亮主题指定为 x:Key,一切都按预期工作。
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.ThemeDictionaries>
                <ResourceDictionary x:Key="Light">
                    //THE CODE//
            </ResourceDictionary.ThemeDiciontaries>
        </ResourceDictionary>
    </Application.Resources>
    

    包起来

    总而言之,这里到底发生了什么?这显然看起来像是某种范围问题,但我在开发人员文档中找不到任何解释我在这里看到的行为的内容。

    最佳答案

    其中很多不起作用的原因是ResourceDictionary.ThemeDictionaries期望它的内容是资源字典,其键对应于各个项目。如果你把常规资源,比如主题画笔放在那里,主题资源查找不知道要做什么,因此它不会拾取资源。
    如果您在医学词典中指定资源,并且还加载包含该资源的词典,则资源词典将覆盖主题资源词典值,这就是为什么混合主题词典并在合并词典中加载资源词典通常是不好的主意。
    如果你只在主题字典“light”中指定一个资源,当应用程序/页面字典是轻主题时,它会被资源查找选中。但是,如果页面采用深色主题,它将查看“深色”字典,在您的情况下该字典丢失了,然后向上查找树,直到找到具有指定资源的深色主题字典。因此,要在所有主题中覆盖该资源,您需要在所有主题词典(浅色、深色和默认)中指定资源。

    关于xaml - UWP/XAML : What is the difference between theming in a page's Resources, 在单独的 ResourceDictionary 文件中,还是在 App.xaml 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37349007/

    相关文章:

    c# - 如何在 Silverlight 中将 List<object> 绑定(bind)到 DataGrid?

    wpf - 使用元素语法设置 UserControl 上的附加属性值

    wpf - DataTrigger 上的 Storyboard未正确触发

    c# - Windows 10 Mobile (10.0.14393) 地理围栏后台任务 (LocationTrigger)

    uwp - AppServiceConnection可以双向通信吗?

    uwp - 如何在 UWP 中创建类似于 Segoe MDL2 Assets 的图标

    wpf - 修改组合框的ItemsSource ObservableCollection后如何刷新组合框

    c# - UWP - 如何在 C# 中使用 OneDrive API 同步文件

    c# - VS2017 中的 XAML 中的 DesignInstance 在设计时不显示数据

    c# - 模板控件中的 ContentPresenter 不起作用