Image shows up in Visual Studio designer but not during run-time
Maybe the Image does not have the Build Action set to Resource. If it's anything else, including Embedded Resource, then it will not display properly at runtime.
This was driving me crazy. I tried lots of different things based on various articles but nothing worked. My xaml window and image are in a separate DLL from the executable assembly. Not sure if that was part of my problem.
In the end I had success with:
- Make sure the image properties in the project has "Build Action" = "Resource", NOT "Embedded Resource"
- In the xaml, with the image tag selected, use the properties windows to select the Source dropdown since NOW the image appears in the dropdown list! This let visual studio format the string for me.
The string visual studio formatted for my image was:
<Image Source="/Paperless.Common;component/Resources/Checkbook.png" />
where "Paperless.Common" was the name of my assembly, and "Checkbook.png" was my image that resided in a "Resources" folder.
For completeness, the above URI is a Resource File Pack URI, where the URI prefix part is automatically added by the XAML Parser.
The full URI - which you would have to use in code behind - has a pack://application:
prefix and would also work in XAML:
Source="pack://application:,,,/Paperless.Common;component/Resources/Checkbook.png"
siteoforigin
is for files copied to the executable directory or subdirectories, for resources you should use application
as authority as far as i know.
SiteOfOrigin:
Path: The name of the site of origin file, including its path relative to the location from which the executable assembly was launched.
Well, I waisted more than 3 hours of my time on this problem. Your answers helped me to partially solve the problem and that's what urged me to add to this useful discussion.
I got my problem solved as:
- select all images in your solution explorer resources.
- Right click and go to properties.
- Build action to "Resource"
After all of that I found my self when I use the image multiple times or try to change to another image on the same image control to change the path for the image either on XAML or in the image control properties to be:
Source="pack:://application:,,,/Resources/image.png"