[英]Use a StaticResource in Uwp VisualState.Setters
This is my code 这是我的代码
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Property="Background" Value="{StaticResource LightButtonBackground}"/>
<Setter Property="Foreground" Value="White" />
</VisualState.Setters>
</VisualState>
The compiler says XamlCompiler error WMC0615: Type 'StaticResource' used after '{' must be a Markup Extension. Error code 0x09ff.
编译器说XamlCompiler error WMC0615: Type 'StaticResource' used after '{' must be a Markup Extension. Error code 0x09ff.
XamlCompiler error WMC0615: Type 'StaticResource' used after '{' must be a Markup Extension. Error code 0x09ff.
I didn't find any useful information on the network. 我在网络上找不到任何有用的信息。 What's wrong? 怎么了?
You're mixing the two usages of the Setter here. 您在这里混合了Setter的两种用法。
The Property
property can be used only when defining a Style
: 只有在定义Style
时才能使用Property
属性:
<Style TargetType="TextBlock" x:Key="TextBlockStyle">
<Setter Property="Foreground" Value="Navy"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
</Style>
In a style definition the targeted control type is already known (It is provided by Style.TargetType
). 在样式定义中,目标控件类型是已知的(由Style.TargetType
提供)。
In a VisualState.Setters
list, you are not defining a Style
. 在VisualState.Setters
列表中,您没有定义Style
。 You are altering some properties on some existing child controls . 您正在更改某些现有子控件上的某些属性。 In this case, you need to use the Target
property to let the XAML runtime know which element and properties you are targeting. 在这种情况下,您需要使用Target
属性使XAML运行时知道您要定位的元素和属性。
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="NarrowState">
<VisualState.Setters>
<Setter Target="myPanel.Orientation" Value="Vertical"/>
<Setter Target="myPanel.Width" Value="380"/>
<Setter Target="myTextBlock.MaxLines" Value="3"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<StackPanel x:Name="myPanel" Orientation="Horizontal">
<TextBlock x:Name="myTextBlock" MaxLines="5" Style="{ThemeResource BodyTextBlockStyle}"/>
</StackPanel>
</Grid>
As @Vincent answer points out, you are not utilizing the setter correctly when defining it inside a visual state, but instead utilizing as you would when defining a style resource. 正如@Vincent答案所指出的那样,在视觉状态下定义设置器时,您没有正确使用它,而是在定义样式资源时像在使用那样。 Nevertheless my answer is an insight on how you can utilize a defined StaticResource
on a style, analyzing the two situations and whether it is possible or not. 不过,我的答案是关于如何在样式上使用定义的StaticResource
的见解,分析这两种情况以及是否可行。
With that said, I don't think it's possible to define a property of a style by setting it onto a resource. 话虽如此,我认为不可能通过将样式设置为资源来定义样式的属性。
What you are actually looking for is the BasedOn
, property which allows to inherit styles. 您实际上正在寻找的是BasedOn
属性,该属性允许继承样式。 The only downside is that Styles
that inherit from other styles must target the same type of control or either a control that derives from the type targeted by the base style. 唯一的缺点是, Styles
,从其他样式继承必须针对同一类型的控制或者要么是从由基本样式有针对性的类型派生的控制。
Take a look onto the documentation, here: https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/xaml-styles#use-based-on-styles 在这里查看文档: https : //docs.microsoft.com/zh-cn/windows/uwp/design/controls-and-patterns/xaml-styles#use-based-on-styles
Edit: 编辑:
Unfortunately, I don't think it's possible to define your VisualState
style to inherit from a resource using the BasedOn
, since we are forced to specify every setter on its definition. 不幸的是,我认为不可能使用BasedOn
定义从资源继承的VisualState
样式,因为我们被迫在其定义上指定每个设置器。 Might we be under an XY problem ? 我们可能会遇到XY问题吗? On a resource definition, that would be the way to go if you want to inherit from a style, but actually applying it on a visual state seems to be another completely story. 在资源定义上,如果要从样式继承,那将是一种方法,但实际上将其应用于视觉状态似乎又是另一回事了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.