WPF-Can';t停止动画故事板,是否可控制不工作?

WPF-Can';t停止动画故事板,是否可控制不工作?,wpf,storyboard,Wpf,Storyboard,我有一个3D立方体,我正在使用一个共享的故事板制作动画。动画代码位于组合框的selectionChanged事件中,用于确保在下一次开始之前停止任何仍在运行的动画;但它不是那样工作的 我意识到这是一段相当混乱的代码,但我仍然不明白为什么我的故事板在我打电话时不会响应控件 有人能告诉我为什么我不能停止故事板吗?我仍然收到狡猾的'System.Windows.Media.Animation警告:6:无法执行操作,因为指定的情节提要从未应用于此对象以进行交互控制。;动作class='Stop';'消息

我有一个3D立方体,我正在使用一个共享的故事板制作动画。动画代码位于组合框的selectionChanged事件中,用于确保在下一次开始之前停止任何仍在运行的动画;但它不是那样工作的

我意识到这是一段相当混乱的代码,但我仍然不明白为什么我的故事板在我打电话时不会响应控件

有人能告诉我为什么我不能停止故事板吗?我仍然收到狡猾的
'System.Windows.Media.Animation警告:6:无法执行操作,因为指定的情节提要从未应用于此对象以进行交互控制。;动作class='Stop';'消息

        Storyboard sb = new Storyboard();
    DoubleAnimation forward90 = new DoubleAnimation(0,90,TimeSpan.FromMilliseconds(2000));
    DoubleAnimation back90 = new DoubleAnimation(0,-90, TimeSpan.FromMilliseconds(2000));

private void cbo_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        forward90.BeginTime = TimeSpan.Zero;
        back90.BeginTime = TimeSpan.Zero;

        NameScope.SetNameScope(this, new NameScope());

        RegisterName(this.Name, this);

        sb.Stop(this);
        sb.Remove(this);

        sb.Children.Clear();

        sb.AccelerationRatio = 0;
        sb.DecelerationRatio = 1;
        sb.RepeatBehavior = RepeatBehavior.Forever;

        int i = cbo.SelectedIndex;
        Orientation o = (Orientation)i;

        ViewModel vm = this.DataContext as ViewModel;
        if(vm !=null)vm.Orient = o;


        switch (o)
        {
            case Orientation.Front0:

                break;
            case Orientation.Front90:

                sb.Children.Add(forward90);

                Storyboard.SetTarget(forward90, cube2);

                Storyboard.SetTargetProperty(forward90, new PropertyPath(CubeControl.CubeControl.XRotationProperty));
                sb.Begin(this, true);

                break;
            case Orientation.Right0:

                sb.Children.Add(back90);

                Storyboard.SetTarget(back90, cube2);

                Storyboard.SetTargetProperty(back90, new PropertyPath(CubeControl.CubeControl.YRotationProperty));
                sb.Begin(this, true);

                break;
            case Orientation.Right90:

                back90.BeginTime = TimeSpan.FromMilliseconds(2000);

                sb.Children.Add(forward90);
                sb.Children.Add(back90);

                Storyboard.SetTarget(back90, cube2);
                Storyboard.SetTarget(forward90, cube2);

                Storyboard.SetTargetProperty(forward90, new PropertyPath(CubeControl.CubeControl.YRotationProperty));
                Storyboard.SetTargetProperty(back90, new PropertyPath(CubeControl.CubeControl.ZRotationProperty));

                sb.Begin(this, true);

                break;
            case Orientation.Top0:

                sb.Children.Add(back90);

                Storyboard.SetTarget(back90, cube2);
                Storyboard.SetTargetProperty(back90, new PropertyPath(CubeControl.CubeControl.ZRotationProperty));
                sb.Begin(this, true);

                break;
            case Orientation.Top90:

                back90.BeginTime = TimeSpan.FromMilliseconds(2000);

                sb.Children.Add(forward90);
                sb.Children.Add(back90);

                Storyboard.SetTarget(forward90, cube2);
                Storyboard.SetTarget(back90, cube2);

                Storyboard.SetTargetProperty(forward90, new PropertyPath(CubeControl.CubeControl.XRotationProperty));
                Storyboard.SetTargetProperty(back90, new PropertyPath(CubeControl.CubeControl.ZRotationProperty));

                sb.Begin(this, true);
                break;
            default:
                break;
        }
    }
}

我相信你需要通过cbo而不是begin方法


这指的是当前类(我猜是您的窗口类),而控制动画的是cbo中的更改。

恐怕没有骰子。刚试过,但还是不行。好吧!我只是补上了一些旧票,发现你确实是对的……我只需要删除名称范围和注册名称的东西。