Monday, August 11, 2008

An Answer and A Question

This afternoon a colleague was attempting to add an image to his WPF application. What made this unusual was that he wanted to get the image from a resource cooked into an assembly. This was not particularly straightforward to do.

The image itself was in c:\dev\project\framework\IntranetFramework\Content\Images\CSiLogo.png. And we were putting into the IntranetFramework assembly. That seemed to go straightforwardly, but there arose a problem when we went to use it.

Ultimately we added to the xaml file the following:

Source="/IntranetFramework;component/Content/Images/CSiLogo.png">



What's interesting is the syntax that we had difficulty getting right. Don't forget the initial "slash" followed by the assembly name delimited by semi-colon followed by "component" followed by the path within the project's directory tree to the image in question.

It took a fair amount of googling to add the "/" and the "component/" to the image source attribute.

You might keep that in mind if you're struggling with something like this.

It was my friend who did all the work, but I just sat watching. When I thought I understood what he did, I did likewise with a different image file I added to the project and then added to another xaml. Everything the same except the different filename.

It didn't work. Then my friend told me what he'd done before I dropped by. He added the image file to the project as a Resource, then immediately deleted it again. Whereupon my version started working. I don't exactly know why; I figure it's a bug in Visual Studio 2008's WPF code generator. If anybody figures it out, please add a comment explaining it.

No comments: