tags:

views:

2140

answers:

1

I have a 3D cube which I'm animating using a shared storyboard. The animation code is in the selectionChanged event of a combobox and is INTENDED to make sure that any animation that is still running be stopped before the next begins; but it's not working like that!

I realize this is some pretty messy code but i still don't see why my storyboard wont respond to control since I'm calling .begin(this,true).

Can someone tell me why i can't stop the StoryBoard? I'm still getting that dodgy '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';' message

        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  A: 

I believe you need to pass in cbo rather than this into the begin method.

this refers to the current class (I guess your window class) while it is the change in cbo that it is to control the animation.

John
No dice I'm afraid. Just tried but it still didn't work. Ah well!
Stimul8d
Just catching up on some old tickets and found you were indeed right...I just needed to remove the NameScope and RegisterName stuff.
Stimul8d