我不认为你需要一个app.xaml来注册这些事件。
如果你想做到这一点,在你的类的一个静态初始化,将肯定被任何人使用控制库使用添加以下代码:
if (AppDomain.CurrentDomain != null) {
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
if (Dispatcher.CurrentDispatcher != null) {
Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_UnhandledException;
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
// Do something with the exception in e.ExceptionObject
}
static void CurrentDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) {
// Do something with the exception in e.Exception
}
我同意你的看法,是不是好的杀死库中的异常。但我不想杀死例外。我对处理它们感兴趣,可以处理一个异常,甚至让它继续向上抛出。还有一点是,我没有分发我的图书馆,我用它来达到一个非常特定的目的。这个库将成为系统的一部分,但它不在.net中的系统。所以我需要将例外的日志放入我的库中。 –