2010-05-06 104 views
3

我的Silverlight项目的目录结构是这样的:的Silverlight:下载本地文件与Web客户端

\Bin 
- MainModule.xap 
- \Images 
--- Image1.png 
--- Image2.png 
- \Modules 
--- SubModule.xap 

我希望能够通过任何Web服务器或通过Visual Studio直接运行(调试目的我想绕过内容下载)。

在我的媒体加载代码我这样做如下:

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

这适用于图像,但我也有子模块,其只是组件住房UserControl秒。由于Silverlight无法读取磁盘,因此我已经辞职了,因为我将不得不“下载”XAP,因为我需要的是否是本地运行的。问题是,如果我在本地运行该项目,并尝试使用WebClient下载XAP我得到一个异常:

System.Net.WebException: An exception occurred during a WebClient request. ---> System.NotSupportedException: The URI prefix is not recognized. 

有没有运行时,我可以让我的子模块XAPs任何方式(Web客户端或其他方式)直接Silverlight项目而不是击中Web服务器?

编辑:

忘了提我使用MEF与DeploymentCatalog也试过,虽然我没有得到一个例外,从来都没有撰写所以我只能假定它有同样的问题。

回答

0

我的设计试图访问文件关闭未在Silverlight允许本地磁盘。我仍然没有一个好的答案,说明为什么你可以在Visual Studio中从磁盘运行Silverlight应用程序,但是它可以成功地从磁盘读取图像/视频/音频。

0

如果您不想一次又一次地从服务器上下载相同的文件(在调试过程中),请使用WebClient从服务器下载一次,然后将其存储在IsolatedStorage上。

下面的代码将让你开始:

// read/write from/to IsolatedStorage 
IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForSite.OpenFile