Home
Manage Your Code
Snippet: CanonStoryboards (C#)
Title: CanonStoryboards Language: C#
Description: Playing a series of storyboards in canon using WPF Views: 76
Author: sudhakar kalmari Date Added: 8/27/2008
Copy Code  
1
2using System;
3using System.Collections.Generic;
4using System.Text;
5using System.Windows;
6using System.Windows.Media.Animation;
7using System.Diagnostics;
8
9namespace SmartBorderTest.StoryBoards {
10    /// <summary>
11    /// Plays a series of storyboards in a canon sequence, one after the other
12    /// </summary>
13    class CanonStoryboards {
14        private List<Storyboard> _storyboards = new List<Storyboard>();
15        private int _count = -1;
16
17        /// <summary>
18        /// The FrameworkElement that contains the storyboards 
19        /// </summary>
20        private FrameworkElement _containingObject;
21        public FrameworkElement ContainingObject {
22            get { return _containingObject; }
23            set { _containingObject = value; }
24        }
25
26        /// <summary>
27        /// Initializes a new instance of the CanonStoryboards class
28        /// </summary>
29        public CanonStoryboards() {
30        }
31        
32        /// <summary>
33        /// Initializes a new instance of the CanonStoryboards class and
34        /// sets the containing object used when starting the storyboards
35        /// </summary>
36        /// <param name="containingObject">The FrameworkElement that contains
37        /// the storyboards</param>
38        public CanonStoryboards(FrameworkElement containingObject) : this() {
39            _containingObject = containingObject;
40        }
41
42        /// <summary>
43        /// Adds a storyboard to the canon sequence.
44        /// Storyboards are played in the order they are added.
45        /// </summary>
46        /// <param name="storyboard">The storyboard to add</param>
47        public void AddStoryboard(Storyboard storyboard) {
48            if (storyboard == null)
49                throw new ArgumentNullException("storyboard");
50            _storyboards.Add(storyboard);
51        }
52        
53        /// <summary>
54        /// Begins the canon sequence of storyboards
55        /// </summary>
56        public void Begin() {
57            if (_containingObject == null)
58                throw new InvalidOperationException(
59                    "ContainingObject must be set before Begin can be called");
60            
61            if (_storyboards.Count == 0)
62                throw new InvalidOperationException(
63                    "Storyboards must be added before Begin can be called");
64
65            // start the first storyboard
66            BeginStoryboard(_storyboards[0]);
67        }
68
69        /// <summary>
70        /// Handler for storyboards' Completed event
71        /// </summary>
72        /// <param name="sender">The sender</param>
73        /// <param name="e">The event args</param>
74        void Storyboard_Completed(object sender, EventArgs e) {
75            Debug.WriteLine("CanonStoryboards: storyboard completed");
76            // get the next storyboard in the series
77            Storyboard nextStoryboard = GetNextStoryboard();
78            if (nextStoryboard != null) {
79                BeginStoryboard(nextStoryboard);
80            }
81            else {
82                Debug.WriteLine("CanonStoryboards: all storyboards completed");
83            }
84        }
85
86        /// <summary>
87        /// Begins a storyboard in the canon sequence
88        /// </summary>
89        /// <param name="storyboard">The storyboard to begin</param>
90        void BeginStoryboard(Storyboard storyboard) {
91            // wire up hanlder to completed event
92            storyboard.Completed += new EventHandler(Storyboard_Completed);
93            _count++; // increment storyboard counter
94            storyboard.Begin(_containingObject);
95            Debug.WriteLine("CanonStoryboards: storyboard begun, " + storyboard.Name);
96        }
97
98        /// <summary>
99        /// Gets the next storyboard in the canon sequence
100        /// </summary>
101        /// <returns>The next storyboard in the sequence,
102        /// null if no other storyboards to play</returns>
103        Storyboard GetNextStoryboard() {
104            if (_count >= 0 && _storyboards.Count > _count + 1) {
105                return _storyboards[_count + 1];
106            }
107            return null;
108        }
109    }
110}
111
112
Usage
void OnLoadHomeCommand(object sender, ExecutedRoutedEventArgs e) {
    // Start the show home storyboard
    CanonStoryboards cs = new CanonStoryboards(this);
    cs.AddStoryboard(((Storyboard)Resources["closeAbout"]));
    cs.AddStoryboard(((Storyboard)Resources["openHome"]));
    cs.Begin();
}

You might also find it useful to be able to play a number of storyboards in parallel and then play a final storyboard:

void OnLoadHomeCommand(object sender, ExecutedRoutedEventArgs e) {
    // Start the show home storyboard
    CanonStoryboards cs = new CanonStoryboards(this);
    cs.AddStoryboard(((Storyboard)Resources["closeAbout"]));
    cs.AddStoryboard(((Storyboard)Resources["openHome"]));
    ((Storyboard)Resources["closeFaq"]).Begin(this);
    cs.Begin();
}