Debugging Expression Blend applications
- Syntax errors
- Compilation errors
- Run-time errors
- Debugging in Visual Studio 2005
- Debugging performance issues
- Event tracing
- Debugging hybrid applications
- Getting help
If your Microsoft® Expression Blend application does not behave the way that you expect, or if errors occur when you try to build your application, there is 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.
When you build your application, any syntax errors are displayed in the Errors tab of the Results panel of Expression Blend, or in the Error List panel in Microsoft® Visual Studio® 2005.
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 Expression Blend, or in Visual Studio 2005 with the extensions for .NET Framework 3.0 installed. The extensions can be downloaded from MSDN.
- 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 a string,
- 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
For information about XAML syntax, see the overview topics for individual controls listed in the Control Library topic in the Windows Presentation Foundation section on MSDN. For information about programming syntax, you can search on MSDN for keywords in your code.
When you build your application, any compilation errors are displayed in the Errors tab of the Results panel of Expression Blend, or in the Error List panel in Visual Studio 2005.
Compilation errors occur when the compilation system of Expression Blend or Visual Studio 2005 cannot find something that your project requires. For example, if your project is missing a reference to the WPF assemblies, you might get an error such as "The name 'Window' does not exist in namespace 'http://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\v3.0 folder:
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
FullyQualifiedNamespaceis the namespace in the code file:
- The compiler options are not set properly, or your system is not capable of building Microsoft® .NET Frameworkbased applications. If you have the Microsoft .NET Framework 3.0 installed, and you are building your application by using Expression Blend or Visual Studio 2005, this should not be an issue.
- A file has not been saved before trying to build the project. For example, if you use the Events panel of Expression 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 Expression 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.
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 Expression 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 Debugging in Visual Studio 2005 later in this topic.
Some common causes of run-time errors are as follows:
- XAML elements are not laid out properly, or the wrong panel element is being used to contain other elements. To learn about layout, see Layout overview in this User Guide, 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 Expression Blend, select the element under Objects and Timeline in the Interaction panel, and then in the Properties panel, click the Events button. For more information, see Event handling in this User Guide.
- An animation trigger in Expression 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 Essentials of Animation in this User Guide. For an example of animation triggers, see Create a simple animation, or see the topics under the Try it! section in Simple styles.
- In a code file, a variable references an object that does not yet exist. For
example, in a code-behind file, you can not reference a XAML object before the line
this.InitializeComponent();, otherwise you will get a XamlParseException error.
- In a code file, assumptions are made that make your application progress down an unexpected code path. This situation benefits most from stepping through your code while debugging your application in Visual Studio 2005. For more information, see Debugging Expression Blend applications in Visual Studio in this User Guide.
- 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 only see 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 your 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 anything you need to do with UI elements that exist in your XAML document. For more information, see Object Lifetime Events in the WPF section on MSDN . For a list of events and when they occur, see the Events quick reference in this User Guide.
Debugging in Visual Studio 2005
Expression Blend is a design tool for creating rich user interfaces and Windows®-based applications that make use of WPF features. You can use Visual Studio 2005, which is also used to build Windows-based applications, to open, build, and debug Expression Blend projects. If you are having trouble debugging your application by using the Test (F5) feature of Expression Blend, you can use Visual Studio 2005 to obtain detailed error messages about run-time errors. For more information, see Debugging Expression Blend applications in Visual Studio in this User Guide.
|To create new XAML applications and XAML Browser applications (XBAPs) in Visual Studio 2005, you need to install the extensions for .NET Framework 3.0. These can be downloaded from MSDN. (You do not need the extensions to debug XAML applications that were created with Expression Blend.)|
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 .
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.
While being debugged, your application has the same security permissions that it has when another person uses it. For more information, see Deploying and publishing Expression Blend applications in this User Guide. For more information about WPF application security, see Security in the WPF section on MSDN.
If you need more help debugging your Expression Blend application, you can search on Windows Presentation Foundation Forum for posts related your issue or post a question.