WPF ShowDialog在窗口加载期间吞咽异常 [英] WPF ShowDialog swallowing exceptions during window load

查看:547
本文介绍了WPF ShowDialog在窗口加载期间吞咽异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Window类中的ShowDialog方法显示WPF窗口对话框,就像在主窗口上按下按钮一样。

A WPF window dialog is shown using the ShowDialog method in the Window class like when a button is pressed on the main window, like this.

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var window = new Window1();
                window.ShowDialog();
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show("I am not shown.");
            }
        }

窗口中已加载的事件在xaml中订阅,这个:

The window has a Loaded event subscribed in the xaml like this:

<Window x:Class="Stackoverflow.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Loaded="Window_Loaded">
    <Grid />
</Window>

Window_Loaded事件中抛出异常

An exception is thrown in the Window_Loaded event

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        throw new ApplicationException();
    }

然而,ShowDialog调用的catch没有捕获异常,呼叫返回。吞咽例外,窗口仍然显示。

However the exception is not catched by the catch around the ShowDialog call, nor does the call return. The exception is swallowed and the window still displayed.

为什么会发生这种情况,我将如何处理WPF窗口的Window_Loaded事件中的异常?我必须在事件处理程序中抓住它并手动处理窗口?

Why does this happen and how would I go about handling an exception in the Window_Loaded event of a WPF window? Do I have to catch it in the event-handler and Dispose the window manually?

在WinForms中,您需要调用 Application.SetUnhandledExceptionMode(UnhandledExceptionMode。 ThrowException)

In WinForms you need to call Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException)

为了让异常通过ShowDialog调用发泡。有没有类似的开关需要在WPF上设置?

in order to let exceptions bubble through ShowDialog calls. Is there a similar switch that needs to be set on WPF?

推荐答案

我只在x64机器上看到这个问题,用任何Cpu编译的代码。
更改您的程序以编译x84可能会修复它,但是我自己已经有问题,根据我们的程序集。_
我唯一的代码建议是以下,甚至不保证拿起来
捕获异常,并将其重新抛出在后台工作器中。

I've only seen this issue on x64 machines, with code compiled with Any Cpu. Changing your program to compile as x84 may fix it, but I've had problems there myself depending on our assemblies.
My only code suggestion is the following, and even then it's not guaranteed to pick it up. Catch the exception, and re-throw it in a Background worker.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    try
    {
        /// your code here...
        throw new ApplicationException();
        /// your code here...
    }
    catch (Exception ex)
    {
        if (IntPtr.Size == 8)   // 64bit machines are unable to properly throw the errors during a Page_Loaded event.
        {
            BackgroundWorker loaderExceptionWorker = new BackgroundWorker();
            loaderExceptionWorker.DoWork += ((exceptionWorkerSender, runWorkerCompletedEventArgs) => { runWorkerCompletedEventArgs.Result = runWorkerCompletedEventArgs.Argument; });
            loaderExceptionWorker.RunWorkerCompleted += ((exceptionWorkerSender, runWorkerCompletedEventArgs) => { throw (Exception)runWorkerCompletedEventArgs.Result; });
            loaderExceptionWorker.RunWorkerAsync(ex);
        }
        else
            throw;
    }
}

这篇关于WPF ShowDialog在窗口加载期间吞咽异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆