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

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.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AgDynAnimations
{
  public abstract class DynamicEffect
  {
    FrameworkElement target;
    Storyboard storyboard;

    public DynamicEffect()
    {
    }

    protected abstract Storyboard CreateStoryboard(FrameworkElement target);

    protected virtual void Add(FrameworkElement target)
    {
      this.target = target;
      storyboard = CreateStoryboard(target);
      storyboard.Completed += new EventHandler(OnCompleted);
      target.Resources.Add(storyboard);
    }
    protected virtual void Remove()
    {
      if (target == null)
        return;
      if (storyboard == null)
        return;
      target.Resources.Remove(storyboard);
      target = null;
      storyboard = null;
    }
    protected virtual void OnCompleted(object sender, EventArgs e)
    {
      Remove();
      if (Completed != null)
        Completed(sender, e);
    }

    public virtual void Start(FrameworkElement target)
    {
      Add(target);
      storyboard.Begin();
    }
    public virtual void Stop()
    {
      if (target == null || storyboard == null)
        return;
      storyboard.Stop();
      Remove();
    }

    public event EventHandler Completed;
  }

  public class FadeEffect : DynamicEffect
  {
    double from;
    double to;
    double speed;

    public FadeEffect(double from, double to, double speed)
    {
      this.from = from;
      this.to = to;
      this.speed = speed;
    }

    protected override Storyboard CreateStoryboard(FrameworkElement target)
    {
      Storyboard result = new Storyboard();
      
      DoubleAnimation animation = new DoubleAnimation();
      animation.From = from;
      animation.To = to;
      animation.SpeedRatio = speed;
      
      Storyboard.SetTarget(animation, target);
      Storyboard.SetTargetProperty(animation, "(UIElement.Opacity)");

      result.Children.Add(animation);

      return result;
    }
  }

  public class FadeInEffect : FadeEffect
  {
    public FadeInEffect()
      : base(0.0, 1.0, 3.0)
    {
    }
  }

  public class FadeOutEffect : FadeEffect
  {
    public FadeOutEffect()
      : base(1.0, 0.0, 3.0)
    {
    }
  }
}

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
Web03 | 2.8.140926.1 | Last Updated 5 Sep 2008
Article Copyright 2008 by SteveLi-Cellbi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid