0

我正在使用 Silverlight 3 加载用户图像。一切正常,我可以将文件流设置为 aBitmapImage并且渲染正常。

问题是,如果我尝试加载不是图像的内容(例如重命名为 .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
            }
        }
4

1 回答 1

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); });
    }
}

这是一个 hack,我不喜欢它,但它比未处理的异常要好。

顺便说一句,您真的应该为此行为提交一个 Connect 错误。它肯定不符合“最小惊讶法则”。请参阅 Tim Heuer 关于如何提交错误的帖子:http: //timheuer.com/blog/archive/2010/05/03/ways-to-give-feedback-on-silverlight.aspx

于 2010-09-21T16:58:57.667 回答