Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Setting Animation children in code RSS

13 replies

Last post May 31, 2007 09:33 PM by bpatters

(0)
  • hsabri

    hsabri

    Member

    16 Points

    11 Posts

    Setting Animation children in code

    May 08, 2007 03:58 AM | LINK

    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

    16 Points

    11 Posts

    Re: Setting Animation children in code

    May 08, 2007 04:16 AM | LINK

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

  • Bill Reiss

    Bill Reiss

    Contributor

    4973 Points

    947 Posts

    Re: Setting Animation children in code

    May 08, 2007 11:34 AM | LINK

    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
    My blog
  • hsabri

    hsabri

    Member

    16 Points

    11 Posts

    Re: Setting Animation children in code

    May 08, 2007 04:23 PM | LINK

    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

    4973 Points

    947 Posts

    Re: Setting Animation children in code

    May 08, 2007 09:27 PM | LINK

    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
    My blog
  • hsabri

    hsabri

    Member

    16 Points

    11 Posts

    Re: Setting Animation children in code

    May 08, 2007 10:11 PM | LINK

    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

    282 Points

    96 Posts

    Microsoft

    Re: Setting Animation children in code

    May 09, 2007 05:49 PM | LINK

    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

    186 Points

    88 Posts

    Re: Setting Animation children in code

    May 11, 2007 07:29 AM | LINK

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

  • Bill Reiss

    Bill Reiss

    Contributor

    4973 Points

    947 Posts

    Re: Setting Animation children in code

    May 11, 2007 01:46 PM | LINK

    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
    My blog
  • samsp

    samsp

    Member

    282 Points

    96 Posts

    Microsoft

    Re: Setting Animation children in code

    May 11, 2007 08:44 PM | LINK

    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.