Debugging a Windows Store App
I’m always optimistic and believe that any code that I write will run without error the first time that I run it. To date, that has never happened. I spend a significant amount of my time debugging code which does not do what I want.
In this section, I discuss the tools in Visual Studio which you can use to debug your code. I discuss how you can use the JavaScript Console window, use breakpoints, and use the DOM Explorer.
Using the Visual Studio JavaScript Console Window
When I write JavaScript code for pages used in websites, I use the JavaScript console window to view JavaScript errors. I also write custom messages to the console window using console.log() so I can debug my code (see Figure 1.29).
Figure 1.29. Debugging with the Google Chrome JavaScript console
When running a Windows Store app, you don’t have access to the browser JavaScript Console. Instead of using the browser JavaScript Console, you need to use the Visual Studio JavaScript Console (see Figure 1.30).
Figure 1.30. The Visual Studio JavaScript Console window
You can view JavaScript errors and write debug messages to the Visual Studio JavaScript Console window by using console.log() in exactly the same way as you would write to a browser console window.
If you hit an error and you want to display the value of a JavaScript variable, then you can enter the variable name in the bottom of the JavaScript Console (see Figure 1.31).
Figure 1.31. Dumping a JavaScript variable to the JavaScript Console window
Setting Breakpoints
If you are building a Windows Store app, and the Windows Store app is behaving in ways that you don’t understand, then it is useful to set breakpoints and step through your code.
You set a breakpoint by clicking in the left gutter of the Visual Studio code editor next to the line that you want to break on (see Figure 1.32). When you run your app in debug mode, and the breakpoint is hit, then you can examine the values of your variables by hovering over them with a mouse.
Figure 1.32. Setting a breakpoint
You can step through your code, line by line, by using the Step Into toolbar button or by pressing F11.
Using the DOM Explorer
Another of my favorite browser developer tools is the HTML inspector (this is a feature, for example, of Firebug). You can use this tool to view the live HTML and CSS in a document.
Visual Studio supports a similar tool named the DOM Explorer. You can use the DOM Explorer to inspect the property of any HTML element in a running Windows Store app.
After running a Windows Store app in Visual Studio, you can view the DOM Explorer window by selecting the menu option Debug, Windows, DOM Explorer. Within the DOM Explorer window, you can click any element and view all of the properties of the element including information about all of the styles associated with the element (see Figure 1.33).
Figure 1.33. Using the DOM Explorer Window
If you click an element associated with a WinJS control, then you can see all of the HTML attributes and elements rendered by the control. Adding a ListView control to a page, for example, adds a lot of new DIV elements to the page.