tags:

views:

276

answers:

2

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?

EDIT:

Forgot to mention I also tried using MEF with a DeploymentCatalog and while I don't get an exception, nothing ever composes so I can only assuming it has the same problem.

A: 

If you dont want to download the same files from the server over and over again (while debugging), download it from the server once using WebClient then store it on IsolatedStorage.

The following code will get you started:

// read/write from/to IsolatedStorage
IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForSite.OpenFile
Arvin Meralpis
A: 

My design was trying to access files off local disk which isn't allowed in Silverlight. I still don't have a good answer for why you can run a Silverlight application from disk in Visual Studio and it can successfully read images/video/audio from disk however.

David