C# 检查图像是否已加载
你好,我有这个代码:C# 检查图像是否已加载,c#,windows-phone-7,windows-phone-8,C#,Windows Phone 7,Windows Phone 8,你好,我有这个代码: BitmapImage image = new BitmapImage(new Uri(contact.PreviewPhoto, UriKind.Relative)); a.photo.Source = image; contact.PreviewPhoto="https://files.test.lv/public/photos/h66/38B83D35-15B5-4872-8CE5-F3E50001E921.jpg"; 如何确定图像是否已加载 因为有时intern
BitmapImage image = new BitmapImage(new Uri(contact.PreviewPhoto, UriKind.Relative));
a.photo.Source = image;
contact.PreviewPhoto="https://files.test.lv/public/photos/h66/38B83D35-15B5-4872-8CE5-F3E50001E921.jpg";
如何确定图像是否已加载
因为有时internet连接可能会丢失,或者链接是空白的,在这种情况下如何替换它?我不知道是否已加载检查图像。但这里有一个示例,用于说明图像是否存在
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("YourImageUrl");
request.Method = "HEAD";
try
{
request.GetResponse();
return true;
}
catch
{
return false;
}
是的,多亏了Fabrice,正确答案是:
BitmapImage image = new BitmapImage(new Uri(contact.PreviewPhoto));
image.ImageFailed += (s, e) =>
{
image = new BitmapImage(new Uri("/no-picture.png", UriKind.Relative));
a.photo.Source = image;
};
看起来你更多的是寻找未加载的而不是加载的 您可以侦听BitmapImage的事件,如DecodeFailed或DownloadFailed:(msdn.microsoft.com/library/system.windows.media.imaging.BitmapImage_events.aspx) 如果.photo的类型为Image,则还可以侦听事件ImageFailed和ImageOpened(msdn.microsoft.com/library/windowsphone/developer/system.windows.controls.Image_events(v=vs.105).aspx)检查ImageOpened事件: