优雅地处理图像加载异常
我正在使用 Silverlight 3 加载用户图像。 一切工作正常,我可以将文件流设置为 BitmapImage 并渲染正常。
问题是,如果我尝试加载非图像的内容(例如已重命名为 .png 的 .exe),Silverlight 就会崩溃,并显示“灾难性故障”的 System.Exception
。 MSDN 文档毫无帮助地说它应该在那里 msdn 链接,我应该监听 ImageFailed
事件(该事件永远不会被触发)。
我是否遗漏了某些内容,或者从流加载时库是否损坏?
我从源加载图像的代码:
var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "*.jpg;*.jpeg;*.png|*.jpg;*.jpeg;*.png";
openFileDialog.Multiselect = false;
var showDialog = openFileDialog.ShowDialog();
if (showDialog.HasValue && showDialog.Value)
{
using (var reader = openFileDialog.File.OpenRead())
{
var picture = new BitmapImage();
picture.DownloadProgress += (o, e) => System.Threading.SynchronizationContext.Current.Send((oo) => System.Windows.Browser.HtmlPage.Window.Alert("Download progress: " + e.Progress), null);
picture.ImageFailed += (o, e) => System.Threading.SynchronizationContext.Current.Send((oo) => System.Windows.Browser.HtmlPage.Window.Alert("Image failed: " + e.ErrorException), null);
picture.ImageOpened += (o, e) => System.Threading.SynchronizationContext.Current.Send((oo) => System.Windows.Browser.HtmlPage.Window.Alert("Image opened: " + e.OriginalSource), null);
picture.SetSource(reader); // BANG! here without any of the alerts showing up
}
}
I'm loading user images using Silverlight 3.
Everything works fine and I can set the file stream to a BitmapImage
and it gets rendered OK.
The problem is that if I try to load something that's not an image (like a .exe that's been renamed to .png) Silverlight crashes with a System.Exception
that says "Catastrophic failure".
The MSDN documentation unhelpfully says that it should be so there msdn link and I should listen to the ImageFailed
event (which never gets fired).
Am I missing something there or is the library broken when loading from a stream?
The code I've got loading the image from the source:
var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "*.jpg;*.jpeg;*.png|*.jpg;*.jpeg;*.png";
openFileDialog.Multiselect = false;
var showDialog = openFileDialog.ShowDialog();
if (showDialog.HasValue && showDialog.Value)
{
using (var reader = openFileDialog.File.OpenRead())
{
var picture = new BitmapImage();
picture.DownloadProgress += (o, e) => System.Threading.SynchronizationContext.Current.Send((oo) => System.Windows.Browser.HtmlPage.Window.Alert("Download progress: " + e.Progress), null);
picture.ImageFailed += (o, e) => System.Threading.SynchronizationContext.Current.Send((oo) => System.Windows.Browser.HtmlPage.Window.Alert("Image failed: " + e.ErrorException), null);
picture.ImageOpened += (o, e) => System.Threading.SynchronizationContext.Current.Send((oo) => System.Windows.Browser.HtmlPage.Window.Alert("Image opened: " + e.OriginalSource), null);
picture.SetSource(reader); // BANG! here without any of the alerts showing up
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很奇怪,但你是对的,即使在 Silverlight 4 上,它也确实如此。
可能有更好的选择,但我发现解决这些无法处理的异常的一种方法是修改App.Application_UnhandledException() 方法。这对你有用:
这是一种黑客行为,我不喜欢它,但它比未处理的异常要好。
顺便说一句,您确实应该针对此行为提交一个 Connect 错误。它肯定不符合“最小惊讶法则”。请参阅 Tim Heuer 的帖子,了解如何提交错误:http://timheuer.com/blog/archive/2010/05/03/ways-to-give-feedback-on-silverlight.aspx
That's weird, but you're right, it does behave that way, even on Silverlight 4.
There may be a better option, but one way I've found to work around these exceptions that can't otherwise be handled is to modify the App.Application_UnhandledException() method. This would work for you:
It's a hack and I don't like it, but it's better than an unhandled exception.
BTW, you really ought to file a Connect bug on this behavior. It quite certainly fails the "Law of Least Astonishment". See Tim Heuer's post on how to get the bug filed: http://timheuer.com/blog/archive/2010/05/03/ways-to-give-feedback-on-silverlight.aspx