Silverlight XAML Basics

XAML is a case-sensitive declarative language, based on XML that lets you design the user interface of a Silverlight application in descriptive markup. Similar to the way ASP.NET or Windows Forms work with the concept of a code-behind file, XAML files map to managed-code partial classes where you can write in your language of choice. XAML is important for the evolution of how you create the user interface because the user interface is separate from the code files. This means that a designer using tools like Expression Blend can create a UI using XAML, and that same XAML can be used in Visual Studio and integrated into a larger project. As a matter of fact, Expression Blend and Visual Studio share the same project structure, so the .csproj and .vbproj files can be opened by either tool. The ability for a designer to express a user interface and have it directly used without alteration in an application is something that has never been possible with Microsoft tools. There has always been a large amount of throwaway art work, because developers would get a mockup and try to duplicate it.

XAML files have a .xaml extension and, at first glance, might be confused with an XML data file. This makes sense, because XML (Extensible Markup Language) is the basis for XAML (Extensible Application Markup Language). The following code shows the default Silverlight XAML file when you create a new Silverlight application using Visual Studio, which is also broken down in the table that follows the code:

<UserControl x:Class="SilverlightApplication6.Page"

xmlns="http://schemas.microsoft.com/winfx/200 6/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/200 6/xaml" Width="400" Height="300">

<Grid x:Name="LayoutRoot" Background="White">

</UserControl>

The following table describes the preceding code:

XAML

Description

<UserControl x:Class="SilverlightApplication6.Page"

Opening object tag of the root UserControl

xmlns="http://schemas.microsoft.com/winfx/200 6/xaml/ presentation"

Default Silverlight namespace mapping

xmlns:x="http://schemas.microsoft.com/winfx/200 6/xaml"

Default XAML namespace mapping

Width="400" Height="300">

Default Height and Width properties of the UserControl

<Grid x:Name="LayoutRoot" Background="White">

Opening tag for the Grid layout element

</Grid>

Closing tag for the Grid layout element

</UserControl>

Closing tag for the root UserControl object

In Chapter 1, you learned about the namespace declaration in Visual Studio for other assemblies than the core Silverlight and code Silverlight XAML namespaces. If you had a compiled user control or class file that you wanted to include in your opening UserControl declaration, you would use a custom prefix and point to the fully qualified namespace and object you are adding to the page. For example, if you need to access features in the System.Windows assembly, you would add the following namespace declaration to your page:

xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"

where vsm is your custom prefix (which can be whatever prefix you would like), the clr-namespace you are using is System.Windows, and the actual assembly name is System.Windows. Later in this chapter, we will talk about the XAML namespace, whose objects are prefixed with the default x: identifier.

0 0

Post a comment

  • Receive news updates via email from this site