Answered by:
Silverligth animation from code

Question
-
Hi!
I was wondering if anyone can help me with my program.
Rectangle rec = new Rectangle(); rec.Width = 50; rec.Height = 50; SolidColorBrush myBrush = new SolidColorBrush(Colors.Blue); rec.Fill = myBrush; rec.Margin = new Thickness(0, 0, 0, 0); LayoutRoot.Children.Add(rec); Storyboard story = new Storyboard(); DoubleAnimation animateX = new DoubleAnimation(); animateX.To = 5; animateX.From = 0; animateX.Duration = new Duration(new TimeSpan(0, 0, 2)); Storyboard.SetTarget(animateX, rec.RenderTransform); Storyboard.SetTargetProperty(animateX, new PropertyPath(TranslateTransform.XProperty)); story.Children.Add(animateX); story.Begin();
If i run this code, it does not give any error, but do nothing at all, just show the recangle, apart from show the full animation. I would be really thankful if anyone can help me solve this.
Best regards.
Friday, March 2, 2012 6:02 PM
Answers
-
Is this in a Silverlight application, or Silverlight code you're trying to port to a Windows 8 Metro-style app?
This is likely because you haven't created the TranslateTransform. If you have exceptions turned on, you should get an error that says something like "Cannot resolve TargetProperty TranslateTransform.X".
If you apply a TranslateTransform to the rectangle, e.g.:
rec.RenderTransform = new TranslateTransform();
then this should work. You will also likely have to change the parameters passed to Storyboard.SetTargetProperty, which take a String instead of a PropertyPath:
Storyboard.SetTargetProperty(animateX, "TranslateTransform.X");
- Proposed as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Saturday, March 3, 2012 1:22 AM
- Marked as answer by Jie Bao Saturday, April 7, 2012 1:41 PM
Friday, March 2, 2012 10:56 PM -
Jesse's advice should get you fixed up if you're writing a Metro style app. You might also want to consider using transition or theme animations instead of custom animations. See Quickstart: Animating your UI for an overview of what is available and a discussion of differences between animations from Silverlight.
If you are writing a Silverlight app then please post on the silverlight.net forums.
- Marked as answer by Jie Bao Saturday, April 7, 2012 1:41 PM
Saturday, March 3, 2012 1:29 AMModerator
All replies
-
Is this in a Silverlight application, or Silverlight code you're trying to port to a Windows 8 Metro-style app?
This is likely because you haven't created the TranslateTransform. If you have exceptions turned on, you should get an error that says something like "Cannot resolve TargetProperty TranslateTransform.X".
If you apply a TranslateTransform to the rectangle, e.g.:
rec.RenderTransform = new TranslateTransform();
then this should work. You will also likely have to change the parameters passed to Storyboard.SetTargetProperty, which take a String instead of a PropertyPath:
Storyboard.SetTargetProperty(animateX, "TranslateTransform.X");
- Proposed as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Saturday, March 3, 2012 1:22 AM
- Marked as answer by Jie Bao Saturday, April 7, 2012 1:41 PM
Friday, March 2, 2012 10:56 PM -
Jesse's advice should get you fixed up if you're writing a Metro style app. You might also want to consider using transition or theme animations instead of custom animations. See Quickstart: Animating your UI for an overview of what is available and a discussion of differences between animations from Silverlight.
If you are writing a Silverlight app then please post on the silverlight.net forums.
- Marked as answer by Jie Bao Saturday, April 7, 2012 1:41 PM
Saturday, March 3, 2012 1:29 AMModerator -
Thanks for the help!Saturday, March 3, 2012 7:49 AM