Click here to Skip to main content
15,886,724 members
Articles / Programming Languages / C#

Combining Animations in Code in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.62/5 (6 votes)
26 Nov 2011CPOL6 min read 20.9K   438   7  
This article explains to combine animations and easing functions to generate effects like the Les Paul (guitar) Google Doodle by Google and National Geographic channel’s Test your brain episode, etc.
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 CanvasExpt
{
    public partial class MainPage : UserControl
    {
        //http://localhost:2042/CanvasExptTestPage.aspx
        public Dictionary<string, UserControl> pageCache;
        UserControl curPage;
        public delegate void _ONCLOSE_();
        _ONCLOSE_ onClose;
        public MainPage()
        {
            InitializeComponent();
            pageCache = new Dictionary<string, UserControl>();
        }
        public void NavigateToCanvasPage()
        {
            string strPage = "CanvasExpt.CanvasPage";
            if (curPage != null)
            {
                if (onClose != null)
                {
                    onClose();
                }
                LayoutRoot.Children.Remove(curPage);
            }
            CanvasPage cnvPage;
            if (pageCache.ContainsKey(strPage))
                cnvPage = (CanvasPage)pageCache[strPage];
            else
            {
                cnvPage = new CanvasPage();
                pageCache.Add(strPage, cnvPage);
            }
            onClose += cnvPage.onClose;
            LayoutRoot.Children.Add(cnvPage);
            curPage = cnvPage;
            Grid.SetRow(cnvPage, 2);
        }
        public void NavigateToBounceAnimationPage()
        {
            string strPage = "CanvasExpt.StringVibrationPage";
            if (curPage != null)
            {
                if (onClose != null)
                {
                    onClose();
                }
                LayoutRoot.Children.Remove(curPage);
            }
            BounceAnimationPage stringVibrationPage;
            if (pageCache.ContainsKey(strPage))
                stringVibrationPage = (BounceAnimationPage)pageCache[strPage];
            else
            {
                stringVibrationPage = new BounceAnimationPage();
                pageCache.Add(strPage, stringVibrationPage);
            }
            onClose += stringVibrationPage.onClose;
            LayoutRoot.Children.Add(stringVibrationPage);
            curPage = stringVibrationPage;
            Grid.SetRow(stringVibrationPage, 2);
        }
        public void NavigateToCircleAnimationPage()
        {
            string strPage = "CanvasExpt.CircleAnimations";
            if (curPage != null)
            {
                if (onClose != null)
                {
                    onClose();
                }
                LayoutRoot.Children.Remove(curPage);
            }
            CircleAnimations circleAnimationPage;
            if (pageCache.ContainsKey(strPage))
                circleAnimationPage = (CircleAnimations)pageCache[strPage];
            else
            {
                circleAnimationPage = new CircleAnimations();
                pageCache.Add(strPage, circleAnimationPage);
            }
            onClose += circleAnimationPage.onClose;
            LayoutRoot.Children.Add(circleAnimationPage);
            curPage = circleAnimationPage;
            Grid.SetRow(circleAnimationPage, 2);
        }
        public void NavigateToArcAnimationPage()
        {
            string strPage = "CanvasExpt.ArcAnimationPage";
            if (curPage != null)
            {
                if (onClose != null)
                {
                    onClose();
                }
                LayoutRoot.Children.Remove(curPage);
            }
            ArcAnimationPage circleAnimationPage;
            if (pageCache.ContainsKey(strPage))
                circleAnimationPage = (ArcAnimationPage)pageCache[strPage];
            else
            {
                circleAnimationPage = new ArcAnimationPage();
                pageCache.Add(strPage, circleAnimationPage);
            }
            onClose += circleAnimationPage.onClose;
            LayoutRoot.Children.Add(circleAnimationPage);
            curPage = circleAnimationPage;
            Grid.SetRow(circleAnimationPage, 2);
        }
        private void button_Click_ShowCanvasPage(object sender, RoutedEventArgs e)
        {
            NavigateToCanvasPage();
        }

        private void ShowBounceAnimationPage_Click(object sender, RoutedEventArgs e)
        {
            NavigateToBounceAnimationPage();
        }

        private void ShowCircleAnimationPage_Click(object sender, RoutedEventArgs e)
        {
            NavigateToCircleAnimationPage();
        }

        private void ShowArcAnimationPage_Click(object sender, RoutedEventArgs e)
        {
            NavigateToArcAnimationPage();
        }

    }
}

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
Web Developer
India India
I have a PhD in pure mathematics from HCU. I have been programming in Microsoft technologies for more than a decade. My interests are C#, WPF Silverlight, MFC, COM.

Comments and Discussions