Introduction
What is XAML?
XAML is the primary format for declaring a Silverlight UI and elements in that UI. Typically at least one XAML file in your project represents a "page" metaphor in your application for the initially displayed
XAML is an XML-based markup language. XAML is a declarative language used in Silverlight to create UI, such as controls, shapes, text, and other content presented on the screen. XAML files are XML files that generally have the .xaml extension.
XAML is XML-based and therefore must follow XML rules, which includes being well formed.
XAML, like all XML-based languages, is case-sensitive.
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.
There are tools like Visual Studio or Expression Blend to create the UI and they also generate XAML for the UI.
Every element in a XAML document maps to an instance of a .NET class. The name of the element matches the name of the class exactly. For example, the element <Button> instructs WPF to create a Button object.
As with any XML document, you can nest one element inside another. As you’ll see, XAML gives every class the flexibility to decide how it handles this situation.
You can set the properties of each class through attributes. However, in some situations an attribute isn’t powerful enough to handle the job. In these cases, you’ll use nested tags with a special syntax.
The basic syntax for declaring XAML elements and attributes is:
<ElementName AttributeName="Value" AttributeName="Value" ... />
How does this work?
The XAML parser needs to know the .NET namespace where this class is located.
The xmlns attribute is a specialized attribute in the world of XML that’s reserved for declaring namespaces
There are two ways to declare a namespace:
1. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
In first, The first declaration maps the Silverlight core XAML namespace as the default:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
In Second, The namespace is mapped to the prefix x. That means you can apply it by placing the namespace prefix before the element name (as in <x:ElementName>).
For instance, in order to join any code-behind to a XAML file through a partial class, you must name that class as the x:Class attribute in the root element of the relevant XAML file.
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 let declare a button in XAML code:
<Button onClick="ButtonClick"
Name="Bt1"
Width="50"
Content="Click Me" />
In code behind we will declare the handler which execute when button is clicked.
void ButtonClick(object sender, RoutedEventArgs eventArgs)
{
MyButton.Width = 100;
MyButton.Content = "Thank you!";
}
All XAML attributes can be manipulated within code because they are simply XML representations of actual CLR class attributes.
You can also line the code in 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.
<x:Code>
<![CDATA
void ButtonClick(object sender, RoutedEventArgs eventArgs)
{
MyButton.Width = 100;
MyButton.Content = "Thank you!";
}]]>
</x:Code>
In next part we will see what are Elements in XAML.
cd07f325-31d4-44b3-8136-d661baf071d3|0|.0