Silverlight image: load URL dynamically

silverlightsilverlight-2.0

I'm tinkering with Silverlight 2.0.

I have some images, which I currently have a static URL for the image source.
Is there a way to dynamically load the image from a URL path for the site that is hosting the control?

Alternatively, a configuration setting, stored in a single place, that holds the base path for the URL, so that each image only holds the filename?

Best Answer

From what I gather you aren't trying to change the image itself dynamically, but rather to correctly determine the location of the image at runtime.

I believe simply prefixing the image relative URL with "../" should get you to the root of your application, not necessarily the site as the application might not be hosted in the root of a site.

If your XAP file is located as follows:

http://somesite.foo/app1/somethingelse/clientbin/MyFoo.xap

And you where trying to link the following image:

http://somesite.foo/app1/somethingelse/images/a/boo.png

Apparently all relative URI's are relative to where the XAP file is located (ClientBin folder typically) and Silverlight appends the current Silverlight client namespace. So if you Silverlight control is in the namespace Whoppa you would need to put all your images in the clientbin/Whoppa/ directory. Not exactly convenient.

The workaround is to use absolute URIs as follows:

new Uri(App.Current.Host.Source, "../images/a/boo.png");