Silverlight:使用WebClient下载本地文件

3
我的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
如果您不想一遍又一遍地从服务器下载相同的文件(在调试时),可以使用 WebClient 从服务器下载一次,然后将其存储在 IsolatedStorage 中。
以下代码将帮助您入门:
// read/write from/to IsolatedStorage
IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForSite.OpenFile

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接