CanonStoryboards
CodeKeep C# Feed Agosto 27th, 2008
Description: Playing a series of storyboards in canon using WPFLink: http://www.codekeep.net/snippets/53d0a024-8ec4-4fe5-b96c-968b479a7d13.aspx
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Media.Animation;
using System.Diagnostics;
namespace SmartBorderTest.StoryBoards {
/// <summary>
/// Plays a series of storyboards in a canon sequence, one after the other
/// </summary>
class CanonStoryboards {
private List<Storyboard> _storyboards = new List<Storyboard>();
private int _count = -1;
/// <summary>
/// The FrameworkElement that contains the storyboards
/// </summary>
private FrameworkElement _containingObject;
public FrameworkElement ContainingObject {
get { return _containingObject; }
set { _containingObject = value; }
}
/// <summary>
/// Initializes a new instance of the CanonStoryboards class
/// </summary>
public CanonStoryboards() {
}
/// <summary>
/// Initializes a new instance of the CanonStoryboards class and
/// sets the containing object used when starting the storyboards
/// </summary>
/// <param name="containingObject">The FrameworkElement that contains
/// the storyboards</param>
public CanonStoryboards(FrameworkElement containingObject) : this() {
_containingObject = containingObject;
}
/// <summary>
/// Adds a storyboard to the canon sequence.
/// Storyboards are played in the order they are added.
/// </summary>
/// <param name="storyboard">The storyboard to add</param>
public void AddStoryboard(Storyboard storyboard) {
if (storyboard == null)
throw new ArgumentNullException("storyboard");
_storyboards.Add(storyboard);
}
/// <summary>
/// Begins the canon sequence of storyboards
/// </summary>
public void Begin() {
if (_containingObject == null)
throw new InvalidOperationException(
"ContainingObject must be set before Begin can be called");
if (_storyboards.Count == 0)
throw new InvalidOperationException(
"Storyboards must be added before Begin can be called");
// start the first storyboard
BeginStoryboard(_storyboards[0]);
}
/// <summary>
/// Handler for storyboards' Completed event
/// </summary>
/// <param name="sender">The sender</param>
/// <param name="e">The event args</param>
void Storyboard_Completed(object sender, EventArgs e) {
Debug.WriteLine("CanonStoryboards: storyboard completed");
// get the next storyboard in the series
Storyboard nextStoryboard = GetNextStoryboard();
if (nextStoryboard != null) {
BeginStoryboard(nextStoryboard);
}
else {
Debug.WriteLine("CanonStoryboards: all storyboards completed");
}
}
/// <summary>
/// Begins a storyboard in the canon sequence
/// </summary>
/// <param name="storyboard">The storyboard to begin</param>
void BeginStoryboard(Storyboard storyboard) {
// wire up hanlder to completed event
storyboard.Completed += new EventHandler(Storyboard_Completed);
_count++; // increment storyboard counter
storyboard.Begin(_containingObject);
Debug.WriteLine("CanonStoryboards: storyboard begun, " + storyboard.Name);
}
/// <summary>
/// Gets the next storyboard in the canon sequence
/// </summary>
/// <returns>The next storyboard in the sequence,
/// null if no other storyboards to play</returns>
Storyboard GetNextStoryboard() {
if (_count >= 0 && _storyboards.Count > _count + 1) {
return _storyboards[_count + 1];
}
return null;
}
}
}