【问题标题】:ThemeResource in Style isn't used when I build my UWP app构建 UWP 应用程序时未使用 Style 中的 ThemeResource
【发布时间】:2016-09-28 18:27:37
【问题描述】:

我正在尝试制作一个在点击事件后改变颜色的矩形。它应该在禁用模式(灰色)和强调色之间循环。我正在尝试使用样式来做到这一点。当我在 XAML 代码{ThemeResource SystemControlHighlightAltListAccentHighBrush} 中使用(一种阴影)强调色时,它会正确显示强调色。它还在设计器 (Visual Studio) 中显示正确的颜色。但是,如果我在资源字典中使用主题颜色,它会在我的应用程序中用灰色或无颜色替换它们(构建后)。这是我的资源字典:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:I_have_never">
    <!--Disabled-->
    <Style TargetType="RelativePanel" x:Key="Disabled">
        <Setter Property="Tag">
            <Setter.Value>0</Setter.Value>
        </Setter>
        <Setter Property="Margin">
            <Setter.Value>30,15,30,15</Setter.Value>
        </Setter>
        <Setter Property="Background">
            <Setter.Value>{ThemeResource SystemControlBackgroundListLowBrush}    </Setter.Value>
        </Setter>
    </Style>

    <!--Enabled-->
    <Style TargetType="RelativePanel" x:Key="Enabled">
        <Setter Property="Tag">
            <Setter.Value>2</Setter.Value>
        </Setter>
        <Setter Property="Margin">
            <Setter.Value>30,15,30,15</Setter.Value>
        </Setter>
        <Setter Property="Background">
            <Setter.Value>{ThemeResource SystemControlHighlightAltListAccentHighBrush}</Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

这是我的 XAML 代码:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="150" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <RelativePanel Name="Category1"
                           Grid.Row="0" 
                           Grid.Column="0"
                           Tapped="ToggleStatus" 
                           Style="{StaticResource Enabled}">
    </RelativePanel>
</Grid>

这是我在样式之间切换的 C# 代码:

private void ToggleStatus(object sender, RoutedEventArgs e)
    {
        RelativePanel panel = sender as RelativePanel;

        if ((string)panel.Tag == "0")
        {
            panel.Style = (Style)App.Current.Resources["Enabled"]; // Tag = 1
        }
        else if ((string)panel.Tag == "1")
        {
            panel.Style = (Style)App.Current.Resources["Disabled"]; // Tag = 2
        }
    }

在设计视图中,这很有效。它很好地显示了灰色或强调色背景。但是,当我构建它时,没有显示颜色。

只要我使用真实颜色(例如灰色)而不是 ThemeResource,它就会显示颜色。如果我在 XAML 代码中直接使用ThemeResource(无样式),它也可以工作。为什么它只适用于设计师?如果我在样式中使用ThemeResource,为什么它不起作用?我该如何解决这个问题?

【问题讨论】:

    标签: xaml uwp


    【解决方案1】:

    如果我在样式中使用 ThemeResource,为什么它不起作用?我该如何解决这个问题?

    您正在使用 XAML 属性元素用法语法 将 extensionUsage(即 {ThemeResource} 或 {StaticResource})设置为 Setter.Value property 样式:

    <Setter ...>
      <Setter.Value>
        objectValue
      </Setter.Value>
    </Setter>
    

    根据官方文档Setter.Value property的语法部分,在将extensionUsage(即{ThemeResource}或{StaticResource})设置为Setter.Value property时样式,我们应该使用 XAML 属性使用语法 而不是使用 XAML 属性元素使用语法:

    <Setter Property="propertyName" Value="extensionUsage"/>
    

    因此您应该使用以下代码在样式中设置带有{ThemeResource} 的背景:

    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ThemeResourceInStyle">
    
    <!--Disabled-->
    <Style TargetType="RelativePanel" x:Key="Disabled">
        <Setter Property="Tag">
            <Setter.Value>0</Setter.Value>
        </Setter>
        <Setter Property="Margin">
            <Setter.Value>30,15,30,15</Setter.Value>
        </Setter>
        <!--use XAML Attribute Usage Syntax to set extensionUsage -->
        <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundListLowBrush}" />
    </Style>
    
    <!--Enabled-->
    <Style TargetType="RelativePanel" x:Key="Enabled">
        <Setter Property="Tag">
            <Setter.Value>1</Setter.Value>
        </Setter>
        <Setter Property="Margin">
            <Setter.Value>30,15,30,15</Setter.Value>
        </Setter>
        <!--use XAML Attribute Usage Syntax to set extensionUsage -->
        <Setter Property="Background" Value="{ThemeResource SystemControlHighlightAltListAccentHighBrush}" />
    </Style>
    </ResourceDictionary>
    

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 1970-01-01
      • 2018-06-12
      • 2019-01-30
      • 2018-06-11
      • 1970-01-01
      • 2017-04-09
      • 2020-12-18
      • 1970-01-01
      相关资源
      最近更新 更多