views:

106

answers:

2

I've been working on a Windows Phone 7 app, and after a bit of Googling it seems that for images that I have added to the Visual Studio project, I need to set the build action to "Content" in order to be able to reference the images in my app.

However, the Windows Phone List Application project template includes an image (ArrowImg.png) that has its Build Action set to "Resource", and is still available to be referenced from the application.

I was wondering if anyone could confirm that we should definitely be using the Content build action, or whether there is some way to access images added to a project with the Resource Build Action as shown in the project sample, which we should be using instead?

+7  A: 

If you set the action to "Content" the image is included "as-is" in the XAP. If you set the action to "Resource" the image is embedded into a shared DLL.

In a lot of situations you can use either. There may be a performance, or other, issue with using one rather than another but I'm not aware of and have never noticed any.

For what it's worth, unless I need to specifically make it a resource, I use content.

With the current (Beta) tools, I have seen VS complain that images directly referenced in XAML should be set to "Resource" (if set to "Content") but the app works fine with either. Hopefully this is an issue which will be addressed in the RTM tools.

For more information see the discussion in http://stackoverflow.com/questions/145752/what-are-the-various-build-action-settings-in-vs-net-project-properties-and-wha

Matt Lacey
thanks for the response - so you have definitely managed to refer to an image with a resource build action in the beta wp7 tools? (I tried for quite a while to do so but haven't been able to, which was a little frustrating as that's what the sample project does...)
Blakomen
I've accessed images as resources in the beta tools in code using Application.GetResourceStream(new Uri(filename, UriKind.Relative)); I've also directly refered to images of both build types in XAML. VS creates a warning about the resource but it does still work.I guess it depends how you want to refer to them
Matt Lacey
Using contents instead of resources will also minimize the size of assemblies, which will reduce application startup time (see the [high performances whitepaper](http://www.jeff.wilcox.name/2010/08/windows-phone-performance/) for details).
Andréas Saudemont
One of the most substantial differences between Resource and Content is Resource is loaded on app startup (with corresponding startup performance impacts) whereas Content is loaded on demand (with corresponding load time impacts).
Mick N
+1  A: 

Either build action is correct.

Also worth looking at when resolving issues relating to build action is the pathing you use.

I've seen a fair few people running into trouble with this because they assume they've set their build action inappropriately.

You can set the build action either way to suit your requirements of when to incur the load time cost, you just need to adjust the pathing to suit.

Some more info on the topic from this post.

Set source to image in C#

You can liken content to the lazy loading version of resources.

The difference is you will incur the performance hit of all resources when the assemblies are loaded to start your app.

Content, on the other hand, the performance hit is deferred to when you use it.

Which is more suitable will vary on a case by case basis.

Note also that the pathing to reference resources and content is different as can see here.

  //Uri uri = new Uri("/Resources/Images/MyImage.jpg", UriKind.Relative); // Content 
  Uri uri = new Uri("/PhoneApp;component/Resources/Images/MyImage.jpg", UriKind.Relative); // Resource 
  BitmapImage imgSource = new BitmapImage(uri);
  image1.Source = imgSource;
Mick N