Page view counter
Setting Animation children in code
Last post 05-31-2007 5:33 PM by bpatters. 13 replies.
Sort Posts:
05-07-2007 11:58 PM
Setting Animation children in code

So i'm trying to create animation on the fly in C#, but i'm running into a funny bug when i try to set the Children property on the Storyboard object.  The API claims that the Children property is of type TimelineCollection. However, the intellicense begs to differ and exposes it simply as one Timeline object instead.  That is weird.  The second weird thing is that i can not seem to set that property even if i wanted to.  It simply runs into that code, and disappears into code oblivion. An exception of some sort is thrown, but nothing that i can recognize. I'd also like to be able to set the Storyboard.TargetName property on the fly; again, that seems to bomb out.  Could someone help me here.  I have pasted the code below. The conflict spots are in bold...Thank you,

 

Haider 

 

Storyboard xSB = new Storyboard();
            TimelineCollection timeLines = new TimelineCollection();
            KeyFrameCollection keyFramez;
            DoubleAnimationUsingKeyFrames anim;
           
            //1st Animation
            anim = new DoubleAnimationUsingKeyFrames();
            keyFramez = new KeyFrameCollection();
            anim.SetValue<string>(Storyboard.TargetPropertyProperty, "(FrameworkElement.Width)");
            SplineDoubleKeyFrame xSDKeyFrame = new SplineDoubleKeyFrame();
            xSDKeyFrame.Value = 130;
            xSDKeyFrame.KeyTime = TimeSpan.FromSeconds(0.1);
            keyFramez.Add(xSDKeyFrame);
            anim.KeyFrames = keyFramez;
            timeLines.Add(anim);
            xSB.Children = anim;
            try
            {
                xSB.SetValue<Timeline>(Storyboard.ChildrenProperty, timeLines[0]);
                xSB.SetValue<string>(Storyboard.TargetNameProperty, ((Image)sender).Name);

            }
            catch (Exception ex)
            {

                throw ex;
            }
           
            xSB.Begin();

hsabri

Loading...
Joined on 11-10-2006
Posts 11
05-08-2007 12:16 AM
Re: Setting Animation children in code

 just to add. the error that i receive is "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))"

hsabri

Loading...
Joined on 11-10-2006
Posts 11
05-08-2007 7:34 AM
Re: Setting Animation children in code

I wouldn't create my own timelineCollection, I think this is created for you when you create a storyboard object. Change xSB.Children = anim to xSB.Children.Add(anim); and see if that helps.


Bill Reiss, Coauthor of Hello! Silverlight 2
My blog (rss feed)

Bill Reiss

Loading...
Joined on 05-01-2007
Tampa, FL
Posts 848
05-08-2007 12:23 PM
Re: Setting Animation children in code

Hi Bill...Yeah, I would agree with you in doing in that way, however, the xSB.Children property is NOT a TimelineCollection as advertised.  Try compiling that code, and it won't work.   

hsabri

Loading...
Joined on 11-10-2006
Posts 11
05-08-2007 5:27 PM
Re: Setting Animation children in code

Hmm yeah I see what you mean...I've written some code where I set properties dynamically of the child animations in a storyboard (and children) laid out in XAML but I hadn't tried to create a storyboard in code. I don't see how you would do it, so I'm interested to see if it's even possible right now.


Bill Reiss, Coauthor of Hello! Silverlight 2
My blog (rss feed)

Bill Reiss

Loading...
Joined on 05-01-2007
Tampa, FL
Posts 848
05-08-2007 6:11 PM
Re: Setting Animation children in code

hi bill. i am able to create the animation in code, but it can only have one Timeline (as opposed to multiple).  As of now, this is what i got working:

 Storyboard xSB = new Storyboard();
                TimelineCollection timeLines = new TimelineCollection();
                KeyFrameCollection keyFramez;
                DoubleAnimationUsingKeyFrames anim;

                //1st Animation
                anim = new DoubleAnimationUsingKeyFrames();
                keyFramez = new KeyFrameCollection();

                SplineDoubleKeyFrame xSDKeyFrame = new SplineDoubleKeyFrame();
                xSDKeyFrame.Value = 130;
                xSDKeyFrame.KeyTime = TimeSpan.FromMilliseconds(100);
                anim.KeyFrames.Add(xSDKeyFrame);
                anim.BeginTime = new TimeSpan(0, 0, 0);
                anim.SetValue<string>(Storyboard.TargetPropertyProperty, "Width");
                anim.SetValue<string>(Storyboard.TargetNameProperty, ((Image)sender).Name);

                try
                {
                    
                    xSB.Children = anim;
                }
                catch (Exception ex)
                {

                    throw ex;
                }
                this.Resources.Add(xSB);
                xSB.Begin();

hsabri

Loading...
Joined on 11-10-2006
Posts 11
05-09-2007 1:49 PM
Re: Setting Animation children in code

Another trick that is quite handy is to create you animations declaratively in xaml, and then use xamlreader.load to initialize the xaml. You can then use blend to define the animations, copy them to code (replace the " with '), and then use string.format to substitute in values that you want. For example:

string sbdef = @"<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime='00:00:00' Storyboard.TargetName='text1' Storyboard.TargetProperty='(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)'>
<SplineDoubleKeyFrame KeyTime='00:00:00.5000000' Value='{0}'/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>"
;
double angle=0;
void Clicked(object sender, EventArgs e)
{
angle +=90;
Storyboard sb= (Storyboard) XamlReader.Load(String.Format(sbdef, angle));
this.Resources.Add (sb);
sb.Begin();
}

Sam

samsp

Loading...
Joined on 08-08-2002
Posts 96
05-11-2007 3:29 AM
Re: Setting Animation children in code

I abused this thread and made this :)    Source included.

m3taverse

Loading...
Joined on 05-01-2007
Posts 83
05-11-2007 9:46 AM
Re: Setting Animation children in code

m3taverse:

I abused this thread and made this :)    Source included.

Nice sample, thanks! Now I need to go rewrite my particle code to work like this...


Bill Reiss, Coauthor of Hello! Silverlight 2
My blog (rss feed)

Bill Reiss

Loading...
Joined on 05-01-2007
Tampa, FL
Posts 848
05-11-2007 4:44 PM
Re: Setting Animation children in code

m3taverse:

I abused this thread and made this :)    Source included.

Sweet. Glad to see you also included cleanup code to remove the storyboard once the animation is complete.

samsp

Loading...
Joined on 08-08-2002
Posts 96
05-11-2007 5:41 PM
Re: Setting Animation children in code

samsp:

m3taverse:

I abused this thread and made this :)    Source included.

Sweet. Glad to see you also included cleanup code to remove the storyboard once the animation is complete.

Had to, cos it stops working rather quickly if I don't remove the elements after they've completed their animation.

m3taverse

Loading...
Joined on 05-01-2007
Posts 83
05-15-2007 5:40 PM
Re: Setting Animation children in code

Nice example.

However, it does not address the original problem.

I created the StoryBoard with Blend and I could trigger it with code. But whenever I try to change the Storyboard.TargetNameProperty of the DoubleAnimationUsingKeyFrames using SetValue, I get the " Catastrophic failure" the second time I call SetValue.

tony_ser

Loading...
Joined on 04-30-2007
Posts 12
05-31-2007 5:33 PM
Re: Setting Animation children in code

I had this same problem and found a work around:

Call Stop() on your storyboard before chaning the properties.

bpatters

Loading...
Joined on 05-16-2007
Posts 24
05-31-2007 5:33 PM
Re: Setting Animation children in code

I had this same problem and found a work around:

Call Stop() on your storyboard before changing the properties.

bpatters

Loading...
Joined on 05-16-2007
Posts 24
Microsoft Communities