Silverlight: Download local files with WebClient
- by David
The directory structure of my Silverlight project is like the following:
\Bin
- MainModule.xap
- \Images
--- Image1.png
--- Image2.png
- \Modules
--- SubModule.xap
I want to be able to run it through either a web server or through Visual Studio directly (for debugging purposes I want to bypass content downloading).
In my media loading code I do something like the following:
if (runningLocally)
{
var bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri("Images/Image1.png", UriKind.Relative);
var image = new Image();
image.Source = bitmapImage;
}
else
{
WebClient wc = new WebClient();
wc.OpenReadCompleted += (s, e) =>
{
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(e.Result);
var image = new Image();
image.Source = bitmapImage;
};
wc.OpenReadAsync(new Uri("Images/Image1.png", UriKind.Relative));
}
This works for images but I also have sub-modules which are just assemblies housing UserControls. Since Silverlight has no ability to read disk I've resigned myself to the fact that I'm going to have to "download" the XAPs I need whether I'm running locally or not. Problem is if I run the project locally and try to use a WebClient to download a XAP I get an exception:
System.Net.WebException: An exception occurred during a WebClient request. ---> System.NotSupportedException: The URI prefix is not recognized.
Is there any way (WebClient or otherwise) I can get to my sub-module XAPs when running the Silverlight project directly rather than hitting a web server?