Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight image: load URL dynamically?

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?

like image 321
pearcewg Avatar asked Oct 24 '08 02:10

pearcewg


2 Answers

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");

like image 112
Craig Nicholson Avatar answered Oct 08 '22 02:10

Craig Nicholson


In the code behind or a value converter you can do

  Uri uri = new Uri("http://testsvr.com/hello.jpg");
  YourImage.Source = new BitmapImage(uri);
like image 27
Aaron Fischer Avatar answered Oct 08 '22 02:10

Aaron Fischer