Thursday, April 10, 2008

Understanding Application Resource Files

First let me mention that the word "resources" is used in two contexts in Silverlight (and WPF). The main usage refers to styles, templates etc. that are reusable resources. The other usage is for referring to application resource files like images, videos etc that are used by the application. This post is about the latter.


When you add such resources to a Silverlight application you can set their build action mainly in three ways: Resource, Content or None.

In the example on the left, I have added 3 images - one as Resource, the second as Content and the last as None.

What happens to these images when the application is compiled? The image assigned as Resource (Bread.png) gets embedded into the Silverlight application assembly. So this image is not seen in the screenshots below. The image assigned as Content (Burger.png) is not embedded into the application assembly but nevertheless gets packaged into the .xap file. (Remember that the .xap file is basically a zip file renamed with .xap extension . This zip file contains the application assembly as well as other Content resource files.) The image assigned as None (Watermelon.png) is a loose resource file and so is outside the .xap package.
The screenshots below should be helpful to visualize this.

Ths screenshot is of the client bin. You can see the .xap package and the image marked as None. This resource can act as an on-Demand resource file, and so will not be downloaded initially to the client machine. It will be downloaded only when the user navigates to the page that displays this image.


When the .xap package is unzipped, you get the files shown in the screenshot on the left. You can see that the image marked as Content is part of the unzipped contents. The image marked as Resource is embedded with the application assembly (ResourceFiles.dll) and hence cannot be seen in this screen shot.


How should one refer to these resources in the XAML? And when would one prefer to use one type of build action over the other? This MSDN article explains this thoroughly. Also, this post by Ashish Shetty.

You can find the sample application I described above here (updated to Beta 2).

2 comments:

Gear said...

However the problem with None is that when we use the Publish in VS2010, the images we have in our project aren't copied to the destination hence having to cpy them all manually. It's time consuming. Any other idea ?

Gear said...

I also saw that when I use CONTENT, I do not see the images in the xap file.