Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i want to create animation in windows phone i can able to create animation through Expression blend but i want to create animation through c# code but i have tried a lot but its not working here i want to move a rectange from right end to left end only in x coordinate but it was rasing an error please help me

C#
Rectangle r = new Rectangle();
           r.Fill = new SolidColorBrush(Colors.Purple);
           r.Width = 100;
           r.Height = 100;
           r.Name = "rectange";
           LayoutRoot.Children.Add(r);
           Storyboard sb = new Storyboard();
           DoubleAnimation fadeInAnimation = new DoubleAnimation();
           fadeInAnimation.From = 340;
           fadeInAnimation.To = 0;
           fadeInAnimation.Duration = new Duration(TimeSpan.FromSeconds(1.0));
           Storyboard.SetTarget(fadeInAnimation, r);
           Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateX)"));
           sb.Children.Add(fadeInAnimation);
           sb.Begin();

it was raising error at sb.begin();
Posted
Comments
venkatesh konatham 20-Jul-13 6:20am    
ohhhh sry i have missed the line

r .RenderTransform= new CompositeTransform();

1 solution

Please try this code


// Create a red rectangle that will be the target
// of the animation.
Rectangle myRectangle = new Rectangle();
myRectangle.Width = 200;
myRectangle.Height = 200;
Windows.UI.Color myColor = Windows.UI.ColorHelper.FromArgb(255, 255, 0, 0);
SolidColorBrush myBrush = new SolidColorBrush();
myBrush.Color = myColor;
myRectangle.Fill = myBrush;

// Create the transform
TranslateTransform moveTransform = new TranslateTransform();
moveTransform.X = 0;
moveTransform.Y = 0;
myRectangle.RenderTransform = moveTransform;


// Add the rectangle to the tree.
LayoutRoot.Children.Add(myRectangle);
// Create a duration of 2 seconds.
Duration duration = new Duration(TimeSpan.FromSeconds(1.0));
// Create two DoubleAnimations and set their properties.
DoubleAnimation myDoubleAnimationX = new DoubleAnimation();
Storyboard sb = new Storyboard();
sb.Duration = duration;
sb.Children.Add(myDoubleAnimationX);
Storyboard.SetTarget(myDoubleAnimationX, moveTransform);
Storyboard.SetTargetProperty(myDoubleAnimationX, "X");

myDoubleAnimationX.To = 0;
myDoubleAnimationX.From = 340;

// Make the Storyboard a resource.
LayoutRoot.Resources.Add("unique_id", sb);

// Begin the animation.
sb.Begin();


Thanks,
Bilaal
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900