tags:

views:

22

answers:

1

Loading images in Silverlight has many options depending on what you want to do, but this small problem has me slightly confused. Hopefully somebody here can help me out!

In the web project, within the ClientBin directory I have a directory called Images which contains a png file.

In the MainPage.xaml file, I can create an Image object, call it mainImage and then in the code behind use this in the constructor:

BitmapImage src = new BitmapImage(new Uri("Images/myImage.png", UriKind.Relative)); mainImage.SetValue(Image.SourceProperty, src);

This all works fine, however when I create a UserControl with the same functionality and then create an instance of that control on the MainPage.xaml the application runs but I get no images. Why is this?

I will also point out that the new user control has been created in a Controls folder and so has a different namespace (solutionName.Controls or whatever)

Any help would be great, and if you need any more info please ask (tried to keep it brief)

Kris

+1  A: 

Change how you create the URI for the image to be this instead:

Uri url = new Uri(Application.Current.Host.Source, "Images/myImage.png");
BitmapImage src = new BitmapImage(url);

This will use the xap as the root uri and then make the url relative to that so it will work regardless of what the current url is.

Bryant