WP7 Development Tip of the Day: Understanding the difference between content and resources

by brad 13. September 2010 10:40

There are two ways to include an image in a Windows Phone 7 project (that I know of).  As content or a resource. Resources are included in your assembly (DLL) while content is included in your deployment package (XAP) alongside the DLL.

Resource

If you copy and paste an image into your project, the default build action is “Resource”.  You can see this by selecting the image and viewing the properties.

CropperCapture83

When you reference an image that is a resource, the name of the assembly is appended to the reference.  Funky.

<Image Source="/WindowsPhoneApplication1;component/Untitled.png" />

If I look at the XAP file for this project (by changing the file extension from XAP to ZIP), then I can see that the image is nowhere to be found.  That’s because it’s in the DLL (note the large file size for WindowsPhoneApplication1.dll).

CropperCapture92

Content

The other choice is to include images as content.  This is done by switching the build action in the properties window.

CropperCapture112

References to content images are much less funky compared to the resource images.

<Image Source="Untitled.png" />

But the most obvious difference is in the packaging.  Now, my image is sitting inside the XAP instead of embedded in the DLL.  Note the difference in file size between the DLL now and when the image was embedded as a resource.  It’s one-fourth the size.

CropperCapture132

Which one do I use?

Each build action has its place.  Setting your build action to Content means that your application will load faster since the initial DLL will be smaller.  Jeff Wilcox points this out in his interview with Scott Hanselman.

Update: As Matthieupointed out in the comments, Content is the way to go for your application bar icons.  Otherwise, they will not show up and you’ll get the ugly default.

On the other hand, settings your build action to Resource will let you redeploy your DLL and the image with it.  This would be good for a class library

It’s interesting that the default is Resource – I usually change my build action to content for the faster load time and the easier-to-read XAML.

Other Resources

More Tips

Tags:

WP7 Tip of the Day | Windows Phone

Comments

9/13/2010 11:25:30 AM #

Matthieu

Good explanation !

You can see the content (sic) of your assembly with Reflector (http://www.red-gate.com/products/reflector/) and so see the embedded Image.

I learned that for icons in ApplicationBar you need to change to Content otherwise you will not see your image.

"Resource" can be useful if you distribute/reuse your images in another assembly, so the "funky" url has the name of the assembly where to find image, so you can have :

MyApplication.dll
MyImage.dll

<Image Source="/MyImage;component/Untitled.png" />

Matthieu Canada

9/13/2010 12:26:43 PM #

brad

Great point about Reflector, I should use that tool more often.

brad United States

9/14/2010 12:33:29 AM #

pingback

Pingback from iphone-ipod-ipad.zeo.hk

Apps : : Silverlight Cream for September 13, 2010 — #947 - iPhone iPod iPad

iphone-ipod-ipad.zeo.hk

10/27/2010 5:56:31 AM #

trackback

Дайджест технических материалов #5 (Windows Phone 7)

Tools, Books, Guides Windows Phone 7 Developer Tools RTM (online installer) , ISO UI Design and Interaction

Oleksandr Krakovetskiy blog

12/24/2010 4:07:48 PM #

pingback

Pingback from polhovskiy.wordpress.com

Использование памяти и немного о производительности « Блог Антона Полховского

polhovskiy.wordpress.com

6/21/2011 11:31:38 AM #

pingback

Pingback from chaokuaidi.com

超快递 beta版 » Windows Phone 7 资源汇总(超全)

chaokuaidi.com

5/17/2012 10:10:50 PM #

pingback

Pingback from dancecoder.com

Windows Phone 7 资源汇总 | DanceCoder

dancecoder.com

Comments are closed

About Brad

Brad Tutterow lives in Illinois and works in Missouri. He has 12 years of experience developing web sites and Windows applications using a variety of technologies and is most excited currently about Silverlight, Windows Phone 7, Halo Reach, and Visual Studio 2010.