XML
  Home arrow XML arrow XAML in a Nutshell
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

XAML in a Nutshell
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-03-29

    Table of Contents:
  • XAML in a Nutshell
  • Basic Rules for XAML Elements
  • Elements
  • Control Elements

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    XAML in a Nutshell


    (Page 1 of 4 )

    Extensible Application Markup Language (XAML) is a .NET technology. It's a markup language that can be used to help create desktop applications, web pages, and printable documents. This article, the first of three parts, introduces you to the syntax. It is excerpted from chapter 3 of The Basics of XAML, written by Lori A. MacVittie (O'Reilly, 2006; ISBN: 0596526733). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    XAML is an XML-based markup language. Given that, it shares many properties with other XML documents, such as case sensitivity and having to be well-formed. XAML has some specific syntax peculiarities designed for easing the declaration of specific types of elements. It provides abbreviated markup syntax for specific types of elements that take advantage of the underlying Common Language Runtime (CLR) class constructors.

    This chapter will examine the core XAML syntax, as well as some of the peculiarities of its abbreviated markup syntax, in preparation for understanding more complex concepts in later chapters.

    Core XAML Syntax

    XAML generally follows XML syntax rules, just as any other XML-based markup language does. Each XAML element has a name and one or more attributes. Attributes correspond directly to object properties, and the name of the XAML element exactly matches the name of a CLR class definition.

    XAML is pure markup, which means that while the names of event handlers are specified as attributes, you must implement the actual logic of the event handler in code. If you’re familiar with ASP.NET programming techniques, then you’ll be familiar with the term codebehind , which refers to the code “behind” a XAML interface element that is responsible for providing application logic such as event handlers. It can be implemented in either C# or VB.NET. In both cases, the code can be placed inline in the XAML file, although this contradicts best practices in separating the presentation and application logic layers.

    How does this work? Every event in XAML can be assigned to a codebehind handler, which is implemented in a supported .NET language. For example, it’s a common task to do something when a Button is clicked. So, first a Button is declared with the XAML code shown in Example 3-1.

    Example 3-1. XAML Button declaration

    <Button
        OnClick="ButtonClickedHandler"
        Name="MyButton"
        Width="50"
        Content="Click Me!" />

    Then, a corresponding codebehind handler is declared, and, when the Button is clicked, the handler is automatically executed (Examples 3-2 and 3-3).

    Example 3-2. Button OnClick handler in C#

    void ButtonClickedHandler(object sender, RoutedEventArgs eventArgs)
    {
         MyButton.Width = 100;
         MyButton.Content = "Thank you!";
    }

    Example 3-3. Button OnClick handler in VB.NET

    Sub ButtonClickedHandler(ByVal sender As Object,
                           
    ByVal eventArgs as RoutedEventArgs )
        MyButton.Width = 100
       
    MyButton.Content = "Thank you!"
    End Sub

    In both Examples 3-2 and 3-3, the handler will change the width of the Button from 50 to 100 and change the text displayed on it from “Click Me!” to “Thank you!”. All XAML attributes can be manipulated within code because they are simply XML representations of actual CLR class attributes. You could just as easily change the button’s background color, height, and even its position in code, just as you could in a traditional Windows application.

    It is also acceptable to inline code in the XAML file by specifying the <x:Code> element. All inline code must be enclosed in the <CDATA[...]]> tag to ensure that the parser does not try to interpret the code. The XAML code from Example 3-1 and the C# code from Example 3-2 yield Example 3-4.

    Example 3-4. Inlining code within a XAML file

    <Button
        OnClick="ButtonClickedHandler"
        Name="MyButton"
        Width="50"
        Content="Click Me!" />

    <x:Code>
     
    <![CDATA
       
    void ButtonClickedHandler(object sender, RoutedEventArgs eventArgs)
        {
          MyButton.Width = 100;
          MyButton.Content = "Thank you!";
       
    }

    Example 3-4. Inlining code within a XAML file (continued)

      ]]>
    </x:Code>

    Application developers familiar with C# or VB.NET will immediately grasp the concept of codebehind and inline code and will be able to apply their existing skills to develop the code that drives the application.

    More XML Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "The Basics of XAML," published by...
     

    Buy this book now. This article is excerpted from chapter 3 of The Basics of XAML, written by Lori A. MacVittie (O'Reilly, 2006; ISBN: 0596526733). Check it out today at your favorite bookstore. Buy this book now.

    XML ARTICLES

    - More on Triggers and Styles and Control Temp...
    - Looking at Triggers with Styles and Control ...
    - A Closer Look at Styles and Control Templates
    - Styles and Control Templates
    - Properties and More in XAML
    - Elements and Attributes in XAML
    - XAML in a Nutshell
    - Importing XML Files into Access 2007
    - Using MSXML3.0 with VB 6.0
    - MSXML, concluded
    - MSXML, continued
    - MSXML Tutorial
    - Generating XML Schema Dynamically Using VB.N...
    - XSL Transformations using ASP.NET
    - Applying XSLT to XML Using ASP.NET





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek