1

我使用下面的代码从我的 winodws phone 7 应用程序中的 URL 加载图像。

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

它对我来说很好。但是图像是异步加载的,当我想在那里显示某种忙碌指示符时,如果此类 URL 上不存在图像,那么我想显示一些默认图像。我怎样才能做到这一点?

4

1 回答 1

2

我认为如果您订阅了Image.ImageFailed 事件,您应该能够在图像不存在的情况下显示默认图像。

可能发生此事件的条件包括:

  1. 文件未找到。
  2. 文件格式无效(无法识别或不受支持)。
  3. 上传后未知文件格式解码错误。

所以这样的事情可能对你有用:

image1.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(handlerImageFailed);
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);

void handlerImageFailed(object sender, ExceptionRoutedEventArgs e)
{
     // Show the default image
}
于 2011-11-16T14:07:12.950 回答