Silverlight Path Resolution

First let me explain path resolution.  I have an image I want to use as my XAML button.  Easy enough I add an Image control to my Control Template for the button and set the source to the path of that image.  Path resolution is how the compiler determines where that image is.  I could hard code it to a directory on my hard drive, but that is never good because that path could change between environments or client machines.  So, most times we use relative paths.  Combinations of  ".." and slashes help us navigate the application structure to find the files.

Path resolution in XAML can be hard to understand.  It get's even more complicated once you add Silverlight to the mix.  Then on top of that when you are using User Controls from a Silverlight User Control Library things get down right nasty!  Then add in some shared Control Templates and things get darn near impossible.  That is the scenario I'm in.  I ran across this article which helped me get it working.  Check it out.

One important thing to note is how you must mark the properties on the images.  Depending on how you are going to access the files you have to either set the Build Action property as "Content" or "Resource".  You might also need to set the Copy To Output Directory property to "Copy Always".

Another thing to note in my case is that if the Control Template is a shared\application level template, the images must reside in the application project, not the control library.  The path used in the shared Control Template is related to the application not the control library.

The most important thing is to pick a one way and stick to that throughout the application.

Posted in Labels: , , |


  1. Anonymous Says:

    I’d passion to be sensible that too!