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

标签 wpf storyboard

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

我意识到这是一些非常困惑的代码,但我仍然不明白为什么我的 Storyboard不会响应控制,因为我正在调用 .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;
        }
    }
}

最佳答案

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

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

关于WPF - 无法停止动画 StoryBoard,IsControllable 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/696534/

相关文章:

wpf - 是否有适用于 .NET 3.5 SP1 和 WPF 的 CLR 分析器?

ios - UIScrollView 和 Storyboard

ios - 如何缩放 iOs 5+ 的 UI

wpf - 排序不适用于 WPF DataGrid 模板化列?

c# - 在 WPF 文本框上调用 Select() 不执行任何操作

c# - 当 Pen 具有 Null 以外的 DashStyle 时,StreamGeometry 不会被栅格化

ios - 如何使用segues创建一个uiview,它被storyboard中的多个UIViewControllers使用

Swift Storyboard错误

iphone - 无法设置委托(delegate)和调用方法

c# - 在新窗口中打开新窗口