Introduction to XAML
page 4 of 9
by Keyvan Nayyeri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 56867/ 564

Write First Application with XAML

It is better to step in code and see the simplest XAML code in action.  Listing 1 provides a sample application that lets you to put a text in a TextBox then click on a Button.  Listing 2, which is the handler code for this click, does the rest and shows your text in a MessageBox.  The output is provided in Figure 1.

Listing 1

<Window x:Class="IntroductionToXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Introduction To XAML" Height="200" Width="300"
    >
  <StackPanel>
    <TextBox Name="MyTextBox"
             Width="200"
             Height="30"
             Margin="40" />
    <Button Click="ButtonClickHandler"
            Width="60"
            Height="20">
      Click me!
    </Button>
  </StackPanel>
</Window>

Listing 2

void ButtonClickHandler(object sender, RoutedEventArgs e)
{
    MessageBox.Show(string.Format
        ("You typed '{0}' in TextBox!", MyTextBox.Text), "XAML");
}

Figure 1

Initially you can find some points from this simple code.

1.      XAML file starts with a <Window /> element as root.  This element will be the most common root element for your XAML files.

2.      Default namespace for XAML file is "http://schemas.microsoft.com/winfx/2006/xaml/presentation" and it has another "x" namespace which is set to "http://schemas.microsoft.com/winfx/2006/xaml."

3.      Main elements are embedded in a <StackPanel /> element.  StackPanel is one of the XAML elements for laying out other elements.

4.      There is no identifier for my Button.  In XAML you do not need to put a name as identifier for your controls unless you want to refer to them from other parts of your code.  This helps you to have shorter codes.  But as I want to refer to my TextBox in my code, I defined a Name attribute for it and set it to MyTextBox.

5.      My Button has a Click attribute which is set to an event handler in my code to handle the Click event of this button.

Note that I could merge Listing 1 and Listing 2 codes in one XAML file and use inline model to write my click event as you see in Listing 3.

Listing 3

<Window x:Class="IntroductionToXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Introduction To XAML" Height="200" Width="300"
    >
  <StackPanel>
    <TextBox Name="MyTextBox"
             Width="200"
             Height="30"
             Margin="40" />
    <Button Click="ButtonClickHandler"
            Width="60"
            Height="20">
      Click me!
    </Button>
  </StackPanel>
  <x:Code>
    <![CDATA[
        void ButtonClickHandler(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(string.Format
                ("You typed '{0}' in TextBox!", MyTextBox.Text), "XAML");
        }
      ]]>
  </x:Code>
</Window>

Note that <x:Code /> element must be a direct child of root element in a XAML file.  Since you do not have great tools, such as Intellisense in inline model, and your code will be long and hard to read in this case, I do not recommend using inline model.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 7 and 6 and type the answer here:

User Comments

Title: diff b/w xaml and html   
Name: rajkumar
Date: 10/3/2009 3:10:30 AM
Comment:
hi im raj i persuing mca iwant to know about xaml with examle plz feedback me soon
Title: How to add rows programmatically!   
Name: awe
Date: 6/19/2009 8:54:08 AM
Comment:
OK, I found out, it was actually adding correctly, it was just that the container shrimped the grid so all rows became too narrow. I put the grid into a scroll container, and - voilá !
Title: How to add rows programmatically?   
Name: awe
Date: 6/19/2009 8:17:39 AM
Comment:
Just to make it clear: the label, labelEnglish and textBox are labels and textbox that are created and set content on before the code you see.
Title: How to add rows programmatically?   
Name: awe
Date: 6/19/2009 8:15:10 AM
Comment:
I want to be able to add lables and text boxes to the grid content programmatically. How do I do that? I tried with the following code, but it doesn't seem to work. grid (with lowercase g) is the grid object I am working with. Grid (with uppercase G) lets me run the static methods to set column and row number (I didn't find any other way to do this).

grid.RowDefinitions.Add(new RowDefinition());
grid.Children.Add(label);
Grid.SetColumn(label, 0);
Grid.SetRow(label, grid.RowDefinitions.Count - 1);
grid.Children.Add(labelEnglish);
Grid.SetColumn(labelEnglish, 1);
Grid.SetRow(labelEnglish, grid.RowDefinitions.Count - 1);
grid.Children.Add(textBox);
Grid.SetColumn(textBox, 2);
Grid.SetRow(textBox, grid.RowDefinitions.Count - 1);
Title: how to write XAML code for sign in page in silverlight   
Name: ling
Date: 5/18/2009 6:57:58 AM
Comment:
i am quite new to silverlight n XAML..and my project need to create a login,sign up and forget password page.may i know how to write coding for all of dem.
Title: Excellent   
Name: Guru Natesh
Date: 4/14/2009 6:05:06 AM
Comment:
Awesome information abt Xaml for starters
Title: Excellent   
Name: Mr.Gren
Date: 2/4/2009 11:49:38 PM
Comment:
Very Useful for XAML beginners and VS2008 users
Title: Excellent   
Name: JohnA. Blesson
Date: 1/20/2009 2:54:45 AM
Comment:
Very Useful for XAML beginners and VS2008 users
Title: Good   
Name: Palani
Date: 10/23/2008 7:56:26 AM
Comment:
I m interest in ASP.NET. Contact my no 9600478244
Title: Good Article!!   
Name: Tanvir Ehsan
Date: 9/18/2008 4:24:42 AM
Comment:
Thanks Author Keep continuing to such quick learning guide for the beginner!!
Title: Fine   
Name: M.S. Feroz
Date: 9/2/2008 2:09:20 AM
Comment:
Excellent article for beginner. thanks
Title: Good article for biginner   
Name: Bhavesh patel
Date: 4/11/2007 8:47:02 AM
Comment:
Good article for biginner
Title: My point of View Of XAML   
Name: Zaheer Ahmed
Date: 1/20/2007 5:11:49 AM
Comment:
hi i m software engineer and currently i m working on VS.NET 2.0 and i wana switch to 3.0 this article help me to know wht is XAML Thanx
Title: Events in Windows Presentation Foundation   
Name: Keyvan Nayyeri
Date: 12/14/2006 10:22:36 AM
Comment:
Fifth part of these tutorials about Events in Windows Presentation Foundation:
http://aspalliance.com/1088_Events_in_Windows_Presentation_Foundation
Title: Animations in XAML   
Name: Keyvan Nayyeri
Date: 10/25/2006 12:36:09 AM
Comment:
Fourth part of these tutorials about Animations in XAML:
http://aspalliance.com/1050_Animations_in_XAML
Title: Resources in XAML   
Name: Keyvan Nayyeri
Date: 10/12/2006 12:58:37 AM
Comment:
Third part of these tutorials about Resources in XAML:
http://aspalliance.com/1032_Resources_in_XAML
Title: Layout in XAML   
Name: Keyvan Nayyeri
Date: 10/9/2006 2:07:52 PM
Comment:
Second part of these tutorials about Layout in XAML:
http://aspalliance.com/1023_Layout_in_XAML
Title: Thanks   
Name: Keyvan Nayyeri
Date: 10/5/2006 1:54:25 AM
Comment:
Thank you. I think I have many items there ;-)
Title: Added to NetFXGuide.com   
Name: Francesco
Date: 10/5/2006 1:43:36 AM
Comment:
Hi, I just added your intro to the XAML Section of NetFXGuide.com (http://www.netfxguide.com/guide/xaml.aspx).

Best,

Francesco
NetFXGuide.com

Product Spotlight
Product Spotlight 






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 11/21/2009 10:12:49 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search