【发布时间】: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,为什么它不起作用?我该如何解决这个问题?
【问题讨论】: