If you are doing Windows Phone 7 development, chances are that you are either using or have considered using the Panorama. To make a truly glorious Panorama application, to immerse the user in the experience, or for branding purposes, many applications pay a lot of attention to the background. The Panorama has a Background property of type Brush. Although this is most often used with an ImageBrush, it can also be a SolidColorBrush or a BradientBrush. Using gradients is not recommended, unless you are a fan of color banding. It would be nice if the ImageBrush could be used with a WriteableBitmap, but that doesn’t work. But there is something better…
Unlocking Panorama.Background to allow arbitrary content
If you examine Panorama.UpdateBackground using Reflector, you can see that the background ContentPresenter’s Height is always set to the viewport’s height. It handles a Background that is set to a SolidColorBrush by setting width of the background’s ContentPresenter to the viewport width; if it is a GradientBrush, it sets the width to the width of the items; for ImageBrush it does some magic to wait for the image to be loaded, and then sets the background’s width to the pixel width of the image. It also sets a property ominously called “IsStatic”. Keen observers will also be able to see why WriteableBitmaps can’t be used as the ImageSource of your ImageBrush. But you don’t really want that anyway, because the perf is not good, and anything you can do with a WriteableBitmap, by definition you can do with XAML.
If you want to be able to put arbitrary XAML in the background, you need to somehow do two things:
- set the width of the background’s ContentPresenter
- set IsStatic to false
You can get this done in code, but it is a bit of a pain, and frankly, a little sketchy. Fortunately, there’s an easier way to do this. Remember the good old days of getting stuff to go where you want it in HTML by using 1 x 1 pixel transparent GIFs? Well, I don’t propose doing anything as old-school-hacky as that. No, this method uses a modern 1 x N transparent PNG (where N is the desired width of your background.) OK, I guess this is a little on the sketchy side, too, but like I said, it has the virtue of being easy.
The attached solution includes “dummy.png”, an 800 x 1 transparent PNG that I created using Paint.NET. It is included in the project, and the build action is set to “Resource”. In the main page’s XAML, the Panorama.Background is set to an ImageBrush that has dummy.png as its ImageSource. The other thing that we have to do is to re-template the Panorama, to include our new spiffy XAML background. Here’s what the page looks like:
You can see the Panorama’s Background being set to dummy.PNG (line 64). The new background is set in lines 29-44. Although this doesn’t need it, not having any colors near the edges, you can also apply a margin to prevent seaming. Here there is a reward for the keen observer. You will notice that there is a subtle animation on the Ellipse. Yes, this is a Panorama with an animated background. But please, if you’re going to travel down that path, keep it subtle, and watch that frame rate counter. And beware of wrapping artifacts. The Panorama is not designed to have a dynamic background. And keep both light and dark themes in mind.