Thursday, July 9, 2009

Minimum Code for Silverlight Rotation Animation

This is the minimum code for animate rotating a Silverlight element (a variable named Element):
  1. Element.RenderTransformOrigin = new Point(0.5, 0.5);  
  2. Element.RenderTransform = new RotateTransform();  
  3. DoubleAnimation da = new DoubleAnimation  
  4. {  
  5.   From = 0,  
  6.   To = 360,  
  7.   Duration = TimeSpan.FromSeconds(0.5)  
  8. };  
  9. Storyboard.SetTarget(da, Element.RenderTransform);  
  10. Storyboard.SetTargetProperty(da, new PropertyPath(RotateTransform.AngleProperty));  
  11. Storyboard sb = new Storyboard();  
  12. sb.Children.Add(da);  
  13. sb.Begin();  

4 comments:

  1. I needed to move an object, but using your code it was super easy! Been to about 20 sites before I stumbled on this one -- well done sir!

    ReplyDelete
  2. I totally agree. I had to search way to hard to find this simple example. Thanks!

    ReplyDelete
  3. Sensational...had it working in seconds!

    ReplyDelete