1

在下面的代码中:

Image img = new Image();
img.Source = new BitmapImage(new Uri("http://someURL/somefilename.jpg", UriKind.Absolute));

如何确定图像是否成功加载(当有有效的 URI 时)?即,URI 是有效格式,但文件可能不存在。

4

2 回答 2

1

Image有一个ImageFailed事件。

BitmapSource(base for BitmapImage) 有一个IsDownloading属性,还有DownloadProgress, DownloadCompleted, 和DownloadFailedevents。

于 2008-10-08T18:12:21.897 回答
0

如果您运行上面的示例代码(使用有效的 url 但无效的图像文件),您将抛出异常:

Error: Sys.InvalidOperationException: ImageError error #4001 in control 'Xaml1': AG_E_NETWORK_ERROR

因此,如果您将代码包装在 try/catch 块中,您可以确定图像是否加载了属性。

于 2008-10-09T21:26:57.027 回答