WPF Databinding: catching exceptions thrown by property getters

I am looking for a method for the generally accepted method of catching exceptions created by getters of database properties (and setters, but performed without any special difficulties).

None of these events will catch the exceptions thrown by getters:

AppDomain.CurrentDomain.UnhandledException Application.Current.DispatcherUnhandledException Application.Current.Dispatcher.UnhandledException 

Another idea is to use your own binding class with UpdateSourceExceptionFilter, as described in this thread . Unfortunately, this method only catches exceptions in property installers, not getters.

The last option I've seen is to use the PresentationTraceSources trace listener:

  PresentationTraceSources.Refresh(); PresentationTraceSources.DataBindingSource.Listeners.Add(new PresentationLoggingTraceListener()); PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.Error; 

This method really does basically what I want. Unfortunately, this only gives me a string, not an exception, that is, I will have to figure out a bit of the actual error.

The TraceListener method will probably work in the end, but it seems a bit hacked. Are there any other options that I am missing, or am I pretty much stuck in TraceListener?

+8
exception-handling wpf binding
source share
1 answer

I would suggest an Aspect Oriented Programming (AOP) approach to this problem. This will allow you to enter code at compile time that wraps your getter with whatever you want, in this case try / catch and logging. The only thing I worked with was PostSharp Laos, a free version of PostSharp http://www.sharpcrafters.com .

I think this is a good place to start, http://www.richard-banks.org/2009/02/aspect-oriented-programming.html , in this example it migrates setters with the ability to automatically call NotifyPropertyChanged. There are several more AOP projects that you could use, but I have not used them. Here is a good list: http://www.bodden.de/tools/aop-dot-net . I would suggest weave based methods.

+1
source share

All Articles