0

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
            }
        }
4

1 に答える 1

0

奇妙ですが、Silverlight 4 でもそのように動作します。

より良いオプションがあるかもしれませんが、他の方法では処理できないこれらの例外を回避する 1 つの方法は、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

于 2010-09-21T16:58:57.667 に答える