wpf - 以编程方式反转 Storyboard

标签 wpf storyboard

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

相关文章:

c# - 我应该以哪种方式定义资源字典以保持我的代码干净?

wpf - Hololens 模拟器中的 Project Centennial 应用程序

WPF MVVM ListBox SelectedItem 未突出显示

ios - Xcode 6.3 无法为我的 ViewControllers 选择类

wpf - 绑定(bind)到 DoubleAnimation.To

iOS 设计师不渲染 Storyboard

具有绑定(bind)的 WPF 可见性资源

c# - 如何将 TreeViewItems 添加到 TreeView 控件

Xcode 5 中的 iOS 6 导航栏颜色

ios - 如何使用 swift 将 UI 对象连接到 Xcode 6 中的 cocoa 类的导出