Logo
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
EPL Standings
 
 
Windows XP

Silverlight and ASP.NET : XAML

8/5/2011 6:23:59 PM
XAML stands for eXtensible Application Markup Language. XAML is a dialect of XML invented primarily for constructing object graphs declaratively. Although it was originally developed to support WPF, it's found its way into other uses—most notably Windows Workflow Foundation. Silverlight presentations are described through XAML.

Silverlight uses XAML to build the visual tree. Take another look at the MainPage class from the last example:

<UserControl x:Class="SilverlightSite.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="Hello World!"
x:Name="theButton"
Click="theButton_Click"></Button>
</Grid>
</UserControl>

The XAML constructs an instance of the UserControl class, associates it with the class named SilverlightSite.MainPage, and initializes the DesignWidth property to 400 and the DesignHeight property to 300. Next, the XAML constructs an instance of the Grid class, assigns the name LayoutRoot to the Grid, and sets the Background to white. Finally, the XAML constructs an instance of the Button class, names it theButton, assigns the string "Hello World!" to the Content property, and then sets up the Click event so that it's handled by the theButton_Click method (which is a member of the SilverlightSite.MainPage class). After spending some time with ASP.NET, this should look awfully familiar—this is how the ASP.NET Page class builds its control tree. The major difference here is really between the ASPX syntax and the XAML syntax.

1. Constructing the Visual Tree

The role of XAML in Silverlight is to construct the Silverlight component's visual tree. This happens in the MainPage constructor. Notice that the code produced by Visual Studio contains a call to the method InitializeComponent. InitializeComponent parses the XAML to create the objects composing the visual tree and initializes the properties mentioned in the XAML. For example, the previous snippet and the following snippet produce the same visual tree:

Page ConstructVisualTreeProgrammatically()
{
Page page = new Page();

Grid grid = new Grid();
Button button = new Button();
button.Click += this.theButton_Click;
button.Content = "Hello World!";
grid.Children.Add(button);

return page;
}

2. XAML and Namespaces

Near the top of the XAML in the earlier example, you can see four XML namespaces defined, the first two of which I discuss here (they're most important). The default namespace is assigned to be "http://schemas.microsoft.com/winfx/2006/xaml/presentation". This namespace scopes the typical Silverlight features and the control canon. For example, by making this the default namespace, you can simply use the <Button /> tag without any prefixes. The second namespace, "x", is assigned to "http://schemas.microsoft.com/winfx/2006/xaml". This namespace defines Silverlight keywords such as Name (used to name XAML elements to make them available programmatically) and Key (used to assign key/value pairs within Silverlight resource dictionaries).

You can add other namespaces as necessary. For example, if you want to make any types you define in the SilverlightSite assembly available within XAML, you could add a namespace. For example, if the Silverlight component assembly has a type named CustomButton in it, and you'd like to include it as part of the visual presentation, you could add the namespace as follows, and then use the component from within the XAML as shown in the following bold text:

<UserControl x:Class="SilverlightSite.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:my="clr-namespace:SilverlightSite"

mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<my:CustomButton
Content="Hello World!"
x:Name="theButton"
Click="theButton_Click"></my:CustomButton>

</Grid>
</UserControl>
Other -----------------
- Silverlight and ASP.NET : Creating a Silverlight Application
- Microsoft ASP.NET 4 : Developing a Web Part
- Microsoft ASP.NET 4 : The Web Parts Architecture
- Microsoft ASP.NET 4 : Handlers and Session State & Generic Handlers (ASHX Files)
- Microsoft ASP.NET 4 : HTTP Handlers - Handlers and IHttpHandler
- Microsoft ASP.NET 4 : HTTP Handlers - The Built-in Handlers
- Microsoft ASP.NET 4 : ASP.NET Request Handlers
- Microsoft ASP.NET 4 : HttpModules (part 2) - Seeing Active Modules & Storing State in Modules
- Microsoft ASP.NET 4 : HttpModules (part 1) - Existing Modules & Implementing a Module
- Microsoft ASP.NET 4 : The HttpApplication Class and HTTP Modules - Overriding HttpApplication
- Microsoft ASP.NET 4 : Diagnostics and Debugging - Error Pages
- Microsoft ASP.NET 4 : Diagnostics and Debugging - Debugging with Visual Studio
- Microsoft ASP.NET 4 : Diagnostics and Debugging - Application Tracing
- Microsoft ASP.NET 4 : Diagnostics and Debugging - Page Tracing
- Microsoft ASP.NET 4 : Caching and State Management - The Wizard Control: An Alternative to Session State
- Microsoft ASP.NET 4 : Caching and State Management - Tracking Session State
- Microsoft ASP.NET 4 : Caching and State Management - Configuring Session State
- Microsoft ASP.NET 4 : Caching and State Management - Session State and More Complex Data
- Microsoft ASP.NET 4 : Caching and State Management - Introduction to Session State
- Installing and Configuring a Modem : Modifying the Modem’s Advanced Properties
 
 
Most view of day
- Windows Server 2012 : Installing and Managing Hyper-V in Full or Server Core Mode - Verifying Hyper-V requirements
- How to Troubleshoot Driver Problems (part 1) - How to Find Updated Drivers, How to Roll Back Drivers
- Microsoft Visio 2010 : Creating Web Pages from Visio Drawings (part 3) - Fine-tuning Web Pages and Battling Bugs - Customizing Web Page Output
- Microsoft Lync Server 2010 : Planning for Voice Deployment - Voice Routing
- Windows Server 2012 : Provisioning and managing shared storage (part 7) - Managing shared storage - Managing volumes, Managing shares
- Windows Server 2012 : Support for open standards
- Windows Server 2012 Group Policies and Policy Management : GPO Administrative Tasks - Backing Up and Restoring Domain GPOs
- Microsoft PowerPoint 2010 : Assigning Transitions to Slides
- Managing SharePoint 2010 with Windows PowerShell : Managing SharePoint 2010 Sites (part 1)
- Deploying Applications (part 3) - Injecting in a Disk Image, Repackaging Legacy Applications
Top 10
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 3) - Creating IPv4 DHCP Scopes
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 2) - Installing DHCP Server and Server Tools
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 1)
- Windows Server 2012 : DHCP,IPv6 and IPAM - Understanding the Components of an Enterprise Network
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 3) - Translating Text with the Mini Translator
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 2) - Translating a Word or Phrase with the Research Pane
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 1) - Setting Options for the Research Task Pane, Searching with the Research Task Pane
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 2) - Ending a Linked Notes Session, Viewing Linked Notes
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 1) - Beginning a Linked Notes Session
- Microsoft OneNote 2010 : Doing Research with Side Notes (part 3) - Moving Side Notes to Your Existing Notes
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro