为什么Canvas.SetTop动画无法正常工作?

时间:2012-03-17 20:39:25

标签: wpf vb.net animation

我有这个xaml:

    <Canvas Width="75" Height="75">
        <Button x:Name="button" Background="Olive" Canvas.Left="0" Canvas.Top="0" Width="75" Height="75" Click="button_Click"/>
    </Canvas>

这个代码背后:

    Private Sub button_Click(ByVal sender as Object, ByVal e as System.Windows.RoutedEventArgs)
        Canvas.SetTop(sender, -75)

        Dim sb1 As New Storyboard
        Dim da1 As New DoubleAnimationUsingKeyFrames
        da1.BeginTime = TimeSpan.FromSeconds(0)
        Storyboard.SetTargetName(da1, CType(sender, Button).Name)
        Storyboard.SetTargetProperty(da1, New PropertyPath(Canvas.TopProperty))

        Dim t1 As Double = Canvas.GetTop(sender)
        da1.KeyFrames.Add(New SplineDoubleKeyFrame(t1 + 75, TimeSpan.FromSeconds(0.2)))
        sb1.Children.Add(da1)

        BeginStoryboard(sb1)        
    End Sub

当我第一次单击该按钮时,它正常上升75并动画回到0,但是当我再次单击该按钮时,它只会向下动画75.为什么它会跳过Canvas.SetTop行并继续直接到动画部分?以及如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

在Canvas.SetTop

之前使用此代码(它在C#中)
(sender as UIElement).BeginAnimation(Canvas.TopProperty, null);

这将避免任何动画覆盖值Canvas.TopProperty

此链接可以帮助您

http://joshsmithonwpf.wordpress.com/2008/08/21/removing-the-value-applied-by-an-animation/