WP7 - Animation ends too early and no effect remains
I made an animation to rotate my control, but when it's trigert it just jumps to final position and then immediately returns to starting position.
The Controls are made from code behind and are TextBoxes with following properties:
FontSize = 45;
TextAlignment = TextAlignment.Center;
Widh = 40;
Heigh = 45;
I store the TextBoxes in array, so I trigger the animation with following code:
foreach (TextBlock b in arrayOfTextBoxes)
{
Rotate(b);
}
Animation:
public static void Rotate(UIElement target)
{
var projection = new PlaneProjection();
target.Projection = projection;
DoubleAnimation da = new DoubleAnimation();
da.From = 0;
da.To = 90;
da.Duration = TimeSpan.FromSeconds(0.25);
da.AutoReverse = false;
Storyboard.SetTargetProperty(da, new PropertyPath(PlaneProjection.RotationZProperty));
Storyboard.SetTarget(da, projection);
Storyboard sb = new Storyboard();
sb.Children.Add(da);
EventHandler eh = null;
eh = (s, args) =>
{
project开发者_如何学Cion.RotationZ = 90;
sb.Stop();
sb.Completed -= eh;
};
sb.Completed += eh;
sb.Begin();
}
EDIT: Now i propably know what's the problem. I have one (sometimes two) Dispatcher Times ticking in background every second. When I disable them, the proble's gone. But I can't do it because I need them to measure time.
Stoping them just before I trigger the animation and them starting them again does'n help.
//The DispetcherTimers do this:
void tikac_Tick(object sender, EventArgs e)
{
herniCas += new TimeSpan(0, 0, 1);
}
Animation storyboards will normally hold the last value "until they are stopped". They do not permanently change the actual property value. They just change its currently rendered value (aren't dependency properties wonderful? :))
As you are explicitly stopping it when it ends the value reverts to its original value. Do not stop the storyboard if you want the value to stay changed, or explicitly set it in code when the animation changes.
The animation in your example is so fast it is just ending very quickly (startup time for storyboards can be a few frames and any time in between is skipped as animations are fixed-time interpolations, not frame specific).
精彩评论