How to trigger event in silverlight animation by keyframe?
I need to call a method when the animation gets to a certain keyframe. Is i开发者_StackOverflow社区t 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?
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>
精彩评论