Detecting Xamarin.Forms Binding Errors in the Output Window

I was browsing through the Xamarin.Forms issues, and found this particular one. It describes adding the ability to detect binding errors in Xamarin.Forms. I was triggered by this because I think it would make a nice addition. Detecting binding errors can help to prevent you from spending precious time on a typo. This functionality is not on the roadmap yet, but I did find a way to hack this into your own apps, I thought I would share it with you.

The original post was updated, stating that in the internals of the BindingExpression class there are already some log statements in place that actually output these kinds of errors! I did a bit of research, trying to get these messages in the output of our app, but that didn’t work out of the box. Some solutions online state that adding the TRACE symbol to the compiler symbols, but that doesn’t seem to work.

Then I started looking into the Log class that is in the Xamarin.Forms.Internals namespace. Since it is marked as internal and even has an attribute to keep it out of the IntelliSense, it feels a bit hacky to be working with it, but it does work for now.

Edit: I have worked with Neil to get this easier right into Forms for you, check the actual status here: https://github.com/xamarin/Xamarin.Forms/pull/2911

How To Make It Work Today

Making it work is actually pretty easy. You just need one line:

With the top line, we hook up the internal Xamarin.Forms logger to our own Debug.WriteLine. Now, whenever a trace happens from the Forms code, it will also show up in our application output making it easier to detect binding errors. Please note; this will cause all traces to show up in your application output.

If you have any other mechanism that you want to use, that is possible as well. In the DelegateLogListener you can execute any type of code that helps you detect these errors. When we now run the app which has a binding error, it will show up nicely.

Binding errors in the output window
Binding errors in the output window

I have put a little sample app online for you to check out the details, you can find it here: https://github.com/jfversluis/BindingFailureOutputSample

1 thought on “Detecting Xamarin.Forms Binding Errors in the Output Window

Comments are closed.