How to Debug Xamarin Application on Visual Studio

Team4Solution
5 min readJun 22, 2020

--

Xamarin is an amazing technology for cross-platform mobile application development. Now, iOS developers can build and debug iOS applications in Windows machines, using Visual Studio. The new Xamarin Live Player allows creating, running, testing, and debugging iOS apps directly from a Windows PC that runs Visual Studio. Earlier, you needed a Mac to build and test your apps.

To get started with this new feature, you just need to install the Xamarin Live Player app on the iOS device and then pair it to your PC by scanning a QR code on your screen. Since it is fully baked into Virtual Studio; you also get access to all of the standard functionality you would prefer in your IDE. You can do a lot more including make live edits to your code while it is running and immediately see the result of a color change, etc

Using the Xamarin Live Player, you can preview XAML naturally and easily. MVC Dot Net Visual Studio will automatically give you the glances of the active XAML document as you move from document to document. Also, it includes other features like breakpoints when debugging, and integration with the Console and Error windows. And also sometimes you may start a project with certain platforms in mind, so it also offers the option to select a particular platform of your targeted app. Below are the steps to debug the Xamarin application on Visual Studio :

Setting Breakpoints

When you are getting started with debugging your application you first need to set breakpoints for your application. To do so, simply click in the margin area of the editor, next to the line number of code you want to break at:

https://docs.microsoft.com/en-us/xamarin/ios/deploy-test/debugging-in-xamarin-ios-images/debugging1.png

By clicking the Breakpoints pad, you can view all the breakpoints that have been set in your code

Image source: https://docs.microsoft.com/en-us/xamarin/ios/deploy-test/debugging-in-xamarin-ios-images/image0a.png

If the Breakpoints pad is not displaying automatically, you need to make it visible, to do so, and click View > Debug Windows > Breakpoints

Always remember to set the configuration to Debug, before you start debugging any application. This will always good to use handy tools to support debugging including breakpoints, using data visualizers, and viewing the call stack.

Start Debugging

Firstly, you need to choose the target device or similar in your IDE to start debugging:

Image Source: https://docs.microsoft.com/en-us/xamarin/ios/deploy-test/debugging-in-xamarin-ios-images/debugging7b.png

Once you are done choosing the device, deploy your application by pressing the Play button. When you click a breakpoint, you can see the highlighted yellow color code.

You can use debugging tools, like inspecting the values of objects, at this point to know more about what is happening in your code:

Conditional Breakpoints

You can add some conditional breakpoints by setting rules dictating the situations under which a breakpoint should occur. You can set conditional breakpoints to new breakpoints as well as your existing breakpoints.

Steps to set a conditional breakpoint :

1. Access the Breakpoint Properties window:

• To add a new conditional breakpoint, you can access the breakpoint properties window by right-clicking on the editor margin, to the left of the line number for the code you would like to set a breakpoint on and choose New Breakpoint:

• Right-click the breakpoint and choose Breakpoint Properties to set a condition to an existing breakpoint.

2. You can then enter the condition under which you want to a breakpoint to occur:

Image Source: https://docs.microsoft.com/en-us/xamarin/ios/deploy-test/debugging-in-xamarin-ios-images/image6.png

Navigating through code

When your break point has been reached, the Debug tools allow you to get control over the program’s execution. The IDE will show four buttons, enabling you to run and step through the code.

Breakpoints

You need to keep it in mind that iOS provides the applications only a few seconds (10) to startup and complete the Finished Launching process in the Application delegate, otherwise, it kills the process.

So, setting breakpoints to startup code is quite not possible, you can use some other form of callback method that can be executed after Finished Launching has terminated.

Device Diagnostics

Certainly, you can access detailed diagnostics if any error occurs when setting up the debugger, simply by adding “-v -v -v” to the additional touch arguments in your Project Options. You can get complete detailed error information to the device console.

Wireless Debugging

By default, the Xamarin.iOS is set to debug your application on your devices over the USB connection. However, it also allows debugging over the wireless network. Sometimes the USB device might go through some maintenance including testing plugging/unplugging of the cable for developing External Accessory-powered applications or more. In those cases, you can go for debugging via the wireless network.

For more information on Wireless Debugging, You can visit the Wireless Deployment guide.

Technical Details

Xamarin.iOS uses the new Mono soft debugger which runs by having the Mono runtime expose the debugging functionality through a wire protocol. On startup, an application that is to be debugged contacts the debugger and the debugger starts to operate. In Xamarin.iOS for Visual Studio, the Xamarin Mac Agent plays the role of the mediator between the application (in Visual Studio) and the debugger. This soft debugger needs a cooperative debugging scheme when running on the device.

Accessing the Console

All your Crash logs and the output of the Console class will be saved to the iPhone console. You can always access this console with Xcode. To do so, you simply go to the “Organizer” and choose your device from the organizer.

On the other hand, you can also go to Apple’s iPhone Configuration Utility to directly access the console if you do not wish to start up Xcode.

Debugging Mono’s Class Libraries

If you just want to see that how things are working under the hood at the debugger's end, you can do this by enabling the feature. Xamarin iOS ships with the source code for Mono’s class libraries, and you can use this as well, however, this feature is turned off by default dues to its high memory consumption.

--

--

Team4Solution

Team4solution.com stands amongst the most popular mobile app & web development companies in USA