Xaml Windows应用商店应用程序&x27;在模板中忽略了被重写的资源值
我正在创建一个Windows应用商店应用程序,其中每个页面都有一个用于标题、按钮等的主色调 在Xaml Windows应用商店应用程序&x27;在模板中忽略了被重写的资源值,xaml,templates,windows-runtime,windows-store-apps,winrt-xaml,Xaml,Templates,Windows Runtime,Windows Store Apps,Winrt Xaml,我正在创建一个Windows应用商店应用程序,其中每个页面都有一个用于标题、按钮等的主色调 在App.xaml中,我创建了一个默认的画笔,它在每个页面的xaml文件中被覆盖,并且为按钮创建了一个样式模板,我在每个页面中重复使用它。模板应该使用页面的颜色,但出于某种原因,它们坚持使用默认值 这是我的xaml文件 App.xaml <Application x:Class="Foo.App" xmlns="http://schemas.microsoft.com/winfx/2
App.xaml
中,我创建了一个默认的画笔
,它在每个页面
的xaml文件中被覆盖,并且为按钮
创建了一个样式模板,我在每个页面中重复使用它。模板应该使用页面的颜色,但出于某种原因,它们坚持使用默认值
这是我的xaml文件
App.xaml
<Application
x:Class="Foo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Foo"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<SolidColorBrush x:Name="SectionDefaultBrush" Color="Red" />
<Style TargetType="Button" x:Name="NavigationButtonStyle">
<Setter Property="FontFamily" Value="Myriad Pro" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Rectangle x:Name="Border" Fill="#f4f4f4" Margin="0" />
<TextBlock Foreground="{ ThemeResource SectionDefaultBrush }" Text=">" FontSize="31" />
<ContentPresenter x:Name="Content" VerticalAlignment="Center" Foreground="#5A5A5A" HorizontalAlignment="Left" FontSize="31"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
<Page
x:Class="Foo.Pages.SomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<SolidColorBrush x:Key="SectionDefaultBrush" Color="Green" />
</Page.Resources>
<ViewBox>
<Canvas>
<TextBlock Text="Some text" Foreground="{ ThemeResource SectionDefaultBrush }" Canvas.Left="130" TextWrapping="Wrap" Canvas.Top="252" Height="177" Width="507" FontFamily="Myriad Pro" FontSize="54" />
<Button Content="Click me" Style="{ ThemeResource NavigationButtonStyle }" Canvas.Left="130" Canvas.Top="900" Width="507" Height="48" />
</Canvas>
<ViewBox>
SomePage.xaml
<Application
x:Class="Foo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Foo"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<SolidColorBrush x:Name="SectionDefaultBrush" Color="Red" />
<Style TargetType="Button" x:Name="NavigationButtonStyle">
<Setter Property="FontFamily" Value="Myriad Pro" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Rectangle x:Name="Border" Fill="#f4f4f4" Margin="0" />
<TextBlock Foreground="{ ThemeResource SectionDefaultBrush }" Text=">" FontSize="31" />
<ContentPresenter x:Name="Content" VerticalAlignment="Center" Foreground="#5A5A5A" HorizontalAlignment="Left" FontSize="31"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
<Page
x:Class="Foo.Pages.SomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<SolidColorBrush x:Key="SectionDefaultBrush" Color="Green" />
</Page.Resources>
<ViewBox>
<Canvas>
<TextBlock Text="Some text" Foreground="{ ThemeResource SectionDefaultBrush }" Canvas.Left="130" TextWrapping="Wrap" Canvas.Top="252" Height="177" Width="507" FontFamily="Myriad Pro" FontSize="54" />
<Button Content="Click me" Style="{ ThemeResource NavigationButtonStyle }" Canvas.Left="130" Canvas.Top="900" Width="507" Height="48" />
</Canvas>
<ViewBox>
在本例中,
TextBlock
的文本颜色如预期的那样为绿色(如果我从页面资源中删除笔刷,则为红色),但按钮的内容仍为红色。有没有办法告诉模板使用最终颜色?该应用程序的资源字典不知道其他字典,与WPF的DynamicResources
-StaticResource
不同,它不会重新评估,而ThemeResource
会重新评估,但我认为只有在实际主题发生变化时才会这样做。自定义按钮颜色的方法是在模板中使用TemplateBinding
,并绑定按钮的前台
属性,还可以在按钮样式的前台
设置器
中将其设置为{StaticResource SectionDefaultBrush}
。然后,在页面中,您可以通过将按钮的前景
设置为不同的值或使用派生按钮样式来更改前景
值来覆盖该值
理想情况下,也应该在字典中定义主题资源,以便笔刷根据操作系统主题(特别是高对比度)而变化。然后,您可能希望将画笔命名为“SectionDefaultThemeBrush”,而不仅仅是“SectionDefaultBrush”。您尝试过在模板中使用StaticResource吗?是的,但结果是一样的,谢谢。虽然在本例中,当我们使用一个碰巧未使用的属性时(按钮的前台),感觉有点不舒服。。。如果需要将其设置为其他内容,我该怎么办?您还有背景:)否则你有几个选择。丑陋的解决方案是同时使用标记
,这是一个通用属性,可以用于任何事情,但除了原型设计之外,不应该用于任何事情。否则,您可以定义附加属性或子类按钮
并添加您自己的属性。哦,同样,您可以在XAML中执行所有操作,只需定义多个样式并根据位置切换样式,即可自定义任意数量的笔刷。通常情况下,尽管控件的颜色不超过2-3种,如果您确实需要更多颜色,这些应该是应用程序中随处可重用的常用笔刷资源。