我正在使用Silverlight 3加载用户图像。
一切正常,我可以将文件流设置为BitmapImage
,然后呈现正常。
问题在于,如果我尝试加载不是图像的东西(比如已经重命名为.png的.exe),Silverlight会崩溃,其中System.Exception
会出现“灾难性故障”。
MSDN文档无益地说它应该是msdn link,我应该听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
}
}
答案 0 :(得分:0)
这很奇怪,但是你是对的,即使在Silverlight 4上也是如此。
可能有更好的选择,但我发现解决这些无法处理的异常的一种方法是修改App.Application_UnhandledException()方法。这对你有用:
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// Handle stupid image load exception. Can't think of a better way to do it -- sorry.
if (e.ExceptionObject is System.Exception && e.ExceptionObject.Message.Contains("HRESULT") && e.ExceptionObject.Message.Contains("E_UNEXPECTED"))
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Error loading image.");
});
e.Handled = true;
return;
}
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
}
}
这是一个黑客,我不喜欢它,但它比未处理的例外更好。
顺便说一句,你真的应该在这个行为上提交一个Connect错误。它肯定没有通过“最小惊讶法则”。请参阅Tim Heuer关于如何获取错误的帖子:http://timheuer.com/blog/archive/2010/05/03/ways-to-give-feedback-on-silverlight.aspx