【问题标题】:How can I set page background color using ResoureDictionary?如何使用 ResoureDictionary 设置页面背景颜色?
【发布时间】:2021-02-13 10:42:44
【问题描述】:

我创建了一个ResourceDictionary 文件来存储Page 的背景颜色。接下来,我在主页中引用了ResourceDictionary

虽然 Visual Studio 2019 (16.7.7) 设计器正确地将 Page 的背景颜色呈现为红色,但正在运行的程序本身却没有。

我做错了什么?



以下是来源:

MainPage.xaml

<Page
    x:Class="Test1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Page.Resources>
    <StackPanel>
        <TextBlock>
            Test
        </TextBlock>
    </StackPanel>
</Page>

Dictionary1.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

    <Style TargetType="Page">
        <Setter Property="Background" Value="Red" />
    </Style>
</ResourceDictionary>

【问题讨论】:

    标签: uwp uwp-xaml


    【解决方案1】:

    您不想在Page 中导入资源字典,而是在App.xaml 中导入此资源字典,如下所示:

    <Application
        x:Class="Test1.App">
        
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Dictionary1.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    然后您需要将x:Key 添加到您的自定义样式中:

    <Style x:Key="CustomPageBackground" TargetType="Page">
        <Setter Property="Background" Value="Red" />
    </Style>
    

    现在在您的Page 中,您只需添加以下行:

    Style="{StaticResource CustomPageBackground}"
    

    像这样:

    <Page
        x:Class="Test1.MainPage"
        Style="{StaticResource CustomPageBackground}"
        ...>
        
        <!-- UI-elements -->
    </Page>
    

    【讨论】:

    • 感谢@leander,为我提供了这个解决方案!我特别问这个问题是为了更深入地了解框架。因此,从您的解决方案中,您暗示 ResourceDictionary 不能 应用于其 自己的 对象,而只能应用于其中的 children目的? (不考虑继承,就像你做的那样。)
    • 我问这个是因为我想我可能在 UWP 中发现了一个错误。因为以下内容也不起作用,尽管它应该:&lt;Page&gt; &lt;Page.Resources&gt; &lt;Style TargetType="Page"&gt; &lt;Setter Property="Background" Value="Lavender" /&gt; &lt;/Style&gt; &lt;/Page.Resources&gt; &lt;/Page&gt;
    • @AxD 不,不是,您只是在定义样式,而不是将页面样式设置为此样式。您应该使用&lt;Page&gt;&lt;Page.Style&gt;&lt;Style TargetType="Page"&gt;&lt;Setter Property="Background Value="Lavender"/&gt;&lt;/Style&gt;&lt;/Page.Style&gt;&lt;/Page&gt;,或者在层次结构的更高位置定义样式。
    • 如果是这样,那么文档一定是错误的。在整个页面中,他们都在使用我正在使用的代码:docs.microsoft.com/windows/uwp/design/controls-and-patterns/…
    • 嗯是的忘记了隐式样式,有一段时间没有使用它了...您是否尝试设置例如 Button.Resources 来更改其样式?想知道这是否有效。
    【解决方案2】:

    来自 Jim Walker @https://github.com/MicrosoftDocs/windows-uwp/issues/2790#issuecomment-722065687

    在您的应用程序中更改颜色的首选方法是lightweight styling,它通过使用相同的键创建您自己的画笔来覆盖控制使用的系统画笔。大多数控件的类文档都有一个您可以覆盖的资源列表。

    对于Page 背景,您覆盖ApplicationPageBackgroundThemeBrush 资源:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.ThemeDictionaries>
                <ResourceDictionary x:Key="Default">
                    <SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="Red"/>
                    <Style TargetType="TextBlock">
                        <Setter Property="Foreground" Value="Black"/>
                    </Style>
                </ResourceDictionary>
            </ResourceDictionary.ThemeDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    (因为它是ThemeResource,所以它需要在主题字典中。我使用带有“Default”键的字典以保持简单,但最好同时使用“Light”和“Dark” XAML theme resources 中讨论的资源。)

    这还假定页面背景设置为此资源,在 Visual Studio 模板中默认设置为该资源,如下所示:Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-30
      • 2023-03-29
      • 2010-12-11
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 2020-10-04
      • 2017-10-30
      相关资源
      最近更新 更多