Skip to main content

Microsoft Silverlight

Setting Animation children in codeRSS Feed

(0)

hsabri
hsabri

Member

Member

16 points

11 Posts

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
hsabri

Member

Member

16 points

11 Posts

Re: Setting Animation children in code

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

Bill Reiss
Bill Reiss

Contributor

Contributor

4822 points

915 Posts

Silverlight MVP

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 3
My blog (rss feed)

hsabri
hsabri

Member

Member

16 points

11 Posts

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.   

Bill Reiss
Bill Reiss

Contributor

Contributor

4822 points

915 Posts

Silverlight MVP

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 3
My blog (rss feed)

hsabri
hsabri

Member

Member

16 points

11 Posts

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();

samsp
samsp

Member

Member

282 points

96 Posts

Microsoft

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

m3taverse
m3taverse

Member

Member

186 points

88 Posts

Re: Setting Animation children in code

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

Bill Reiss
Bill Reiss

Contributor

Contributor

4822 points

915 Posts

Silverlight MVP

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 3
My blog (rss feed)

samsp
samsp

Member

Member

282 points

96 Posts

Microsoft

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.

m3taverse
m3taverse

Member

Member

186 points

88 Posts

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.

tony_ser
tony_ser

Member

Member

6 points

12 Posts

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.

bpatters
bpatters

Member

Member

50 points

24 Posts

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
bpatters

Member

Member

50 points

24 Posts

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.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities