|
I am trying to create a user control in a WPF application that includes a BitmapImage that has been added to the project by including it in Resources.resx. In the xaml editor using VS 2012 I can put the cursor inside the BitmapImage tag, and then reference the properties of the BitmapImage from the editor's properties pane.
In the UriSource dropdown box I can see all of the resources that I have added to the application including those images that are included in Resources.resx, but when I add one of the images and run the program, an exception is raised referencing the line in the xaml where the BitmapImage control is defined.
For example if I start with this:
<BitmapImage x:Key="CameraImage" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" />
And use properties pane to select the image I want from the UriSource field, this attribute is added:
UriSource="pack://siteoforigin:,,,/Resources/Camera32x32.png"
From what I can tell this URI references a file that should be on the drive in the "Resources" directory below the point from which the application runs. If so, I understand why this would produce an error, since that directory does not exist where the application runs (and is not supposed to).
From the documentation I have seen, if a resource is embedded in the local assembly (i.e., the exe file), that the right URI should be something like this:
UriSource="pack://application:,,,/Resources/Camera32x32.png"
where "siteoforigin " is replaced with "application ." But that does not seem to work for me either.
The exceptions I see differ depending on different modifications I've made to this attribute's value. The last one referenced gives me a XamlParseException with a message saying, "'Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception," and it had an inner IOException with a message of "Cannot locate resource 'resources/camera32x32.png'." The siteoforigin syntax also throws a XamlParseException , but the inner exception is a DirectoryNotFoundException , and says "Could not find a part of the path."
I have tried several things without luck here, and I've run out of ideas. Can someone help to point me down the right path, and tell me how I can reference an embedded resource from a xaml page?
Gil Yoder
Houston, TX
|
|
|
|
|
I lucked upon an answer to my own question with more research. Having the image in the Resources.resx file was not sufficient to reference the resource with the pack URI. I needed to set the properties of the file to be a resource, or configured to copy to the output directory. I had neither.
By setting the files to "resource" I was able to get the URI to work even after remove all of the files from the RESX file.
[url=http://www.forum-signatures.com][img]http://www.forum-signatures.com/wizard/Sigs/2010/final132825116615.png[/img][/url]
|
|
|
|