Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Setting Animation children in code
13 replies. Latest Post by bpatters on May 31, 2007.
(0)
hsabri
Member
16 points
11 Posts
05-07-2007 11:58 PM |
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();
05-08-2007 12:16 AM |
just to add. the error that i receive is "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))"
Bill Reiss
Contributor
4822 points
915 Posts
05-08-2007 7:34 AM |
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.
05-08-2007 12:23 PM |
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.
05-08-2007 5:27 PM |
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.
05-08-2007 6:11 PM |
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
282 points
96 Posts
05-09-2007 1:49 PM |
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:
Sam
m3taverse
186 points
88 Posts
05-11-2007 3:29 AM |
I abused this thread and made this :) Source included.
05-11-2007 9:46 AM |
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...
05-11-2007 4:44 PM |
Sweet. Glad to see you also included cleanup code to remove the storyboard once the animation is complete.
05-11-2007 5:41 PM |
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
6 points
12 Posts
05-15-2007 5:40 PM |
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
50 points
24 Posts
05-31-2007 5:33 PM |
I had this same problem and found a work around:
Call Stop() on your storyboard before chaning the properties.
Call Stop() on your storyboard before changing the properties.