Debugging in the Emulator
If clicking the user interface was not working the way we would like,
it might help if we could stop the operation during an event to see
what was happening during execution. We can do this by debugging our
operation. We can use the debugger to set breakpoints and break in code
while using the emulator. Place the text cursor inside the event handler
and press F9 to create a breakpoint. When you run the application
(again, press F5) you can see that when you click on the theStatus TextBlock
the debugger stops inside the event handler. You can hover your mouse over specific code elements (e.g., theStatus.Text
) to see the value in a pop up (as shown in Figure 3).
Figure 3. Using the Visual Studio debugger
Pressing the F5 key while stopped at a breakpoint will cause the
application to continue running. There are other ways to walk through
the code, but for now that should be sufficient to get you started.
Using the emulator is the most common way you will develop your
applications, but there are some interactions that are difficult to do
with the emulator (e.g., multitouch, using phone sensors, etc.) for
which debugging directly on a device would be very useful. Luckily,
debugging on the device is supported and works pretty easily.
Debugging with a Device
If you have a phone with which you want to do your development, you
will want to be able to deploy and debug directly on the phone itself.
First you need to connect your phone to your development machine. All
you need to do is connect your phone to your computer by a USB cable.
Now that your device is connected, you can use it to browse to the
directories for music, photos (etc.). However, before you can use a
phone as a development device, you will need to register the phone for
development. This lifts the requirements that applications be signed by
Microsoft, and allows you to deploy your applications directly to the
phone so that you can debug applications.
Before you can enable your phone as a developer phone, you will need to have an account at the Windows Phone developer portal (http://developer.windowsphone.com).
Once you have done that, you can enable your phone to be used for
development. To do this you will need the Windows Phone Developer
Registration tool, which is installed when you install the Windows Phone
SDK. When you run this application it will detect the device. You will
need to make sure the device is unlocked and turned on. At that point
the Windows Phone Developer Registration Tool will enable the “Register”
button as shown In Figure 4.
Figure 4. Windows Phone Developer Registration Tool
Next it will ask you for your Windows Live ID that you used to register with the developer portal, as shown in Figure 5.
Figure 5. Signing into with your Microsoft ID
If your phone is successfully attached to your computer, the Status
area will tell you that it is ready to register your device for
development. At this point, just click the Register button to register
with the developer portal. Once it registers the phone, it changes the
status to show you that the phone is ready.
When you use a device to debug, you will find it much easier to
change the default time-out of the device to be longer than the default
(usually 1 minute, but it depends on the device manufacturer and
carrier). To do this go to the settings on your phone. In the settings,
there is an option called “lock screen” as shown In Figure 6.
Figure 6. “lock screen” option on the settings screen
Once in this option, you can scroll down to find the “Screen times
out after” option and open the option and pick the longest timeout you
can tolerate (this will affect battery life when you’re not debugging on
the device, so be careful to pick a time out you can live with if it’s
not a testing-only device). You can see this in Figure 7.
Figure 7. Changing the default device timeout
Now that you’ve registered your device, you can deploy and debug your
applications using Visual Studio. The key to using the device instead
of the emulator is to change the deployment using the drop-down list of
deployment options. The drop down is located in the toolbar of Visual
Studio, as shown in Figure 8.
Figure 8. Changing the deployment to use a development phone
Once you change the deployment target you can
debug just like you did with the emulator. When you run the
application, it will deploy your application to the device and run it so
that you can debug it in the same way as you did with the emulator.