UWP报错:Style类型中找不到可附加属性'Resources'
UWP Style中添加内部资源的解决方案
嘿,我完全懂你碰到的这个问题——UWP里的Style确实不像WPF那样支持直接嵌套Style.Resources,这是UWP XAML框架的一个设计限制。不过别发愁,有几个实用的替代方案能帮你实现类似的需求,根据你的资源使用场景选就行:
方案1:将资源移到ResourceDictionary根节点或父级容器
如果你的资源需要被多个Style共享,或者只是当前字典内的Style使用,直接把资源放在Style所在的ResourceDictionary根节点是最直接的方式:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestProject.Resources"> <!-- 把Style需要的资源放在ResourceDictionary根节点 --> <SolidColorBrush x:Key="ButtonHoverBrush" Color="#FFE0E0E0"/> <SolidColorBrush x:Key="ButtonPressedBrush" Color="#FFC0C0C0"/> <Style TargetType="Button"> <Setter Property="Background" Value="White"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border x:Name="RootBorder" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="PointerOver"> <Storyboard> <ColorAnimation Storyboard.TargetName="RootBorder" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="{StaticResource ButtonHoverBrush}" Duration="0:0:0.2"/> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
提示:如果资源只给特定Style用,可以给Key加个专属前缀(比如MySpecialButton_HoverBrush),避免和其他资源冲突。
方案2:将资源嵌套在ControlTemplate内部
如果你的资源只和当前Style的ControlTemplate相关,完全可以把资源放在ControlTemplate.Resources里,这样资源只会在模板范围内生效,不会污染外部空间:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestProject.Resources"> <Style TargetType="Button"> <Setter Property="Background" Value="White"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <ControlTemplate.Resources> <!-- 这里放当前模板专属的资源 --> <SolidColorBrush x:Key="ButtonHoverBrush" Color="#FFE0E0E0"/> <SolidColorBrush x:Key="ButtonPressedBrush" Color="#FFC0C0C0"/> </ControlTemplate.Resources> <Border x:Name="RootBorder" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="PointerOver"> <Storyboard> <ColorAnimation Storyboard.TargetName="RootBorder" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="{StaticResource ButtonHoverBrush}" Duration="0:0:0.2"/> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
方案3:使用合并字典管理共享资源
如果你的项目比较大,需要多个ResourceDictionary共享一组资源,可以创建单独的资源字典文件,然后通过合并字典的方式引入:
第一步:创建共享资源字典(比如SharedButtonResources.xaml)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="ButtonHoverBrush" Color="#FFE0E0E0"/> <SolidColorBrush x:Key="ButtonPressedBrush" Color="#FFC0C0C0"/> </ResourceDictionary>
第二步:在你的Style所在字典中合并它
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestProject.Resources"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="SharedButtonResources.xaml"/> </ResourceDictionary.MergedDictionaries> <Style TargetType="Button"> <Setter Property="Background" Value="White"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <!-- 直接引用共享字典里的资源 --> <Border x:Name="RootBorder" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <!-- ... 模板内容 ... --> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
总的来说,UWP的Style确实没有内置的Resources属性,所以你只需要根据资源的使用范围,选择上面的某一种方案就可以解决问题啦。
内容的提问来源于stack exchange,提问作者mazury
相关产品推荐
相关产品推荐

