Mega Code Archive

 
Categories / Silverlight / Animations
 

Use DoubleUsingKeyframes to control animation

<UserControl x:Class='SilverlightApplication3.MainPage'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'      xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     xmlns:d='http://schemas.microsoft.com/expression/blend/2008'      xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'      mc:Ignorable='d'      d:DesignWidth='640' d:DesignHeight='480'>     <Canvas x:Name="LayoutRoot" Background="White" >         <Rectangle Height="122" Width="248" Canvas.Left="59" Canvas.Top="153" Fill="#FF434343" RadiusY="13" RadiusX="13" RenderTransformOrigin="0.5,0.5" x:Name="Rectangle">             <Rectangle.RenderTransform>                 <TransformGroup>                     <ScaleTransform/>                     <SkewTransform/>                     <RotateTransform/>                     <TranslateTransform/>                 </TransformGroup>             </Rectangle.RenderTransform>         </Rectangle>     </Canvas> </UserControl> //File: Page.xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SilverlightApplication3 {     public partial class MainPage : UserControl     {         private Storyboard MoveRight = new Storyboard();         public MainPage()         {             InitializeComponent();             DoubleAnimationUsingKeyFrames Anim = new DoubleAnimationUsingKeyFrames();             Storyboard.SetTargetName(Anim, "Rectangle");             Anim.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"));             Anim.BeginTime = new TimeSpan(0, 0, 0);             SplineDoubleKeyFrame SKeyFrame = new SplineDoubleKeyFrame();             SKeyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5));             SKeyFrame.Value = 150;             Anim.KeyFrames.Add(SKeyFrame);             MoveRight.Children.Add(Anim);             DoubleAnimationUsingKeyFrames YAnim = new DoubleAnimationUsingKeyFrames();             Storyboard.SetTargetName(YAnim, "Rectangle");             YAnim.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"));             YAnim.BeginTime = new TimeSpan(0, 0, 0);             SplineDoubleKeyFrame SKeyFrame1 = new SplineDoubleKeyFrame();             SKeyFrame1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5));             SKeyFrame1.Value = 150;             YAnim.KeyFrames.Add(SKeyFrame1);             MoveRight.Children.Add(YAnim);             MoveRight.FillBehavior = FillBehavior.Stop;             LayoutRoot.Resources.Add("MoveRight", MoveRight);             Rectangle.MouseEnter += new MouseEventHandler(Rectangle_MouseEnter);         }         private void Rectangle_MouseEnter(object sender, MouseEventArgs e)         {             MoveRight.Begin();         }     } }