我得到了下面的代码,它试图将网络中的图像加载到图像控件中,当我运行它时,我在给定的行上收到一个错误,提示不允许网络访问:
private void button1_Click(object sender, RoutedEventArgs e)
{
WebClient webClientImgDownloader = new WebClient();
webClientImgDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(webClientImgDownloader_OpenReadCompleted);
webClientImgDownloader.OpenReadAsync(new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute));
}
void webClientImgDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.Result); // ERROR HERE!
image1.Source = bitmap;
}
适用于 Windows Phone 7 的 Silverlight
最佳答案
尝试使用 WebClient 下载内容将需要在源服务器上存在客户端访问策略文件。对于图像,您可以通过这样做来避免此要求:-
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute)
image1.Source = new BitmapImage(uri);
}
关于Silverlight Windows Phone 7 : Load Images From URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2455195/