6

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?

Henry C
  • 4,781
  • 4
  • 43
  • 83

2 Answers2

10

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 What are the various "Build action" settings in Visual Studio project properties and what do they do?

Community
  • 1
  • 1
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • 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...) – Henry C Sep 03 '10 at 13:03
  • 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 Sep 03 '10 at 14:06
  • 4
    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 Sep 05 '10 at 13:34
  • 2
    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 Sep 14 '10 at 12:13
  • One important difference I have noticed. It is true that setting to Content lowers the size of the assembly. But... if you use the image on the first page to show on startup like this: Source="/MyApp;component/Images/UI/board1.png", you may see the image 'pop'. In other words, if you want to make sure an image is visible the instant the app starts, use Resource. Actually, I am not sure if that is only a problem on the first page to show, but that was where I noticed it and changed my UI images back to Resource.. – Paul Hoenecke Feb 24 '12 at 01:14
3

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
  • 14,892
  • 2
  • 35
  • 41
  • Also worth noting, you can have a look at the impact this has on your XAP packaging by renaming your XAP to ZIP and opening it for a look. – Mick N Nov 01 '10 at 01:05