Freigeben über


Debug Blend applications in Visual Studio

Blend for Visual Studio 2012 is a design application for creating rich Windows Presentation Foundation (WPF) applications for Microsoft Windows, and Microsoft Silverlight applications for the web. Microsoft Visual Studio 2012, which is also used to build Microsoft Windows applications, can open, build, and debug Blend projects. If you are having trouble debugging your application using the Run Project (F5) feature of Blend, you can use Visual Studio 2010 to obtain detailed error messages about runtime errors. Sometimes, you can fix runtime errors by trying out different changes in your XAML or code until you understand what is going on behind the scenes. However, it is faster to actually watch what is going on behind the scenes by stepping through your code line by line as the application is running.

To step through your code line by line

  1. Open your Blend project in Visual Studio 2012.

  2. Click on a line of code that you are interested in and set a breakpoint by pressing F9. If you want to step through your code starting at the beginning, set the breakpoint at the line this.InitializeComponent(); in the Window1.xaml.cs file.

  3. Press F5 to start your application.

    Visual Studio 2012 will build and run your application until the line with the breakpoint is called. At that point, application execution stops and Visual Studio 2012 displays the file that contains the breakpoint, and displays a yellow arrow at the line that is about to be executed.

  4. On the Debug menu, there are three options for stepping through code:

    • Step Into (F11) will execute the next line of code, and if that line is a function call, it will take you to the first line in that function call.

    • Step Over (F10) will also execute the next line of code, but if that line is a function call, it will execute that function and take you to the next line after the function call.

    • Step Out (Shift+F11) will bring you out of a function call.

    The most common option you will use is Step Over (F10). While you step through the code, you can see what code paths are being executed and whether code is not being executed in the order that you expected.

  5. In the Locals panel, you can see what variables are currently instantiated, and what values they contain. As you step through code, the values of variables in the Locals panel are updated. This can help you debug your application if you suspect that a loop is not being called as many times as you expect it to be called, or if you suspect a variable does not contain a value that you expect, or if a variable disappears (goes out of scope) before it is needed.

Debug a WPF Browser Application

To debug a WPF Browser Application while it runs in a browser, you have to use the Attach to Process feature of Visual Studio 2010 Standard Edition or higher. Use the following procedure.

To debug a WPF Browser Application

  1. Build and run your project at least once to create debug files.

  2. From the command line, type the following line and press ENTER.

    %SystemRoot%\system32\PresentationHost.exe -debug
    

    This starts the WPF process in debug mode.

  3. In Visual Studio 2012, open your WPF Browser Application project.

  4. In the Project menu, click Properties. On the Debug tab, select Enable unmanaged code debugging. Close the properties window.

  5. In the Debug menu, click the Attach to Process.

  6. In the Attach to Process window, next to Attach to, click Select. Under Debug these code types, select Managed and Native, and click OK. Under AvailableProcesses, select PresentationHost.exe. Click Attach to start debugging.

  7. In File Explorer, double-click the .xbap file for you application in the bin\Debug folder of your project.

  8. Your application will launch in your default browser. If you have set any breakpoints, then application execution will stop and Visual Studio 2012 will display the file that contains the breakpoint, and display a yellow arrow at the line that is about to be executed. From here, you can step through code as normal.

For more information about debugging, see Debugging in Visual Studio on MSDN.

JJ171018.collapse_all(de-de,VS.110).gifTo debug runtime XAML errors in Visual Studio

If you have a XAML error that is occurring at runtime, you can get information about which line of XAML is causing the error by adding the following line to your project file (.csproj or .vbproj) in the <PropertyGroup> section.

<XamlDebuggingInformation>true</XamlDebuggingInformation>

This project file property is only set to true by default in the debug version of your build because it increases the size of your application.

JJ171018.collapse_all(de-de,VS.110).gifGeneral debugging

It can be difficult to understand what is causing a bug or where it exists in your application, but it helps to understand the types of bugs that you might encounter. For more information, see Debug applications.