Share on FacebookThere 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