【发布时间】:2011-06-19 10:38:47
【问题描述】:
我正在寻找一种应用程序范围的方法来捕获由数据绑定属性 getter(和 setter,但没有太多困难)引发的异常。
这些事件都不会捕获 getter 抛出的异常:
AppDomain.CurrentDomain.UnhandledException
Application.Current.DispatcherUnhandledException
Application.Current.Dispatcher.UnhandledException
另一个想法是使用带有 UpdateSourceExceptionFilter 的自定义绑定类,如 this thread 中所述。不幸的是,这个方法只捕获属性设置器中的异常,而不是获取器。
我看到的最后一个选项是使用 PresentationTraceSources 跟踪侦听器:
PresentationTraceSources.Refresh();
PresentationTraceSources.DataBindingSource.Listeners.Add(new PresentationLoggingTraceListener());
PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.Error;
这种方法似乎确实可以满足我的要求。不幸的是,它只给了我一个字符串,而不是一个异常,这意味着我必须做一些解析工作才能得到实际的错误。
TraceListener 方法最终可能会起作用,但它似乎有点 hackish。是否还有其他我遗漏的选项,或者我几乎被 TraceListener 卡住了?
【问题讨论】:
-
我想看看这个问题的答案
标签: wpf exception-handling binding