SILVERLIGHT Tutorial - What is XAML?
In this article I will try to explain what is XAML and how it is used in Silverlight. Extensible Application Markup Language (XAML, pronounced zammel) is a declarative XML-based language created by Microsoft which is used as a user interface markup language to define UI elements, data binding, event handlers, and other features. XAML files are XML files that generally have the .xaml file name extension.
Silverlight, WPF and XAML
As explained previously XAML is a XML file which defines the UI elements. This XML file can be read by WPF framework or Silver light framework for rendering. Microsoft first developed WPF and they used XAML files to describe the UI elements to the WPF framework. Microsoft then extended WPF and made WPF/E which helped to render the UI in the browser. WPF/E was the code name for Silverlight. Later Microsoft launched Silverlight officially.
So the XAML just defines the XML structure to represent the UI elements. Both the frameworks i.e. WPF and Silverlight then read the UI elements and render the UI elements in the respective platform.
The Role of XAML in Silverlight
1. XAML is the primary format for declaring a Silverlight UI and elements in that UI.
2. XAML is the format that is used for declaring styles and templates applied to the logical basis of Silverlight controls and UI.
3. Silverlight XAML defines the visual appearance of a UI, and an associated code-behind file defines the logic. The UI design can be adjusted without necessarily making changes to the logic in code-behind. XAML in this role simplifies the workflow between individuals who might have a primary visual design responsibility and individuals who are responsible for application logic and information design.
The following example shows the contents of a very basic Silverlight XAML file.
The XAML Language XAML Namespace
One particular XAML namespace that is declared in nearly every Silverlight XAML file is the XAML namespace for elements that are defined by the XAML language. By convention, the XAML language XAML namespace is mapped to the prefix x:. The default project and file templates for Silverlight projects always define both the default XAML namespace (no prefix, just xmlns=) and the XAML language namespace (to prefix x
as part of the root element. The x: prefix/XAML language XAML namespace contains several programming constructs that you will use frequently in your Silverlight XAML.
The following are the most common x: prefix/XAML namespace constructs:
1. x:Key: Sets a unique user-defined key for each resource in a ResourceDictionary. The key token string is used as the argument for StaticResource Markup Extension to retrieve any such resource from another XAML usage.
2. x:Class: Specifies the CLR namespace and class name for the class that provides code-behind for a XAML page, and names the class that is created or joined by a markup compiler in the Silverlight application model. You must have such a class to support code-behind, or to support being initialized as a RootVisual. It is for these reasons that you almost always see x: mapped, even if there are no resources and you never use x:Name.
3. x:Name: Specifies a run-time object name for the instance that exists in run-time code after an object element defined in XAML is processed. You use x:Name for element-naming scenarios in the occasional cases where the more convenient FrameworkElement.Name property is not supported.
Case Sensitivity in XAML
The XAML language is fundamentally based on the XML language so XAML as a language is case-sensitive. The names of XAML elements and attributes are case-sensitive. The value of an attribute is potentially case-sensitive; this will depend on how the attribute value is handled for particular properties.
XAML File Compiled or Built on runtime
XAML files are usually compiled rather than parsing on runtime. But it also supports parsing during runtime. When we build a XAML based project, you will see it creates g.cs extension in obi\Debug folder. Therefore, for every XAML file you will find a g.cs file.
Silverlight Client Versions and XAML Parsing
In Silverlight 4, Silverlight uses a different XAML parser than was used in Silverlight 3. The XAML parser is a component of the Silverlight core assemblies. This means that if a client is still using Silverlight 3, that client is still using the previous XAML parser. Moreover, if a Silverlight application indicates that is intended to run against Silverlight 3 based on its application manifest information, then the previous XAML parser is used to load the XAML, even if the client is actually the Silverlight 4 run time. This is enabled because both parsers exist in a Silverlight 4 client, which provides backward compatibility for XAML parsing.