如何处理WPF应用程序实例化过程中抛出的异常?
我使用以下代码在 WPF 应用程序中显示未处理的异常:
public MyApplication() {
this.DispatcherUnhandledException += (o, e) => {
var exceptionMessage = new ExceptionWindow();
exceptionMessage.ExceptionMessage.Text = e.Exception.Message;
exceptionMessage.ExceptionCallStack.Text = e.Exception.StackTrace;
exceptionMessage.ExceptionInnerException.Text = e.Exception.InnerException.Message;
exceptionMessage.WindowStartupLocation = WindowStartupLocation.CenterScreen;
exceptionMessage.WindowStyle = WindowStyle.ToolWindow;
exceptionMessage.ShowDialog();
e.Handled = true;
Shell.Close();
};
}
事实证明,在应用程序实例化期间出现异常,因此应用程序构造函数永远不会执行。
重现它的一种简单方法(有不同的例外)是引入额外的“<”在应用程序配置文件中的某些标记之前并运行它。
在调用应用程序构造函数之前会出现类似这样的无用错误消息。 替代文本 http://srtsolutions。 com/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikewoelmer/ExceptionWPF1_5F00_1C1F39AA.jpg
有谁知道如何捕获此类异常?
备注:我正在使用 Caliburn 并且我的应用程序扩展了 CaliburnApplication。
I'm using the following code to display unhandled exceptions in a WPF application:
public MyApplication() {
this.DispatcherUnhandledException += (o, e) => {
var exceptionMessage = new ExceptionWindow();
exceptionMessage.ExceptionMessage.Text = e.Exception.Message;
exceptionMessage.ExceptionCallStack.Text = e.Exception.StackTrace;
exceptionMessage.ExceptionInnerException.Text = e.Exception.InnerException.Message;
exceptionMessage.WindowStartupLocation = WindowStartupLocation.CenterScreen;
exceptionMessage.WindowStyle = WindowStyle.ToolWindow;
exceptionMessage.ShowDialog();
e.Handled = true;
Shell.Close();
};
}
Turns out that I have an exception during the instantiation of the application, so the app constructor is never executed.
A simple way to reproduce it (with a different exception) is by introducing an extra "<" before some tag in your app's configuration file and run it.
A useless error message like that appears before the application constructor get called.
alt text http://srtsolutions.com/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mikewoelmer/ExceptionWPF1_5F00_1C1F39AA.jpg
Does anyone know how to catch such kind of exceptions?
Remark: I'm using Caliburn and my application extends CaliburnApplication.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的。我通过执行以下操作解决了该问题:
将
App.xaml
文件的Build Action
从ApplicationDefinition
更改为Page
。创建一个如下所示的新类:
它用这个方法替换生成的 App.g.cs Main 方法,这样我们就有机会捕获异常。
Okay. I solved the problem by doing the following:
Change the
Build Action
of theApp.xaml
file fromApplicationDefinition
toPage
.Create a new class like following:
It replaces the generated App.g.cs Main method by this one, so we have a chance to catch the exceptions.