/* Classe MyWpfExtensions : PlayAnimations Jouer des animations les unes après les autres facilement ! Version 1.0 - Last Modif : 08/01/2009 15:00 Développé par Sebastien WARIN < sebastien [arobase] warin [point] fr > (c) 2009 - http://sebastien.warin.fr */ using System; using System.Windows; using System.Windows.Media.Animation; namespace SmartCooking { public static class MyWpfExtensions { public static void PlayAnimations(this Window window, params string[] animationsName) { // If animationsName is empty: return if (animationsName.Length <= 0) return; // For each animation for (int i = 0; i < animationsName.Length; i++) { // Get Storyboard object var anim = window.GetStoryBoard(animationsName[i]); if (anim != null) { // If not the last animation, if (i < (animationsName.Length - 1)) { // Add handler on the Completed event of the current storyboard to play the te next animation anim.Completed += new EventHandler( new StoryboardPlayNextHandler(window, anim, animationsName[i + 1]).OnHandler); } } } // Finally, play the 1st animation window.BeginStoryboard(window.GetStoryBoard(animationsName[0])); } public static Storyboard GetStoryBoard(this Window window, string name) { if (window.Resources.Contains(name)) return (Storyboard)window.Resources[name]; else return null; } public class StoryboardPlayNextHandler { private string _nextAnimationName; private Storyboard _actualStoryBoard; private Window _window; public StoryboardPlayNextHandler(Window window, Storyboard actualStoryBoard, string nextAnimationName) { this._actualStoryBoard = actualStoryBoard; this._nextAnimationName = nextAnimationName; this._window = window; } public void OnHandler(object sender, EventArgs e) { if (_actualStoryBoard != null) { // Remove the current handler on the completed event ! _actualStoryBoard.Completed -= new EventHandler(this.OnHandler); // Launch next storyboard _window.BeginStoryboard(_window.GetStoryBoard(_nextAnimationName)); } } } } }