XAML becoming a new HOT wave in Microsoft Windows Longhorn programming model. XAML syntactically inherited from XML is a scripting programming language used to write applications that covers both Windows and Web worlds.
Microsoft is well-known and a proven expert in evolving new technologies based on existing technologies by inheriting from the existing technologies, adding new features to them and improvising the existing features. Microsoft .NET Framework and C# language are the best example of it and XAML is no exception.
XML is designed to store data. Microsoft extends the usefulness and power of XML in form of XAML by making it a .NET programming language. Now as a developer, you may find yourself scripting XAML tags to write Windows or Web applications. Besides the easy to use and understand, XAML is flexible enough to provide options to use any .NET programming language as codebehind. Not only you can write the UI definitions, such as size, color, and layout of Windows but you can also write the events and methods in XAML files.
XAML takes advantage of existing XML syntaxes. Almost every .NET developer is familiar with XML. If you are not, you better get started now.
What is XAML?
XAML (Extensible Application Markup Language) is a new scripting language based on XML that allows Longhorn developers to build and managed UI applications via scripting. If you remember Windows Forms model, the settings of a Form and its child controls were used to store in a .cs file with all the attributes of the Form and controls. Now, all these attributes can store in an xaml file and that can run without any additional code. For example, the following code shows how to create a Form with a button saying "Hello, C# Corner".
<?xml version="1.0" standalone="yes"?>
<Window>
<Button>Hello, C# Corner</Button>
</Window>
To run this code, simply type this code in a text editor, save as xaml extension and double click on it. Of course you have to install Longhorn SDK on your machine if you are not working on Longhorn.
Similar to the above sample code, XAML has tags and attributes for every possible Windows control. You can also embed your code (C# or VB.NET) in an XAML file.
Why XAML? Another Scripting Language?
XML was designed to store data. There is no concept of presentation or events. XAML extends the functionality of XAML by adding user-interfaces and events. Now you can write a fully functional Windows Longhorn application in XAML that will work on both Windows or Web environments. The main purpose of XAML to bring both Windows and Web programming worlds together.
As I said earlier, many of the programmers (Microsoft as well as non-Microsoft) are already familiar with XML and how about a language that looks familiar to these XML developers? Here comes Microsoft with XAML. After the success of C#, one can't doubt the success of XAML.
How does XAML Work?
If you are familiar with Windows Forms, a Form represents a window. A window has properties such as width, height, background color, foreground color and so on. These property values can be set at design-time as well as at run-time. In case of design-time, the values are stored in a file that is associated with a window. When a C# compiler compiles the code, the values of a window properties get stored in the executables.
In case of XAML, a panel represents a window. The values of the panels are stored in the XAML itself in form of XML attributes and elements. When you open an XAML file in browser (Windows Longhorn), the operating system is smart enough to read the XAML tags, elements, and attribute values and creates a window with the given attribute and element values.
All controls or windows in XAML have a predefined tag and each of these tag has properties related to that control or window. For example, the following code for Button tag sets a few properties of Button control:
<Button Width="20" Height="10">OK Button</Button>
XML can have CodeBehind Files
Similar to ASP.NET pages, you can use any .NET programming language to write code for XAML events. For example, you can use C# language to write your code and let XAML provides the layout. An XAML file can embed C# code or it can be stored in a separate codebehind (.cs) file.
XAML replacement of Windows Forms and ASP.NET
True. XAML can be used in place for both Windows Forms and ASP.NET. Now you can write a single application in XAML that would work for both Windows and Web users. There is no need to use two different technologies for two different types of users. Similar to Windows Forms, XAML also support codebehind programming using a new technology called Avalon. Avalon is used to write user-interface applications programmatically.
However, both Windows Forms and ASP.NET will still be used for operating systems that don't support Windows Longhorn applications.
Avalon: Object Model for XAML
Avalon is an object-oriented class library that provides an object model for XAML. All the controls in XAML has a class associated with it in Avalon that can be used to write applications programmatically. See Avalon Section for more details.
Summary
In this article, I discussed the need of XAML and why developers may want to think about moving to XAML. XAML is a technology to write a single application that targets both Windows and Web users without any modifications.