Walkthrough: Debugging a WPF Application

This walkthrough shows how to debug a simple Windows Presentation Foundation (WPF) application with the WPF Designer. You will use the Exception Assistant dialog box to interpret an Extensible Application Markup Language (XAML) parse exception.

In this walkthrough, you perform the following tasks:

  • Create a WPF project.

  • Interpret an exception.

  • Interpret an exception from a child control.

When you are finished, you will know how to interpret exceptions which occur during XAML parsing.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Working with Settings.

Prerequisites

You need the following components to complete this walkthrough:

  • Visual Studio 2010.

Creating the Project

The first step is to create the project for the application.

To create the project

  1. Create a new WPF Application project in Visual Basic or Visual C# named DebuggingExample. For more information, see How to: Create a New WPF Application Project.

    MainWindow.xaml opens in the WPF Designer.

  2. In XAML view, set the window's Background property to "Azure".

    <Window x:Class="DebuggingExample.MainWindow"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Background="Azure"
        Title="MainWindow" Height="300" Width="300">
        <Grid>
    
        </Grid>
    </Window> 
    

    The background color for the window updates in Design view.

  3. Change "Azure" to "Azurer" and click in the Design view.

    The Design view displays the message "The document contains errors that must be fixed before the designer can be updated. Click here to open the Error List."

    Also, the Error List window displays the "Token is not valid" error message.

  4. From the Debug menu, select Start Debugging.

    The application starts, the XAML view opens, and the Exception Assistant dialog box appears, as shown in the following illustration.

    Exception Assistant displaying a XAML parse error

    Note   If the XAML view does not open when the Exception Assistant appears, turn off the Just My Code debugging option. For more information, see How to: Step Into Just My Code.

Interpreting the Exception

The exception is a XAML parse error. The Exception Assistant dialog box shows you information about the error, which you can use to interpret its cause.

To interpret the exception

  1. In the Exception Assistant dialog box, click the View Detail… link.

    The View Detail dialog box opens.

  2. In the Exception snapshot section, expand the System.Windows.Markup.XamlParseException item.

    The LineNumber item displays 3 and the LinePosition item displays 5.

  3. Click the Message item.

  4. A down-arrow button appears at the end of the line. Click the down-arrow button to see the entire error message, as shown in the following illustration.

    Exception Assistant displaying a XAML parse error

    The message reads "Cannot convert the value in attribute 'Background' to object of type 'System.Windows.Media.Brush'. Token is not valid. Error at object 'DebuggingExample.MainWindow' in markup file 'DebuggingExample;component/MainWindow.xaml' Line 3 Position 5."

  5. This exception is describing a XAML parse error encountered in the MainWindow.xaml file, on line five.

    The exception is raised because the TypeConverter for Brush could not convert the "Azurer" string to a Brush object.

  6. Click OK to close the View Detail dialog box.

  7. Click OK to close the Exception Assistant dialog box.

  8. From the Debug menu, select Stop Debugging.

Creating the UserControl Project

You can use the Exception Assistant dialog box to interpret errors which are raised from a control in a separate assembly.

To create the project

  1. In XAML view, change the value of the Background property back to "Azure".

  2. Add a new WPF User Control Library project named DemoControlLibrary to the solution. For more information, see How to: Create a WPF UserControl Library Project.

    UserControl1.xaml opens in the WPF Designer.

  3. In XAML view, set the window's Background property to "Plum".

    <UserControl x:Class="DemoControlLibrary1.UserControl1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Background="Plum"
        Height="300" Width="300">
        <Grid>
    
        </Grid>
    </UserControl>
    
  4. Build the solution.

  5. In Solution Explorer, in the DebuggingExample project, add a reference to the DemoControlLibrary project.

  6. Open MainWindow.xaml in the WPF Designer.

  7. Map the DemoControlLibrary namespace by inserting the following XAML into the <Window> opening tag. For more information, see How to: Import a Namespace into XAML.

    xmlns:d="clr-namespace:DemoControlLibrary;assembly=DemoControlLibrary"
    
  8. Insert the following XAML after the <Grid> opening tag.

    <d:UserControl1 />
    

    Design view updates with the new background color.

  9. Open UserControl1.xaml in the WPF Designer.

  10. In XAML view, change "Plum" to "Plumr".

  11. From the Debug menu, select Start Debugging.

    The application starts, the XAML view opens to UserControl1.xaml, and the Exception Assistant dialog box appears.

    Note   If the XAML view does not open when the Exception Assistant appears, turn off the Just My Code debugging option. For more information, see How to: Step Into Just My Code.

Next Steps

  • You can also use the Error List window to view XAML parse errors at design time. For more information, see XAML Errors and Help.

See Also

Concepts

XAML Errors and Help

Exception Handling (Debugging)

Walkthrough: Building a Simple WPF Application with the WPF Designer

Other Resources

Error Message Reference for the WPF Designer

Debugging in Visual Studio

Working with Controls in the WPF Designer