Compartilhar via


Debug Expression Blend applications

If your Microsoft Expression Blend application does not behave the way that you expect, or if errors occur when you try to test 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.

Syntax errors

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

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

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

Cc294906.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

Compilation errors

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

Compilation errors occur when the compilation system of Expression Blend or Visual Studio 2008 cannot find something that your project requires. For example, if your 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\v3.5 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 (or in the case of a Microsoft Silverlight 1.0 project, in a .js code file), 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 3.5 installed, and you are building your application by using Expression Blend or Visual Studio 2008, 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 Cc294906.6c67bb3b-e8a2-4a63-bad5-54d5c15b04dd(en-us,Expression.10).png 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.

Cc294906.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

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 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 2008 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 The Layout System 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 a WPF project open 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 and user interactivity in this User Guide. In a Silverlight 1.0, this can happen if you hook up an event handler to the root element by using the following code:

    rootElement.addEventListener(…);
    

    Instead of hooking up the event handler to a child element in the document, such as by using the following code:

    this.control.content.findName("WebLink").addEventListener(…);
    

    For more information, see Create a button that controls a storyboard in a Silverlight application.

  • 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 Animation in this User Guide. For an example of animation triggers, see Create 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 in a WPF project, you cannot 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 2008. For more information, see Debug Expression Blend applications in Visual Studio 2008 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 a Window1() constructor method in a WPF project, 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 WPF events quick reference in this User Guide.

Cc294906.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

-

Debugging in Visual Studio 2008

Expression Blend is a design tool for creating rich user interfaces for WPF-based applications and Silverlight 1.0 applications. You can use Visual Studio 2008 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 2008 to obtain detailed error messages about run-time errors. For more information, see Debug Expression Blend applications in Visual Studio 2008 in this User Guide.

Cc294906.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

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.

Cc294906.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

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.

Cc294906.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

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.

Cc294906.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

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 Expression Blend applications in this User Guide. For more information about WPF application security, see Security in the WPF section on MSDN.

Cc294906.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top

Getting help

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.

Cc294906.7e183f1f-37d8-4dcb-980c-19a5d61ca087(en-us,Expression.10).gifBack to top