Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Creating and Reusing Dynamic Animations in Silverlight

, 5 Sep 2008
Creating dynamic animations, and a simple way to reuse them to reduce the XAML code size.
using System;
using System.Collections.Generic;
using System.Linq;
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 AgDynAnimations
{
  public partial class EffectsPage : UserControl
  {
    DynamicEffect effect;

    public EffectsPage()
    {
      InitializeComponent();

      fadeInEffect.MouseLeftButtonDown += new MouseButtonEventHandler(fadeInEffect_MouseLeftButtonDown);
      
      fadeOutEffect.MouseLeftButtonDown += new MouseButtonEventHandler(fadeOutEffect_MouseLeftButtonDown);
      fadeOut.Completed += new EventHandler(fadeOut_Completed);

      growEffect.MouseLeftButtonDown += new MouseButtonEventHandler(growEffect_MouseLeftButtonDown);
      grow.Completed += new EventHandler(grow_Completed);

      shrinkEffect.MouseLeftButtonDown += new MouseButtonEventHandler(shrinkEffect_MouseLeftButtonDown);
      shrink.Completed += new EventHandler(shrink_Completed);

      rotateEffect.MouseLeftButtonDown += new MouseButtonEventHandler(rotateEffect_MouseLeftButtonDown);

      expandEffect.MouseLeftButtonDown += new MouseButtonEventHandler(expandEffect_MouseLeftButtonDown);

      collapseEffect.MouseLeftButtonDown += new MouseButtonEventHandler(collapseEffect_MouseLeftButtonDown);
      collapse.Completed += new EventHandler(collapse_Completed);

      dynamicEffect.MouseLeftButtonDown += new MouseButtonEventHandler(dynamicEffect_MouseLeftButtonDown);
    }

    void ResetScale(FrameworkElement target)
    {
      TransformGroup transform = (TransformGroup)target.RenderTransform;
      ScaleTransform scale = (ScaleTransform)transform.Children[0];
      scale.ScaleX = 1.0;
      scale.ScaleY = 1.0;
    }
    void ResetClip(FrameworkElement target)
    {
      PathGeometry path = (PathGeometry)target.Clip;
      PathFigure figure = (PathFigure)path.Figures[0];
      
      LineSegment first = (LineSegment)figure.Segments[0];
      first.Point = new Point(60, 0);

      LineSegment second = (LineSegment)figure.Segments[1];
      second.Point = new Point(60, 50);

      LineSegment third = (LineSegment)figure.Segments[2];
      third.Point = new Point(0, 50);
    }
    void ApplyDynamicEffect()
    {
      if (effect != null)
      {
        effect.Stop();
        effect = null;
      }

      if (chkFadeIn.IsChecked.Value)
        effect = new FadeInEffect();
      else
        effect = new FadeOutEffect();

      effect.Completed += new EventHandler(effect_Completed);
      effect.Start(dynamicEffect);
    }

    void effect_Completed(object sender, EventArgs e)
    {
      effect = null;
      dynamicEffect.Opacity = 1.0;
    }

    void fadeInEffect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      fadeIn.Begin();
    }
    void fadeOutEffect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      fadeOut.Begin();
    }
    void fadeOut_Completed(object sender, EventArgs e)
    {
      fadeOutEffect.Opacity = 1.0;
    }

    void growEffect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      grow.Begin();
    }
    void grow_Completed(object sender, EventArgs e)
    {
      ResetScale(growEffect);
    }

    void shrinkEffect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      shrink.Begin();
    }
    void shrink_Completed(object sender, EventArgs e)
    {
      ResetScale(shrinkEffect);
    }

    void rotateEffect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      rotate.Begin();
    }

    void expandEffect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      expand.Begin();
    }

    void collapseEffect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      collapse.Begin();
    }
    void collapse_Completed(object sender, EventArgs e)
    {
      ResetClip(collapseEffect);
    }
    void dynamicEffect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      ApplyDynamicEffect();
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

SteveLi-Cellbi
Web Developer
United States United States
I'm excited about computers and programming, since my school days. I have master's degree in software development and at the moment I'm a software developer at Cellbi Software.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 5 Sep 2008
Article Copyright 2008 by SteveLi-Cellbi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid