按钮开始故事板动画

时间:2013-01-23 21:52:59

标签: c# wpf xaml storyboard buttonclick

**编辑问题* *

Storyboard s = (Storyboard)TryFindResource("kupmove");
s.Stop();

这不会阻止正在进行的动画,我该如何阻止它?


如何通过visual c#中的按钮点击事件开始故事板?

以下是我用来开始故事板的代码:

    Storyboard s = (Storyboard)TryFindResource("kupmove");
    Storyboard.SetTargetName(s, "geometryModel3D");
    s.Begin();

动画未开始,我收到以下错误:

'Children' property value in the path '(0).(1)[2].(2)' points to immutable instance of 'System.Windows.Media.Media3D.Transform3DCollection'

这是我的xaml故事板:

<Storyboard x:Key="kupmove" 
            RepeatBehavior="Forever"
            AutoReverse="True">
  <Rotation3DAnimationUsingKeyFrames Storyboard.TargetProperty="(Model3D.Transform).(Transform3DGroup.Children)[2].(RotateTransform3D.Rotation)"
                                     Storyboard.TargetName="geometryModel3D">
    <EasingRotation3DKeyFrame KeyTime="0">
      <EasingRotation3DKeyFrame.EasingFunction>
        <CubicEase EasingMode="EaseInOut" />
      </EasingRotation3DKeyFrame.EasingFunction>
      <EasingRotation3DKeyFrame.Value>
        <AxisAngleRotation3D Axis="0,1,0"
                             Angle="0" />
      </EasingRotation3DKeyFrame.Value>
    </EasingRotation3DKeyFrame>
    <EasingRotation3DKeyFrame KeyTime="0:0:1">
      <EasingRotation3DKeyFrame.EasingFunction>
        <CubicEase EasingMode="EaseInOut" />
      </EasingRotation3DKeyFrame.EasingFunction>
      <EasingRotation3DKeyFrame.Value>
        <AxisAngleRotation3D Axis="0,1,0"
                             Angle="179" />
      </EasingRotation3DKeyFrame.Value>
    </EasingRotation3DKeyFrame>
    <EasingRotation3DKeyFrame KeyTime="0:0:2">
      <EasingRotation3DKeyFrame.EasingFunction>
        <CubicEase EasingMode="EaseInOut" />
      </EasingRotation3DKeyFrame.EasingFunction>
      <EasingRotation3DKeyFrame.Value>
        <AxisAngleRotation3D Axis="0,-1,0"
                             Angle="1" />
      </EasingRotation3DKeyFrame.Value>
    </EasingRotation3DKeyFrame>
    <EasingRotation3DKeyFrame KeyTime="0:0:3">
      <EasingRotation3DKeyFrame.EasingFunction>
        <CubicEase EasingMode="EaseInOut" />
      </EasingRotation3DKeyFrame.EasingFunction>
      <EasingRotation3DKeyFrame.Value>
        <AxisAngleRotation3D Axis="0.009,-0.014,1"
                             Angle="90.005" />
      </EasingRotation3DKeyFrame.Value>
    </EasingRotation3DKeyFrame>
  </Rotation3DAnimationUsingKeyFrames>


  <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Model3D.Transform).(Transform3DGroup.Children)[4].(TranslateTransform3D.OffsetX)"
                                 Storyboard.TargetName="geometryModel3D">
    <EasingDoubleKeyFrame KeyTime="0:0:2"
                          Value="0">
      <EasingDoubleKeyFrame.EasingFunction>
        <CubicEase EasingMode="EaseInOut" />
      </EasingDoubleKeyFrame.EasingFunction>
    </EasingDoubleKeyFrame>
    <EasingDoubleKeyFrame KeyTime="0:0:3"
                          Value="0">
      <EasingDoubleKeyFrame.EasingFunction>
        <CubicEase EasingMode="EaseInOut" />
      </EasingDoubleKeyFrame.EasingFunction>
    </EasingDoubleKeyFrame>
  </DoubleAnimationUsingKeyFrames>

</Storyboard>

*编辑*

建议的解决方案不起作用,实际上我没有错误的前一个代码触发故事板,它的工作原理但我认为因为已经有一个动画已经启动了以下代码,导致异常。告诉我一个在它运行时停止它的方法:

>    DoubleAnimation myAnimation = new DoubleAnimation();
>    myAnimation.From = 1;
>    myAnimation.To = 361;
>    myAnimation.Duration = new >Duration(TimeSpan.FromMilliseconds(1000));
>    myAnimation.RepeatBehavior = RepeatBehavior.Forever;
>    rotateTransform.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, myAnimation);
>    geometryModel3D.Transform = rotateTransform;

我想我必须停止这个第一站 - &gt; geometryModel3D.Transform = rotateTransform; 因为我想要触发的动画已经使用此代码运行。如何在使用xaml代码中声明的自己的参数重新启动故事板之前停止此操作。

  

使用BeginAnimation命令使用第二个参数null值停止geometryModel3D.Transform,没有帮助。第一个例外仍然存在。

我认为问题在这里开始:

Storyboard s = (Storyboard)TryFindResource("kupmove");
s.Stop();

此命令不会停止正在进行的动画。所以必定有一些错误,这就是s.Begin()没有触发的原因。

2 个答案:

答案 0 :(得分:1)

使用x:Name而不是x:Key并在该storyboard实例上调用Begin()方法。

答案 1 :(得分:0)

刚才,一位绅士发布了我读过的答案:

“尝试通过密钥调用故事板,即不使用TryFindResource()

也许您可以尝试使用它的实例化名称kupmove

来调用它

我只是读了这个,用户删除了他的答案。对不起,如果它没有帮助。

此外,正如上面发布的另一个用户使用x:Name而不是key来创建故事板的名称,然后使用该初始化名称调用它。