Changing the WP7 Panorama Background Image dynamically with Animation
published on: 3/15/2011 | Views: N/A | Tags: Panorama Animation
by WindowsPhoneGeek
In this post I am going to demonstrate how to change the Panorama Background Image dynamically using Animation.
To begin with lets first create a sample "Windows Phone 7 Panorama Application'", create a new folder into the project called Images and add two Images into it : image1.jpg and image2.jpg.
![]()

1.) At first we will set image1.jpg as a background of the Panorama:
<controls:Panorama Title="my application" x:Name="panorama">
<controls:Panorama.Background>
<ImageBrush ImageSource="Images/image1.jpg"/>
</controls:Panorama.Background>
...
</controls:Panorama>
2.) After that we will add a new PanoramaItem with a button into it:
<controls:PanoramaItem Header="Last item">
<Button Content="Change Background Image" Background="Black" Height="100" Click="Button_Click"/>
</controls:PanoramaItem>
3.) The next step is to implement the animation that will be used to switch the background images. We will create a FadeIn/FadeOut animation effect:
private void FadeInOut(DependencyObject target,Storyboard sb, bool isFadeIn)
{
Duration d = new Duration(TimeSpan.FromSeconds(1));
DoubleAnimation daFade = new DoubleAnimation();
daFade.Duration = d;
if (isFadeIn)
{
daFade.From = 1.00;
daFade.To = 0.00;
}
else
{
daFade.From = 0.00;
daFade.To = 1.00;
}
sb.Duration = d;
sb.Children.Add(daFade);
Storyboard.SetTarget(daFade, target);
Storyboard.SetTargetProperty(daFade, new PropertyPath("Opacity"));
sb.Begin();
}
4.) And finally we will add the following code into the button click event handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
Storyboard sbFadeIn = new Storyboard();
sbFadeIn.Completed += new EventHandler(sb_Completed);
FadeInOut(this.panorama.Background, sbFadeIn, true);
}
void sb_Completed(object sender, EventArgs e)
{
BitmapImage bitmapImage = new BitmapImage(new Uri("Images/image2.jpg", UriKind.Relative));
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = bitmapImage;
this.panorama.Background = imageBrush;
Storyboard sbFadeOut = new Storyboard();
FadeInOut(this.panorama.Background, sbFadeOut, false);
}
5. That`s it. Just build and run the application here is how the result should looks like:
Here is the full source code:
PanoramaAnimatedBackground.zip
I hope that the article was helpful.
NOTE: if you are experiencing performance issues take a look at this solution : http://vantsuyoshi.wordpress.com/2012/01/04/do-not-animate-wp7-panorama-control-background/
Another approach you can find in Jeff Wilcox`s post : WP7 Panorama: Smoothly fading the background (and enabling fading when changing, too)
You can also follow us on Twitter @winphonegeek
Comments
Extremely helpful
posted by: Patrunjelu on 5/3/2011 7:37:27 PM
Thanks!
Request
posted by: Arses on 8/15/2011 8:51:45 AM
Hi Great job I want know is it possible to make an app for WP7 to change background automatically ?? if exist, please link me to. thanks
Re:Request
posted by: winphonegeek on 8/19/2011 6:36:32 PM
Yes it is possible. We will consider your request.
posted by: zzks on 9/6/2011 9:35:09 AM
very slow and very unpleasure, 2 animation work together,we should stop one first!
Do not animate panorama background opacity
posted by: vantsuyoshi on 1/4/2012 7:40:02 AM
Hi,
you must not animate panorama control's background opacity as you can see on video, its choppy
use image Strech fill and the size is as same as your image actual size
animate the image
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
