Partilhar via


Walkthrough: depurar um aplicativo WPF

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:

  • Crie um projeto WPF.

  • 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.

ObservaçãoObservação

Caixas de diálogo e comandos de menu que você vê podem diferir das descritas na Help dependendo das suas configurações ativas ou edição. Para alterar as configurações, escolha Import and Export Settings sobre o Ferramentas menu. For more information, see Trabalhando com configurações.

Pré-requisitos

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 Como: Criar um novo projeto de aplicativo WPF.

    MainWindow. XAML é aberto no WPF Designer.

  2. No modo XAML , defina a janelado Backgroundapropriedade como "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.

    Assistente de Exceção exibindo um erro de análise XAML

    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 Como: Basta entrar meu código..

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. Um botão de seta para baixo-aparece no final da linha. Clique no botão de seta de-para baixo para ver a mensagem de erro de todo, conforme mostrado na ilustração a seguir.

    Assistente de Exceção exibindo um erro de análise XAML

    The message reads "Cannot convert the value in attribute 'Background' to object of type 'System.Windows.Media.Brush'. Token is not valid. Erro no objeto 'DebuggingExample.MainWindow' no arquivo de marcação ' DebuggingExample;componente/MainWindow.xaml' Posição de linha 3 5."

  5. Essa exceção é que descreve um erro de análise de XAML encontrados no arquivo MainWindow. XAML, na linha cinco.

    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 Como: Criar um projeto de biblioteca UserControl WPF.

    UserControl1.xaml opens in the WPF Designer.

  3. No modo XAML , defina a janelado Backgroundapropriedade como "Ameixa".

    <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. Abrir MainWindow. XAML na WPF Designer.

  7. Map the DemoControlLibrary namespace by inserting the following XAML into the <Window> opening tag. For more information, see Como: Importar um namespace em 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. No modo XAML , alterar "Ameixa" para "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 Como: Basta entrar meu código..

Próximas etapas

  • You can also use the Error List window to view XAML parse errors at design time. For more information, see XAML Erros e Ajuda.

Consulte também

Conceitos

XAML Erros e Ajuda

Tratamento de Exceção (Depuração)

Passo a passo: Criando um Aplicativo WPF Simples com o designer WPF

Outros recursos

Erro de mensagem referência para o WPF Designer

Depuração no Visual Studio

Trabalhar com controles no criador de WPF