Section 3.6. codebehind


3.6. codebehind

The concept of codebehind has been mentioned but not fully explored yet. You've already noted that event handlers can be assigned to elements and implemented in code and that the attribute name must exactly match the handler name in code. The event handlers specified by name as attributes for controls are associated with the codebehind in a C# or VB.NET file during the compilation process. The compiler generates a partial class for XAML and then assembles it with the code, which defines the rest of the class in a codebehind file. This allows the two pieces to be tied together when the code is interpreted within the runtime engine.

But there are other things that can be accomplished in code besides handling events. Many applications require initialization of data sources, or automatically adding fields to the user interface depending on the user's role. These things cannot be done in XAML; they must be done programmatically.

Every XAML application represents the declaration of a partial CLR class. Part of the class is declared using XAML, and the rest of it can be declared in a codebehind file using C# or Visual Basic. The implementation can then programmatically modify the user interface or interact with other systems such as a database or remote application to accomplish the application's designated task.

As with event handlers, the name of the class assigned as the implementation class for a XAML application must exactly match, including the namespace. For example, the XAML class declaration in Example 3-18 referencing the StartPage class with a namespace of MyNameSpace exactly matches the name of the class in Example 3-19. Note that the Page element in the XAML file has no other elements. The TextBlock and Button seen in Figure 3-7 are the result of programmatically adding the two elements to the Page in the C# codebehind implementation.

Example 3-18. XAML declaration of StartPage.xaml

     <Page xmlns="http://schemas.microsoft.com/winfx/avalon/2005"           xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"           x:           Loaded="Init" /> 

Example 3-19. C# implementation of StartPage class within StartPage.xaml.cs

     using System;     using System.Windows;     using System.Windows.Controls;     using System.Windows.Navigation;           namespace MyNameSpace     {        public partial class StartPage : Page        {            TextBlock txtElement;            StackPanel rootPanel;            Button aButton;            void Init(object sender, EventArgs args)            {                      rootPanel = new StackPanel(  );                      txtElement = new TextBlock(  );                      aButton = new Button(  );                      txtElement.TextContent = "Some Text";                      aButton.Content = "Press me";                      Child = rootPanel;                      rootPanel.Children.Add(txtElement);                      rootPanel.Children.Add(aButton);            }        }     } 

In Example 3-19, you can see that a StackPanel is declared as rootPanel, indicating that it will be the first (and only) child of Page. Page is only allowed a single child of type UIElement, so all other elements to be displayed on the page will have to be added to the StackPanel. The C# code in this example is equivalent to the XAML code in Example 3-20.

Figure 3-7. Programmatic creation of a XAML application


Example 3-20. XAML declaration to produce Figure 3-7

     <Page xmlns="http://schemas.microsoft.com/winfx/avalon/2005"           xmlns:x=http://schemas.microsoft.com/winfx/xaml/2005 >         <StackPanel>             <TextBlock>Some Text</TextBlock>             <Button Content="Press me" />         </StackPanel>     </Page> 

If it can be done in XAML, it can be done programmatically. Every XAML element is accessible from C# or Visual Basic and can be manipulated within event handlers or from within the class's implementation. This provides you with the means to add or remove elements from the user interface, allows for localization, and offers the ability to dynamically build a user interface based on data-driven principles.

While XAML was designed to separate the presentation layer from the application logic, its representative CLR classes are available to the programmer and can be used to build an application in the same way traditional Windows Forms or .NET applications are built.




XAML in a Nutshell
XAML in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596526733
EAN: 2147483647
Year: 2007
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net