I pulled this code from the Application resources and localization sample sample project:
var resourceLoader = new ResourceLoader(); var file = resourceLoader.GetFile("Images/projector.png"); fileImage.Source = new BitmapImage(new Uri(file.Path, UriKind.Absolute));
This works fine for images located within the main application assembly. However, how do I load an image located within a referenced library inside my app package? (I made sure the image's Build Action is set to Content -- same as images in the main application).
The following resource names fail to be resolved when passed to GetFile (all throw"NamedResource Not Found. (Exception from HRESULT: 0x80073B17"):
Is my syntax incorrect, or is what I'm trying to do just not supported at all?
- Edited by nrcaliendo Friday, February 24, 2012 1:55 PM
[information on non-public build deleted
This forum is for discussion of the publically available Developer Preview only. If you have access to a newer build under NDA then please address all questions through the channel from which you received that build.]
- Edited by Rob Caplan [MSFT]Microsoft employee, Owner Wednesday, February 29, 2012 1:58 AM non-public
So, ResourceLoader.GetFile, which was in Developer Preview, is now gone from WinRT in Consumer Preview.
I've been looking at the updated sample project, Application resources and localization sample. Some additional scenarios have been added (e.g., loading images from AppX manifest); but none address loading images from a class library from code. I can load strings all day long, but not images.
It's noteworthy that only strings can be added to resource (RESW) files, but not images or anything else. So, do "resources" = strings now in Metro?(BTW, "ms-resource://ClassLibrary1/Files/Images/projector.png" does not work either).
- Edited by nrcaliendo Thursday, March 01, 2012 2:57 AM
File resources are now just "files" so if you use the StorageFile API's with an "ms-appx://" URL, it will virtualize appropriately. The ms-appx protocol only covers files, so you don't need to insert "Files" in the URI.
So assuming your image is in the "ClassLibrary" folder of your main package, GetFileFromApplicationUriAsync("ms-appx:///ClassLibrary/Images/projector.png") should work for you on the Consumer Preview build. If you have multiple variants of projector.png for language, region, scale, contrast, etc. you'll get the most appropriate one for your current context.
- Proposed as answer by Robert_Levy Friday, March 02, 2012 2:37 PM