【问题标题】:CommonStates not triggered automaticallyCommonStates 不会自动触发
【发布时间】:2018-09-04 09:01:02
【问题描述】:

我正在尝试使用CommonStates 来为我的用户控件设置动画。 我尝试了以下方法:

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
            <VisualState x:Name="Normal">
            </VisualState>
            <VisualState x:Name="PointerOver">
                <Storyboard>
                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="circle"
                                                  Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                                  EnableDependentAnimation="True">
                        <LinearColorKeyFrame Value="Red"
                                             KeyTime="0:0:0.02" />
                    </ColorAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
            <VisualState x:Name="Pressed">
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Ellipse x:Name="circle"
             Height="280"
             Width="280"
             Fill="Green" />
</Grid>

我可以通过手动调用UpdateStates(true) 来产生更改,但是参考文档应该会自动触发 commonstates。

知道我做错了什么吗?

【问题讨论】:

  • 没有UpdateStates(Boolean)方法,你的意思是VisualStateManager.GoToState方法吗?
  • 是的,我的意思是 VisualStateManager.GoToState,抱歉。我想知道为什么默认情况下不会触发它。我读了这篇文章:stackoverflow.com/questions/32912308/…,它说应该在没有明确调用 VisualStateManager.GoToState 的情况下触发它。

标签: uwp storyboard uwp-xaml visualstatemanager visualstategroup


【解决方案1】:

某些控件已定义视觉状态并在其实现中在它们之间切换,例如ButtonCheckbox。在这种情况下,这些状态列在文档like here 中。不幸的是,Grid 不是其中之一,所以如果你想在那里有类似的行为,你需要添加事件处理程序并手动使用GoToState。您还可以从 Grid 创建自定义派生类,以使此行为可重用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-05
    • 2019-08-10
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2018-10-14
    相关资源
    最近更新 更多