如何确定从Silverlight 2.0(RC0)中的URI成功加载的图像?

时间:2021-12-24 20:20:15

In the following code below:

在以下代码中:

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

how can I determine if the image successfully loaded (when there's a valid URI)? i.e., The URI is a valid format, but the file may not exist.

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

2 个解决方案

#1


1  

Image has an ImageFailed event.

Image有一个ImageFailed事件。

BitmapSource (base for BitmapImage) has an IsDownloading property, as well as DownloadProgress, DownloadCompleted, and DownloadFailed events.

BitmapSource(BitmapImage的基础)具有IsDownloading属性,以及DownloadProgress,DownloadCompleted和DownloadFailed事件。

#2


0  

If you run your example code above (with a valid url but invalid image file) you will get an exception thrown:

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

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

So if you wrap your code in a try/catch block you can determine if the image loaded property or not.

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

#1


1  

Image has an ImageFailed event.

Image有一个ImageFailed事件。

BitmapSource (base for BitmapImage) has an IsDownloading property, as well as DownloadProgress, DownloadCompleted, and DownloadFailed events.

BitmapSource(BitmapImage的基础)具有IsDownloading属性,以及DownloadProgress,DownloadCompleted和DownloadFailed事件。

#2


0  

If you run your example code above (with a valid url but invalid image file) you will get an exception thrown:

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

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

So if you wrap your code in a try/catch block you can determine if the image loaded property or not.

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