Freigeben über


Code-Behind and XAML

Code-behind is a term used to describe the code that is joined with the code that is created by a XAML loader when a XAML page is compiled into an application. This topic describes requirements for code-behind as well as an alternative inline code mechanism for code in XAML.

This topic contains the following sections.

  • Prerequisites
  • Code-behind, Event Handler, and Partial Class Requirements
  • x:Code
  • Inline Code Limitations
  • Related Topics

Prerequisites

This topic assumes that you have read the XAML Overview and have some basic knowledge of the CLR and object-oriented programming.

Code-behind, Event Handler, and Partial Class Requirements

  • The partial class must derive from the type of the class that was used as the root element. You can leave the derivation blank in the partial class definition in code-behind, but the compiled result will assume the page root to be the base class for the partial class even if it not specified (because the markup half of the partial class did specify the page root as base).

  • The event handlers you write must be instance methods defined by the partial class within the namespace identified by x:Class. You cannot qualify the name of an event handler to instruct a XAML loader to look for that handler in a different class scope. You also cannot use static methods as an event handler.

  • The handler must match the delegate for the appropriate event.

  • For the Microsoft Visual Basic .NET language specifically, you can use the language-specific Handles keyword to associate handlers with instances and events in the handler declaration, instead of attaching handlers with attributes in XAML. However, this technique does have some limitations because Handles cannot support all of the specific features of the WPF event system,such as certain routed event scenarios or attached events. For details, see Visual Basic and WPF Event Handling.

x:Code

x:Code is a directive element defined in XAML that can contain inline programming code. The code that is defined inline can interact with the XAML on the same page. The following example illustrates inline C# code. Notice that the code is inside the x:Code element and that the code must be surrounded by <CDATA[...]]> to escape it for XML, so that a XAML reader (interpreting either the XAML schema or the WPF schema) will not try to interpret the code literally.

<Page
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="MyNamespace.MyCanvasCodeInline"
>
  <Button Name="button1" Click="Clicked">Click Me!</Button>
  <x:Code><![CDATA[
    void Clicked(object sender, RoutedEventArgs e)
    {
        button1.Content = "Hello World";
    }
  ]]></x:Code>
</Page>

Inline Code Limitations

You should consider avoiding or limiting the use of inline code for a XAML based application. In terms of architecture and coding philosophy, keeping markup and code-behind separated also keeps the designer and developer roles much more distinct. On a more technical level, the code that you put inline can be awkward to write, because you are always writing into the XAML page's generated partial class, and can only use the default namespace mappings. Because you cannot add using statements, you must fully qualify many of the API calls that you make. The default WPF mappings include most but not all CLR namespaces that are present in the WPF assemblies; you will have to fully qualify calls to APIs contained within the other namespaces. You also cannot define multiple classes in the inline code, and all code entities must exist as a member or variable within the generated partial class. Other language specific programming features, such as #ifdef against global variables or build variables, are also not available. For more information, see x:Code XAML Directive Element.

See Also

Reference

x:Code XAML Directive Element

Concepts

XAML Overview
Building a Windows Presentation Foundation Application
XAML Syntax Terminology