gpt4 book ai didi

wpf - 以编程方式反转 Storyboard

转载 作者:行者123 更新时间:2023-12-04 07:00:29 24 4
gpt4 key购买 nike

我有以下 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com