Windows Phone – Silverlight: The Difference Between “Resource” and “Content”

Everything in a project folder requires a choice: Resource or Content? This post explains the difference between these two types, along with the pros and cons of each.

1. The Difference

The main difference is that a Resource is embedded inside the DLL file, whereas Content becomes a separate file that ships alongside the DLL.

You might wonder: what is Isolated Storage then? Don’t confuse them — Isolated Storage is a completely different concept: an independent storage area created when you install an application.

Try a quick test. Create a Silverlight project (Windows Phone makes it easy :3), add a few images, mark some as Resource and the rest as Content, then build the XAP file. Rename the XAP to ZIP and extract it. You’ll see that files marked as Content appear directly in the archive, while files marked as Resource have been embedded in the DLL and are no longer visible.

2. Accessing Files

Accessing Resource and Content files differs slightly.

2.1. Content Images

Content is a standalone file, so it’s straightforward to access:

XAML:

<Image Stretch=”None” Source=”/images/yourImage.png”/>

C#:

Uri uri = new Uri(“/images/yourImage.png”, UriKind.Relative);
BitmapImage imgSource = new BitmapImage(uri);

2.2. Resource Images

Because a Resource is embedded inside a DLL, accessing it is more involved:

XAML:

<Image Source=”/TestProjet;component/images/yourImage.png”/>

C#:

Uri uri = new Uri(“/TestProjet;component/images/yourImage.png”, UriKind.Relative);
this.Image.Source = new BitmapImage(uri);

3. Which One to Choose

Now that you understand the difference, which should you use for files added to your project?

A Resource file loads faster when accessed, but slows down application startup. Conversely, a Content file loads more slowly when accessed, but the application doesn’t need to load it into memory at startup.

If you need to access a large file that isn’t required immediately, go with Content. On the other hand, if the application needs the file right away at launch (a wallpaper, for example), Resource is the better choice — you’re going to load it anyway.

Choosing Resource also eliminates the jarring effect where the app has loaded partway and then the background image suddenly “pops in.”