Share via


Debug applications

If your Blend for Visual Studio 2012 application does not behave the way that you expect, or if errors occur when you try to test your application, there may be a bug in your application. It can be difficult to understand what is causing a bug or where in your application it exists, but it helps to understand the types of bugs that you might encounter.

Syntax errors

When you build your application, any syntax errors are displayed in the Errors tab of the Results panel of Blend, or in the Error List panel in Microsoft Visual Studio 2012.

Syntax errors occur if your Extensible Application Markup Language (XAML) or code does not follow the formatting rules of the language. The description of the error can help you understand how to fix it. The description also specifies the name of the file and the line number where the error occurs. Some common causes of syntax errors are as follows:

  • A keyword has been misspelled or the capitalization is wrong.

  • Quotation marks are missing around strings of text.

  • A XAML element is missing a closing tag.

  • A XAML element exists in a location where it is not allowed. You can avoid these errors by editing your documents in Design view in Blend, or in Visual Studio 2012.

  • In a code file, a function or method call does not include the required parameters. For example, the MessageBox.Show() method must have at least one parameter, such as the string in, MessageBox.Show("Hello").

  • In a code file, a variable of one type is being assigned to a different type. For example, the MessageBox.Show() method can have a string argument, but it cannot have an integer argument.

  • In C#, a method that does not need arguments might not have braces at the end. For example, this.InitializeComponent; will cause a syntax error because the correct line is this.InitializeComponent();.

For information about XAML syntax, see the overview topics for individual controls listed in the Windows Presentation Foundation Control Library and the Silverlight Control Gallery on MSDN. For information about programming syntax, you can search on MSDN for keywords in your code.

Compilation errors

When you build your application, any compilation errors are displayed in the Errors tab of the Results panel of Blend, or in the Error List panel in Visual Studio 2012.

Compilation errors occur when the compilation system of Blend or Visual Studio 2012 cannot find something that your project requires. For example, if your Windows Presentation Foundation (WPF) project is missing a reference to the WPF assemblies, you might get an error such as "The name 'Window' does not exist in namespace 'https://schemas.microsoft.com/winfx/2006/xaml/presentation'". If you get this error, you can click Add Reference on the Project menu to add references to the following WPF assemblies in the "C:\Program Files\Reference Assemblies\Microsoft\Framework\" folder:

  • PresentationCore.dll

  • PresentationFramework.dll

  • WindowsBase.dll

If you still receive errors such as "The name '<member>' does not exist in the current context," there might be another assembly reference missing, or you might need to add a using (C#) or Imports (Visual Basic .NET) statement to your code for the missing namespace. To find out which assembly or namespace is required, see the MSDN reference topic for the member that is causing the error.

Some other common causes for compilation errors are as follows:

  • A keyword has been misspelled or the capitalization is wrong.

  • A class is not referenced properly in your application. For example, if your application uses a custom class which is implemented in a separate .cs or .vb code file with its own namespace, any document in your application that uses the custom class needs to include a line like the following, where FullyQualifiedNamespace is the namespace in the code file:

    xmlns:SampleNamespace="clr-namespace:FullyQualifiedNamespace"
    
  • The compiler options are not set properly, or your system is not capable of building Microsoft .NET Framework–based applications. If you have the Microsoft .NET Framework, and you are building your application by using Blend or Visual Studio 2012, this should not be an issue.

  • A file has not been saved before you try to build the project. For example, if you use the Events panel 6c67bb3b-e8a2-4a63-bad5-54d5c15b04dd of Blend to generate a new event handler method in the code-behind file (thus opening the code-behind file in Visual Studio), and then try to build the project in Blend without first saving the code-behind file, you will get an error saying that your project does not contain a definition for the event handler.

Run-time errors

You have a run-time error if your application builds but it behaves in an unexpected way when you run it (by pressing F5 in Blend). Run-time errors are the most difficult to identify because they involve errors in logic. Sometimes, you can fix run-time 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.

For more information, see Debug Blend applications in Visual Studio.

Some common causes of run-time errors are as follows:

  • XAML elements are not laid out properly, or the wrong panel object is being used to contain other objects.

    To learn about layout, see Arranging objects, or see The Layout System and Alignment, Margins, and Padding Overview in the WPF section on MSDN.

    A XAML element is not hooked up to the correct event handler. This can happen if you create many event handler methods and then assign the wrong one to the XAML element. To see which event handlers are assigned to a XAML element in a WPF project open in Blend, select the object in the Objects and Timeline panel, and then, in the Properties panel, click the Events button.

  • An animation trigger in Blend is not set properly. For example, animation storyboards must be started in any trigger if you want to be able to stop or pause them after the application is loaded. (All animation storyboards are started in the Window.Loaded trigger by default, but you can change that.)

    For more information, see Animating objects. For an example of animation triggers, see Create a simple animation.

  • In a code file, a variable references an object that does not yet exist. For example, in a code-behind file, if you reference a XAML object before the line this.InitializeComponent(), you will get a XamlParseException error.

  • In a code file, your application progresses down an unexpected code path. This situation benefits most from stepping through your code while debugging your application in Visual Studio 2010.

    For more information, see Debug Blend applications in Visual Studio.

  • In a code-behind file, user interface (UI) updates are executed on the same thread as other programming logic that should be performed on a separate thread. For example, if you create an event handler method that updates the text that is displayed in a Label, performs some other calculations, and then updates the text in the same Label again before the event handler method completes, you will see only the last update. This is because the rendering of your UI occurs at the end of your event handler method and all processing is done on the same thread, so your application cannot take time out during the execution of your method to update the UI.

    For information about how to write WPF applications that have multiple UI updates and calculations, see Threading Model in the WPF section on MSDN.

  • In an event handler method in a code-behind file, UI elements or their properties are referenced before they are available. For example, in a Window1() constructor method, you will not be able to access UI elements yet. In an OnInitialized() event handler method, you can access UI elements, but you cannot examine properties like ActualWidth because the UI elements have not been laid out yet. In an OnLoaded() event handler method, you can do what you want to do with UI elements that exist in your XAML document.

    For more information, see Object Lifetime Events in the WPF section on MSDN.

Debugging in Visual Studio 2012

Blend is a design tool for creating rich user interfaces for WPF-based applications and Microsoft Silverlight applications. You can use Visual Studio 2012 to open, build, and debug Blend projects. If you are having trouble debugging your application by using the Run Project (F5) feature of Blend, you can use Visual Studio 2012 to obtain detailed error messages about run-time errors.

For more information, see Debug Blend applications in Visual Studio.

Debugging performance issues

WPF provides a suite of performance assessment tools that allow you to analyze the run-time behavior of your application and determine how you can improve performance.

For more information, see Performance Profiling Tools for WPF and Optimizing WPF Application Performance in the WPF section on MSDN.

Event tracing

Experienced .NET programmers can add code to their WPF applications to trigger custom debugging events that help them to debug more complicated bugs. This feature is called Event Tracing for Windows (ETW). The WPF Event Trace profiling tool uses ETW for event logging.

For more information, see "Event Tracing" and "PresentationTraceSources" in Performance Profiling Tools for WPF on MSDN.

Debugging hybrid applications

If you have an application that uses both WPF and another technology like Windows Forms programming, you may experience problems such as unexpected overlapping behavior, scaling behavior, control focus issues, and so on.

For information that can help you debug hybrid applications, see Troubleshooting Hybrid Applications in the WPF section on MSDN.

Security

While being debugged, your application has the same security permissions that it has when another person uses it.

For more information, see Deploy and publish applications.

For more information about WPF application security, see Security in the WPF section on MSDN.

Getting help

If you need more help debugging your Blend application, you can search the Windows Presentation Foundation Forum or the Silverlight learning center for posts related your issue or post a question.