16

我有一个 WPF 应用程序,一旦我将它安装到没有安装开发环境的机器上,它就会崩溃——如果这是一个骗子,我欢迎关闭,但我的 search-fu 没有找到等效的问题。看来我得到了 XamlParseException,但没有什么比这更有用的了。我需要得到有用的信息。

浏览 Windows 7 事件日志给了我这个错误日志:

Fault bucket , type 0
Event Name: CLR20r3
Response: Not available
Cab Id: 0

Problem signature:
P1: MyApp.exe
P2: 1.0.0.0
P3: 4b88323d
P4: PresentationFramework
P5: 3.0.0.0
P6: 4a174fbc
P7: 624f
P8: e1
P9: System.Windows.Markup.XamlParse
P10: 

Attached files:
C:\Users\Mark\AppData\Local\Temp\WER7DC.tmp.WERInternalMetadata.xml

These files may be available here:
C:\Users\Mark\AppData\Local\Microsoft\Windows\WER\ReportArchive
 \AppCrash_generatortestbed_4fa7dff09a9e893eb675f488392571ced4ac8_04ef1100

Analysis symbol: 
Rechecking for solution: 0
Report Id: cd55060c-271f-11df-b6ff-001e52eefb8e
Report Status: 1

我检查了这些目录,第一个不存在,而第二个包含一个仅列出加载的 dll 的 wer 文件。

我可以在我的测试机器上安装一个开发环境,但是它不能成为测试机器,我又回到了原点。安装开发环境后我没有收到此错误,因此我不知道如何获得详细、有用的错误消息。

编辑:基于@Alastair Pitts 的评论,下面是我填写异常处理的方式:

    private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
        Exception theException = e.Exception;
        string theErrorPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\GeneratorTestbedError.txt";
        using (System.IO.TextWriter theTextWriter = new System.IO.StreamWriter(theErrorPath, true)){
            DateTime theNow = DateTime.Now;
            theTextWriter.WriteLine("The error time: " + theNow.ToShortDateString() + " " + theNow.ToShortTimeString());
            while (theException != null) {
                theTextWriter.WriteLine("Exception: " + theException.ToString());
                theException = theException.InnerException;
            }
        }
        MessageBox.Show("The program crashed.  A stack trace can be found at:\n" + theErrorPath);
        e.Handled = true;
        Application.Current.Shutdown();
    }

希望我能以这种方式得到我需要的东西。谢谢您的帮助!

4

4 回答 4

15

我将使用的过程是处理应用程序域中的UnhandledException 事件。

完成此操作后,您有多种选择。将异常记录到文件中,将其序列化以供以后检查,显示带有异常消息的对话框。

编辑:XamlParseException在创建主窗口时发生。这意味着正在调用该窗口的构造函数。如果您在该构造函数中执行任何逻辑,任何产生的异常都会抛出一个XamlParseException. 据我了解,UnhandledException处理程序仍会捕获此异常。

UnhandledException在 WPF 中连接事件,请在 app.xaml 中添加事件连接

<Application 
   x:Class="DispatcherUnhandledExceptionSample.App"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   StartupUri="MainWindow.xaml"     
   DispatcherUnhandledException="App_DispatcherUnhandledException" />

然后在您的 app.cs 中添加一个方法

public partial class App : Application
{
    void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        // Process unhandled exception do stuff below

        // Prevent default unhandled exception processing
        e.Handled = true;
    }
}

MSDN

于 2010-03-04T00:16:44.953 回答
2

除了有一个记录堆栈跟踪的未处理异常事件处理程序之外,查看在重现机器上生成的故障转储也很有用。您提到它是 Windows 7,因此您可以通过以下方式查找相应的故障转储:

  1. 控制面板 -> 所有控制面板项目 -> 操作中心 -> 维护/“检查未报告问题的解决方案”区域下方的“查看要报告的问题”链接。这会显示已崩溃的应用程序列表,您可以深入了解它们以查找转储文件和信息。在问题技术详细信息的左下方查找“查看这些文件的临时副本”链接。这将提取转储文件并在资源管理器窗口中显示给您。

  2. cd /d %ProgramData%\Microsoft\Windows\WER。

WER 似乎将它的故障转储保存在该目录下,所以我所做的是一个 dir /s/b 用于我知道将在那里的部分应用程序名称。例如,我故意制作了一个名为 crashyapp.exe 的崩溃应用程序:

C:\ProgramData\Microsoft\Windows\WER>dir /s/b *crashy*
C:\ProgramData\Microsoft\Windows\WER\ReportQueue\AppCrash_crashyapp.exe_56f8d710d72e31d822d6b895c5c43a18d34acfa1_cab_2823e614
    
该目录包含崩溃的 hdmp 和 mdmp 文件。是时候安装调试器了!

于 2010-11-03T01:03:18.400 回答
1

异常日志记录(如 Alastair Pitts 的回答)将有助于确定错误的来源。

P9 行上存在 XamlParse 表明从 XAML 描述初始化控件或窗口可能存在问题。XAML 引用的程序集可能在目标计算机上找不到,或者与 XAML 中的签名不匹配。

编辑:

XAML 解析发生在 InitializeComponent() 中,该函数在窗口或控件的构造函数中调用

于 2010-03-04T00:29:44.583 回答
0

除了 Alistair 的回答之外,正是 InnerException 给了我正在寻找的线索:

public partial class App : Application {
    void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) {
        Exception ex = e.Exception;
        Exception ex_inner = ex.InnerException;
        string msg = ex.Message + "\n\n" + ex.StackTrace + "\n\n" +
            "Inner Exception:\n" + ex_inner.Message + "\n\n" + ex_inner.StackTrace + "\n\n" + 
            Utils.RegistryVersion();
        MessageBox.Show(msg, "Drop Print Batch Application Halted!", MessageBoxButton.OK);
        Utils.MailReport(msg);
        e.Handled = true;
        Application.Current.Shutdown();
    }
}
于 2016-01-14T01:40:06.407 回答