Tuesday, July 10, 2012

WPF Frame Navigation Animation : TranslateTransform and RotateTransform

RotateTransform

MainWindow.xaml

<Frame Name="FrameWithinGrid"
               ContentRendered="FrameWithinGrid_ContentRendered"             
               Source="Home.xaml">
            <Frame.RenderTransform>
              <RotateTransform Angle="0"
                               CenterX="425"
                               CenterY="300" />
            </Frame.RenderTransform>
</Frame>

MainWindow.xaml.cs

 
 private void FrameWithinGrid_ContentRendered(object sender, EventArgs e)
 {
    Storyboard storyboard = new Storyboard();
    DoubleAnimation rotateAnimation = new DoubleAnimation();
    rotateAnimation.Duration = TimeSpan.FromMilliseconds(1000);
    rotateAnimation.From = 0;
    rotateAnimation.To = 180;        
    storyboard.Children.Add(rotateAnimation);
    Storyboard.SetTargetProperty(rotateAnimation,
          new PropertyPath("RenderTransform.Angle"));
    Storyboard.SetTarget(rotateAnimation, FrameWithinGrid);
    storyboard.Begin(); 
 }
 
 
TranslateTransform

MainWindow.xaml

<Frame Name="FrameWithinGrid"
               ContentRendered="FrameWithinGrid_ContentRendered"             
               Source="Home.xaml">
            <Frame.RenderTransform>
                <TranslateTransform X="0"
                                    Y="0" />
            </Frame.RenderTransform>
</Frame>

MainWindow.xaml.cs

 
 private void FrameWithinGrid_ContentRendered(object sender, EventArgs e)
 {
    Storyboard storyboard = new Storyboard();
    DoubleAnimation transAnimation = new DoubleAnimation();
    transAnimation.Duration = TimeSpan.FromMilliseconds(500);
    transAnimation.From = 0;
    transAnimation.To = 800;
    storyboard.Children.Add(transAnimation);
    Storyboard.SetTargetProperty(transAnimation, new PropertyPath("RenderTransform.X"));
    //Storyboard.SetTargetProperty(transAnimation, new PropertyPath("RenderTransform.Y"));
    Storyboard.SetTarget(transAnimation, FrameWithinGrid);
    storyboard.Completed += new EventHandler(storyboard_Completed);
    storyboard.Begin();
 }
 void storyboard_Completed(object sender, EventArgs e)
 {
    Storyboard storyboard = new Storyboard();
    DoubleAnimation growAnimation = new DoubleAnimation();
    growAnimation.Duration = TimeSpan.FromMilliseconds(500);
    growAnimation.From = -800;
    growAnimation.To = 0;
    storyboard.Children.Add(growAnimation);

    Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.X"));
    //Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.Y"));
    Storyboard.SetTarget(growAnimation, FrameWithinGrid);        
    storyboard.Begin();
 } 

No comments:

Post a Comment