【问题标题】:Debug WPF on client machine在客户端机器上调试 WPF
【发布时间】:2010-08-17 12:39:17
【问题描述】:

在 wpf 应用程序中捕获错误的最佳方法是什么。我已将以下内容添加到我的 app.xaml.cs:

void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{

    var stringBuilder = new StringBuilder();
    stringBuilder.AppendFormat("{0}\n", e.Exception.Message);
    stringBuilder.AppendFormat(
            "Exception handled on main UI thread {0}.", e.Dispatcher.Thread.ManagedThreadId);

    // attempt to save data
    var result = MessageBox.Show(
                    "Application must exit:\n\n" + stringBuilder.ToString() + "\n\nSave before exit?",
                    "app",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Error);
    if (result == MessageBoxResult.Yes)
    {
        MessageBox.Show(e.Exception.InnerException.ToString());
        MessageBox.Show(e.Exception.InnerException.Message.ToString());

    }

    // Return exit code
    this.Shutdown(-1);

    // Prevent default unhandled exception processing
    e.Handled = true;
}
private void Application_Startup(object sender, StartupEventArgs e)
{
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}

void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Exception ex = e.ExceptionObject as Exception;
    MessageBox.Show(ex.Message, "Uncaught Thread Exception", MessageBoxButton.OK, MessageBoxImage.Error);
}

但我仍然遇到应用程序崩溃并且错误未显示在消息框中的情况。如何调试这些类型的错误? windows的事件日志没有显示任何有用的信息。当然,该应用程序在 IDE 中运行良好,所以这也无济于事。

【问题讨论】:

    标签: wpf


    【解决方案1】:

    在 IDE 中你可以捕捉到它。转到
    调试 -> 异常。

    在对话框中,选中Common Language Runtime Exception的抛出复选框。

    现在,您可以从 IDE 中捕获它了。

    HTH

    【讨论】:

    • 很抱歉,您的回答没有回答我的问题:当我的错误处理没有捕获异常但应用程序只是说它停止工作
    猜你喜欢
    • 2011-11-13
    • 1970-01-01
    • 2011-12-02
    • 2013-01-17
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    相关资源
    最近更新 更多