Search code examples
wpfimagepack-uri

At design time pack uri is valid, but not at runtime?


I'm setting a Button's content to an Image. It looks something like this:

<Button>
   <Image Source="pack://application:,,,/NavigationImages/nav_up_left.png" />
</Button>

In my project I have a subfolder named NavigationImages and within that folder is the image file nav_up_left.png.

When I view the Designer the image appears, however during runtime I get an IOException error saying it cannot locate the resource.

The Build Action is set to Resource.

Actually, this worked fine in one project. But when I copied it over the another project it fails. This seems like an incredibly simple problem, but I find myself stumped and ready to start pulling out hair. @_@

Your thoughts and suggestions will be much appreciated!


Solution

  • Whelp, I figured it out...kinda.

    I copied that xaml code from one project where the output type is Windows Application, to another project where the output type is Class Library.

    I didn't think of it at the time, but apparently when the output type is a Class Library the pack URI needs to change.

    So instead of "pack://application:,,,/NavigationImages/nav_up_left.png" I changed it to "/ProjectName;component/NavigationImages/nav_up_left.png" and now it's working just fine.

    I'm not 100% clear why this is works and not the former. I've read through the MSDN documentation on pack URIs in WPF but perhaps I misinterpreted something.

    I'll leave this answer unchecked in the event someone can give me a good explanation why what I previously had doesn't work in a project with output type Class Library.

    I'm probably missing something really simple. @_@