Click here to Skip to main content
15,886,074 members
Articles / Desktop Programming / WPF

Custom Loader Animations in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.67/5 (8 votes)
22 Feb 2011CPOL5 min read 31.8K   856   22  
A guide to making custom loading animations for your applications in Silverlight.
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 RCCustomLoaders.Loaders
{
    public partial class LittleBoxes : UserControl
    {
        private EasingFunctionBase _easingFunction;

        public EasingFunctionBase EasingFunction
        {
            get { return _easingFunction; }
            set
            {
                _easingFunction = value;
                foreach (Timeline anim in animStoryboard.Children)
                {
                    if (anim is DoubleAnimation)
                    {
                        (anim as DoubleAnimation).EasingFunction = value;
                    }
                    else
                    {
                        if (anim is ColorAnimation)
                        {
                            (anim as ColorAnimation).EasingFunction = value;
                        }
                        else
                        {
                            if (anim is DoubleAnimationUsingKeyFrames)
                            {
                                foreach (DoubleKeyFrame key in (anim as DoubleAnimationUsingKeyFrames).KeyFrames)
                                {
                                    if (key is EasingDoubleKeyFrame)
                                    {
                                        (key as EasingDoubleKeyFrame).EasingFunction = value;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private RepeatBehavior _repeatBehavior;

        public RepeatBehavior RepeatBehavior
        {
            get { return _repeatBehavior; }
            set
            {
                _repeatBehavior = value;
                foreach (Timeline anim in animStoryboard.Children)
                {
                    anim.RepeatBehavior = value;
                }
            }
        }

        private Duration _duration;

        public Duration Duration
        {
            get { return _duration; }
            set
            {
                _duration = value;
                foreach (Timeline anim in animStoryboard.Children)
                {
                    anim.Duration = value;
                }
            }
        }

        private bool _autoReverse;

        public bool AutoReverse
        {
            get { return _autoReverse; }
            set
            {
                _autoReverse = value;
                foreach (Timeline anim in animStoryboard.Children)
                {
                    anim.AutoReverse = value;
                }
            }
        }

        private TimeSpan? _addToBeginTime;

        public TimeSpan? AddToBeginTime
        {
            get { return _addToBeginTime; }
            set
            {
                _addToBeginTime = value;
                foreach (Timeline anim in animStoryboard.Children)
                {
                    anim.BeginTime += value;
                }
            }
        }

        public LittleBoxes()
        {
            InitializeComponent();
        }
    }
}

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)


Written By
Software Developer self employed
South Africa South Africa
I develop awesome Windows Phone/Windows 8 stuff, am a Nokia Developer Champion, I do Netduino electronics stuff, and blog a lot. I also occasionally do talks about development at Universities and conferences like TechEd. I run a small indie Windows Phone studio, currently working on an AppCampus-funded game.

Checkout my just-for-fun apps here: http://www.windowsphone.com/en-US/store/publishers?publisherId=RogueCode&appId=23d742d2-5b14-48a7-8e5f-b3b779537338
I also do Windows Phone (and Windows) development for clients, for example: http://www.windowsphone.com/en-za/store/app/dstv/a87feeed-a8dd-4bcb-8d47-15908340fdab

I am currently on hiatus from writing development articles for WPCentral.com.

My first book has just been published on home automation with a Netduino: http://www.amazon.co.uk/Netduino-Home-Automation-Projects-Cavanagh/dp/1849697825

Comments and Discussions