Project Description
This project is StoryboardHelper. It consists of a Silverlight project with a class called StoryboardHelper. Currently it has two public static methods called FadeInFrameworkElement and FadeIOutFrameworkElement. They allow you to specify a FrameworkElement to be faded in or out over a specified duration after a specified delay time. It is my hope that people will add to the StoryboardHelper class to make this the most comprehensive Silverlight Storyboard helper class around.

Please download the project called StoryboardHelperProject and either use the StoryboardHelper class as is or add public static methods to assist with Silvelright Storyboards.

When you open StoryboardHelperProject you will see that there is a Rectangle on the main page (MainPage.xaml) named TheRect. It has an Opacity of 0 (meaning you cannot see it). In MainPage.xaml.cs there is a loaded handler that specifies a MouseLeftDown event. The MainPage_MouseLeftButtonDown event handler checks to see if TheRect's Opacity is 0. If it is it creates a Storyboard called FadeInRect that uses the StorybaordHelper.FadeInFrameworkElement method to create the Storyboard. If it is not null then it Begins the Storyboard. If the Opacity is 1 then it does the same thing except it fades out TheRect by using the StoryboardHelper.FadeOutFrameworkElement. Here is the code:

public partial class MainPage : UserControl
{
public MainPage()
{
// Required to initialize variables
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
MouseLeftButtonDown += new MouseButtonEventHandler(MainPage_MouseLeftButtonDown);
}

void MainPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (TheRect.Opacity == 0)
{
Storyboard fadeInRect = StoryboardHelper.FadeInFrameworkElement(TheRect, .5, 0);
if (fadeInRect != null)
{
fadeInRect.Begin();
}
}
else
{
Storyboard fadeOutRect = StoryboardHelper.FadeOutFrameworkElement(TheRect, .5, 0);
if (fadeOutRect != null)
{
fadeOutRect.Begin();
}
}

}
}

You may use this class as is or go ahead and add to the StoryboardHelper class. An example of a new StoryboardHelper method would be a static method named MoveFrameworkElementXPosition that moves the specified object in the X coordinates to the new specfied X coordinate.

Last edited Nov 8, 2009 at 11:57 PM by victorgaudioso, version 3