Silverlight:使用WebClient下载本地文件

时间:2010-05-06 19:55:20

标签: silverlight

我的Silverlight项目的目录结构如下:

\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.

有没有办法(WebClient或其他方式)我可以直接运行Silverlight项目而不是点击Web服务器来访问我的子模块XAP?

编辑:

忘记提及我也尝试使用MEF和DeploymentCatalog,虽然我没有得到例外,但没有任何组合,所以我只能假设它有同样的问题。

2 个答案:

答案 0 :(得分:0)

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

以下代码将帮助您入门:

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

答案 1 :(得分:0)

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