views:

863

answers:

1

I have a simple WPF application that contains a user control that is animated in and out by a pair of storyboards on the main window. I am animating the user control's opacity, scaletransform.scalex and scaletransform.scaley to pop it in and out of view. It looks cool except for the first time it runs. The animation is set to take 3/10 of a second, though I have increased this during my testing. I've tried setting a default value at a fraction of a second in to see if there is a problem where the values of the properties I'm animating aren't defaulting as expected. That did nothing. I even explicitly call my "hide" animation on window loaded to see if this would help me set my default values, but that didn't work either. Is there some initialization that the runtime is doing the first time I fire the animation? Is there a way I can explicitly call that kind of initialization before showing the UI? The symptom is that the control isn't even visible until about 2/10 of a second into the first animation. Sometimes it's longer and the control shows up after it's been fully animated (opacity and size are their final values when it finally shows up).

+1  A: 

It sounds like you're setting the DataContext of the UserControl right before the animation. Presumably you have binding going on, which is creating visual objects and adding them to the visual tree at the same time the animation is supposed to start. This requires processing that the animation is fighting for. The way to avoid this is to have the control instantiated (with DataContext set), but hidden, so the visual object creation does not need to be performed at the time of animation. (via my co-worker Tim Lee)

Jon Galloway
Okay, you're absolutely right...and this was my initial hunch as well. However, the runtime is trying to be too smart for it's own good. If I try any which way to hide it from view (opacity, negative margin, scaletransform, translatetransform), it does the same thing. If I allow it to be viewable, I don't even need to set the DataContext first...the first animation goes smoothly. It's almost as if I need to trick the runtime into thinking it's viewable when it's actually not. That, or I need to know how to explicitly tell it to build the visual tree. The latter feels less hacky.
Rich