WPF - 无法停止动画 StoryBoard,IsControllable 不起作用?

Posted

技术标签:

【中文标题】WPF - 无法停止动画 StoryBoard,IsControllable 不起作用?【英文标题】:WPF - Can't stop an animating StoryBoard, IsControllable not working? 【发布时间】:2010-10-16 08:12:11 【问题描述】:

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

我意识到这是一些非常混乱的代码,但我仍然不明白为什么我的故事板不会响应控制,因为我正在调用 .begin(this,true)。

谁能告诉我为什么我不能停止 StoryBoard?我仍然收到那个狡猾的'System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='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;
        
    

【问题讨论】:

【参考方案1】:

我相信您需要将 cbo 而不是 this 传递给 begin 方法。

this指的是当前类(我猜是你的window类),而控制动画的是cbo中的变化。

【讨论】:

恐怕没有骰子。刚试过,但还是不行。嗯嗯! 刚赶上一些旧票,发现你确实是对的......我只需要删除 NameScope 和 RegisterName 的东西。

以上是关于WPF - 无法停止动画 StoryBoard,IsControllable 不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

WPF动画 - Loading加载动画

WPF Storyboard 动画播放完毕时触发的事件

WPF 内部Template 动画板 无法冻结此 Storyboard 时间线树供跨线程使用

WPF Storyboard播放多个DoubleAnimation出现卡顿

WPF 内部Template 动画板 无法冻结此 Storyboard 时间线树供跨线程使用

WPF 精修篇 路径动画