Categories
mobile

Simple Debugging in Xamarin Forms

Text labels in your XAML can be given a name. And that name can be referenced in the code behind. First create a label in your XAML. Stick it in the content page and not in ToolbarItems as there isn’t enough space there really. I usually give that label a silly name that’s completely disassociated with the topic of the app I’m writing, so that it stands out when I want to find it and comment it out.

<Label x:Name="banana" Text=“”/>

Now you can reference this label in the code behind. Say for example you have a string variable called CloudType. What I find useful is to state the name of the variable, and then the values held in the variable.

banana.Text = "CloudType: " + CloudType;

Or if you’ve you’ve got a variable that’s not a string, say CloudHeight is an int, then you can tag a ToString on the end

banana.Text = "CloudHeight: " + CloudHeight.ToString();

Probably not the most elegant or best practice way to debug your code. But it’s a quick and easy way of trapping bugs