简体   繁体   English

在Uwp VisualState.Setters中使用StaticResource

[英]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 would happen if you were defining more than one property on your resource? 如果您在资源上定义多个属性,将会发生什么? How would you then be able to set a property style to that resource holding multiple styled properties? 然后,您如何才能为拥有多个样式属性的资源设置属性样式?
  • What would happen if you defined a resource holding a Color for the background, but you then use that resource to set it as the foreground of your dependency object control? 如果您为背景定义了一个持有Color的资源,但是随后又使用该资源将其设置为依赖对象控件的前景,将会发生什么? Should that flexibility exist, since you are targeting a property which accepts the same type? 由于您要针对接受相同类型的属性,是否应该存在这种灵活性?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM