Frage

Ich lade Benutzer Bilder mit Silverlight 3. Alles funktioniert gut und ich kann die Datei-Stream zu einem BitmapImage gesetzt und es wird OK gemacht werden.

Das Problem ist, dass, wenn ich versuche, etwas zu laden, das nicht ein Bild ist (wie ein EXE, der .png umbenannt worden ist) Silverlight mit einem System.Exception stürzt die „Schwerwiegender Fehler“, sagt. Die MSDN-Dokumentation sagt wenig hilfreich, dass es so sollte es sein Msdn Link und ich das ImageFailed Ereignis hören soll (was nie gefeuert wird).

Bin ich dort etwas fehlt oder die Bibliothek gebrochen beim Laden von einem Strom?

Der Code Ich habe das Bild von der Quelle geladen:

        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
            }
        }
War es hilfreich?

Lösung

Das ist seltsam, aber du hast recht, es tut sich benehmen, die Art und Weise, auch auf Silverlight 4.

Es kann eine bessere Option sein, aber eine Weise, die ich an die Arbeit, um diese Ausnahmen gefunden haben, die nicht auf andere Weise behandelt werden kann, ist die App.Application_UnhandledException () -Methode zu ändern. Dies würde für Sie arbeiten:

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

Es ist ein Hack und ich weiß nicht wie, aber es ist besser als eine nicht behandelte Ausnahme.

BTW, sollten Sie wirklich einen Connect Bug auf dieses Verhalten einreichen. Es scheitert ganz sicher das „Gesetz des am wenigsten Astonishment“. Siehe Tim Heuer Beitrag auf, wie man die Fehler erhalten abgelegt: http://timheuer.com/blog/archive/2010/05/03/ways-to-give-feedback-on-silverlight.aspx

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top