A Simple Silverlight Image Rotator

by Bobbi Perreault 24. April 2009 19:05
Share on Facebook

There are a million and three photo display Silverlight applications out there.  And here's one more.  I just wanted my own.  I wanted one that I can load up the images in Xaml and push go with.  Here it is,

First, define all my images in Xaml

  <Canvas x:Name="ImageDisplay" Width="600" Height="450">
        <Image Margin="0,0,0,0" Source="images/IMG_1989.JPG"  Width="600" Height="450" x:Name="image" Opacity="0" />
        <Image Margin="0,0,0,0" Source="images/IMG_1990.JPG"  Width="600" Height="450" Opacity="0"/>
        <Image Margin="0,0,0,0" Source="images/IMG_1991.JPG"  Width="600" Height="450" Opacity="0"/>
 </Canvas>

 

Second, wire it up in the code behind:

public Page()        
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
//start the animations
beginTheFirst();
}
private void beginTheFirst()
{
int icnt = 0;
//tell the imageRotater what it will manage.
Canvas container = ImageDisplay;
mcarousel = new imageRotater();
for (int i = container.Children.Count - 1; i >= 0; i--)
{
FrameworkElement childFE = container.Children[i] as FrameworkElement;
if (childFE is Image)
{
mcarousel.CreateItem( childFE, "resources" + icnt.ToString(), this);
icnt++;
}
}
//start the first.
mcarousel.StartRotating();
}

Everything hinges on this SwitchImage function, but that’s all.  The objects manage themselves after that.

public void SwitchImage(FrameworkElement image, FrameworkElement oldimage)

{

if (image == null)

return;

if (oldimage != null)

{

FadeOutEffect fadeOut = new FadeOutEffect();

fadeOut.Start(oldimage);

}

mImage = image;

FadeInEffect fadeIn = new FadeInEffect();

fadeIn.Start(image);

}

If you’d like the source, you can download it here: http://www.faxt.com/silverlight/code/countrylife.zip

Tags:

Comments

11/18/2009 7:40:54 AM #

Pingback from silverlightblog.cn

A Simple Silverlight Image Rotator Silverlight Blog

silverlightblog.cn

5/20/2010 8:24:26 AM #

Pingback from 273.jeepsunlimted.com

745li Radiator Nissens 740il Bmw 540i, 540i Wagon For Sale Bmw 540it Keyless Entry

273.jeepsunlimted.com

Comments are closed

 

RSS Feed FriendFeed