views:

162

answers:

2

I need to call a method when the animation gets to a certain keyframe. Is it possible to trigger an event when an animation gets to a certain keyframe? If not is there a better way of triggering an event at a certain time?

+3  A: 

Silverlight timelines are very limited when it comes to events. As far as I can tell, only the Completed event is supported. What you could do though is have two timelines inside a single storyboard where the second timeline is updating a bound property that you could watch.

Maybe something like:

<Storyboard>
    <DoubleAnimationusingKeyFrames ... />
    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="TriggerEvent">
        <ObjectKeyFrame KeyTime="00:00:01" Value="True" />
    <ObjectAnimationUsingKeyFrames>
</Storyboard>

Then in your code behind for the control, define a dependency property called TriggerEvent of type Boolean. When it changes to true, call your method.

Another option however, which is probably better actually, would be to split your original animation into two parallel timelines and hook up a Completed event handler to the first timeline (which you'd use to call your method) then on the second timeline, use the BeginTime property to synchronize the two animations so that the second one picks up just as the first one is completing.

<Storyboard>
    <!-- Timeline 1 -->
    <DoubleAnimationusingKeyFrames Completed="MyCompletedHandler" ... />
    <!-- Timeline 2 -->
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:01" ... />
</Storyboard>
Josh Einstein
+1 But I prefer the first option since its an approach that works with all types of KeyFrames (try getting second approach to work when the event is supposed to fire in the middle of an easing frame ). Also I'd use a string property not a boolean this would be more flexiable.
AnthonyWJones
A: 

I'm new to WPF. Do you have an example of the C# behind "define a dependency property called TriggerEvent of type Boolean. When it changes to true, call your method." ?

Thanks

Yko