Tag Archives: XAML

XAML – Extensible Application Markup Language

Extensible Application Markup Language (XAML) is a .NET technology. It’s a markup language that can be used to help create desktop applications, web pages, and printable documents.
XAML is a declarative XML-based language that defines objects and their properties in XML.
It provides abbreviated markup syntax for specific types of elements that take advantage of the underlying Common Language Runtime (CLR) class constructors. 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.
XAML is pure markup, which means that while the names of event handlers are specified as attributes, you must implement the actual logic of the event handler in code like C# or VB.net.
Every event in XAML can be assigned to a codebehind handler,which is implemented in a supported .NET language. eg: Button click event
<Button
OnClick=”ButtonClickedHandler”
Name=”MyButton”
Width=”50″
Content=”Click Me!” />

a corresponding codebehind handler is declared
void ButtonClickedHandler(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.
It is also acceptable to inline code in the 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. eg:
<x:Code>
<![CDATA
void ButtonClickedHandler(object sender, RoutedEventArgs eventArgs)
{
MyButton.Width = 100;
MyButton.Content = “Thank you!”;
}
]]>
</x:Code>

for application logic developers to access specific XAML elements, the elements must be named using either the Name or ID attribute which will be used to manipulate the element in the code.
Three basic rules to follow when declaring XAML elements:
1) XAML is case-sensitive. Element and attribute names must be properly cased.
2) All attribute values, regardless of data type, must be enclosed in double quotes.
3) The resulting XML must be well-formed. like end tag etc.

Elements can be declared all on one line, or attribute declarations can be split across lines; formatting is completely up to you. Because XAML is compiled into Binary Application Markup (BAML) file (This is a binary representation of XAML auto-genned by Visual Studio) before deployment, the amount of space taken up by elements in a XAML file is irrelevant. There are no advantages to using less space by declaring elements on a single line and no disadvantages to the formatting.

All XAML elements are an XML representation of CLR classes, but not all CLR classes are represented in XAML.
XAML element represents a .NET CLR class. but not all CLR classes are represented in XAML. Most of those represented are user-interface elements and are ultimately derived from System.Windows.UIElement, System.Windows.FrameworkContentElement.

XAML elements can be organized into five basic categories
1) Root elements
A page is required to have one root element. Windows and Page elements are the most common root elements.
2) Panel elements
These elements help you to lay out your user interface.  eg: StackPanel, DockPanel, Grid and Canvas.
3) Control elements
Controls are interactive and allow the user to enter data, make choices, and perform other interactive tasks.
eg: Button, CheckBox, RadioButtonList, ComboBox, MenuItem
<MenuItem Header=”First Menu Item”>
<MenuItem Header=”First Child Item” />
<MenuItem Header=”Second Child Item” />
</MenuItem>
4) Geometric elements
This kind of elements helps you to draw shapes and geometric graphics on your UI. eg: LineGeometry, EllipseGeometry, PathGeometry.
5) Document elements
use to deal with presentation of a document. eg: Bold, LineBreak and Italic, Paragraph, List, Block, Figure and Table.
Example:
<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″>
Content=”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>
Benefit of XAML
1) The Run Time :- The ASPX uses the ASP.Net Run Time which is loaded in the IIS, rather Silver Light Runtime is a portion of CLR. Which we can download dowload in to the Client Machine and perform operations.
2) The XAML supports Rich User Interface, which means no need of JS and others. We can use the Straight away .NET WPF Animation Functions.
3) It also supports the Out Of Browser Support. (We can use the Server Web Application as a Client Application). Its included in Silverlight 4.0
4) 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.

Q: Can XAML be used to develop both Web and client-server applications?
Ans: XAML is used as part of Windows Presentation Foundation to write smart client applications that take advantage of the power of the PC and benefit from all of the power of the CLR. For Windows Presentation Foundation applications, the logic runs on the client, unlike ASP.NET, which processes on the server. ASP.NET continues to be the answer to producing powerful Web applications that take advantage of the benefits of the CLR on the server.

Advertisements

Leave a comment

Filed under .Net, C#