Debugging hiccups in Xamarin and how to overcome them

Hey Guys!

When developing our projects sometimes we may face tedious bugs or debugging hiccups that may not seem obvious at first how to solve them, I decided to start recollecting the ones I have faced and here are a few with their solution.

Timespan Formatting

You must be careful when working with formating and different language cultures because not all of them format in the same way their dates or numbers for example TimeSpan in different cultures like Arabic, when transformed with toString Crashes the app with the error of “Not a valid calendar for a given culture”

Solution:

We can solve this issue, telling our project to not try and format it using the Arabic culture but instead keep the culture invariant.

We can enable this by sending a flag to our ToString method called “CultureInfo.InvariantCulture” which is used when you want culture neutral formatting that’s independent of the local system of our users.

Errors when navigating to Page?

Sometimes we have errors when navigating to a page in our app, that could be avoided or tracked down before we trigger them, for this, we always make sure to have XAML Compilation enabled which will grant the following benefits to us:

  • It performs compile-time checking of XAML, notifying us of any errors.
  • It removes some of the load and instantiation time for XAML elements.
  • It helps to reduce the file size of the final assembly by no longer including .xaml files.

Sometimes we also get errors that crash completely our debug session and we can’t seem to see the issue that’s causing it, in these cases we could use our last resort of adding a try-catch in our InitializeComponent(); code in the code behind of our page and catch globally any exception that may be launched in our page. (this is not recommended for production, just add this temporarily if you need to debug a certain case like this).

Are you using DI?

If you are using Dependency Injection in your project, your app may also crash because there might be a problem with one of the dependencies the ViewModel needs to initialize correctly, to track down which one could it be we can do the following:

  • Comment out all dependencies in the ViewModel
  • Add our dependencies back slowly to constructor 1 by 1
  • Verify that all our dependencies are well register

If all dependencies are ok, and our XAML and code behind are good as well, next we must start debugging the properties in our ViewModel. We can simply comment out all properties and start uncommenting them slowly and verify which one is causing the issue.

If you find a property giving the problem verify how you are initializing it, if you are assigning it right away, try initializing it on the constructor and see if it works, if not verify how we are using it on the XAML, or formatting it for output to the page.

Tools that can AID us in the debugging process

It’s always great to have tools that can aid you in the debugging process of our apps and here are some I recommend if you are not using them already!

Xamarin.Forms.DebugRainbows

If you have ever created a Layout and didn’t get the expected result when you compiled the app, it can sometimes be really tedious to find which UI component is not set up correctly, for this our XamBuddy Steven Thewissen, has created this amazing package which, adds a very colorful debug mode to each of your ContentPage’s that lets you immediately see where all of your elements are located!

HttpTracer

We oftentimes find our selves trying to figure out how to set up a proper way to see the requests and response information that our software exchange with backends. And that’s why our XamBuddies Daniel Causer, Dylan Berry, and Chase Florell have created this great library which is an Http tracing library to write request and response information to your output window. Making your life easier when debugging Http calls!

Made with ❤ by Pujolsluis

Spanish Post:

https://medium.com/@Pujolsluis/tropiezos-al-realizar-debugging-en-xamarin-y-como-superarlos-aa94331b5db5

Credits for artwork components:
Mobile Icons for the banner of the post made by Freepik

(Visited 302 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.