我有以下 Storyboard:
<Window.Resources>
<Storyboard x:Key="ButtonsAnim">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="topRightButton" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="-100"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="topRightButton" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="100"/>
...
它基本上在 Canvas 中移动一些按钮。
这是启动动画的代码:
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Storyboard sb = (Storyboard)Resources["ButtonsAnim"];
storyBoard = sb;
storyBoard.Begin(this, true);
}
我想要做的是当我单击隐藏窗口的按钮时重置动画。当窗口重新出现时,动画应从头开始。
当应用程序重新出现时,我尝试使用storyBoard.Begin(this, true),但在最初的几毫秒内,按钮位于最后的位置。
然后我在隐藏窗口之前尝试了storyBoard.seek(TimeSpan.Zero),但失败了:
System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='Seek'; Storyboard='System.Windows.Media.Animation.Storyboard'; Storyboard.HashCode='24901833'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='System.Windows.Media.Animation.Storyboard'; TargetElement.HashCode='24901833'; TargetElement.Type='System.Windows.Media.Animation.Storyboard'
在隐藏窗口之前我也尝试过storyBoard.remove(this),效果相同:按钮位于最后的位置。
有什么想法吗?
谢谢。
最佳答案
要以这种方式使用 StoryBoard.Remove()
- 您应该保留对 Storyboard 对象的引用。
像这样:
Storyboard myStoryBoard;
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
myStoryBoard = (Storyboard)Resources["myStoryBoard"];
myStoryBoard.Begin();
}
void sbRemoveEvent()
{
myStoryBoard.Remove();
}
关于wpf - 以编程方式反转 Storyboard ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1938021/