Advertisment

What is XAML Programming?

author-image
PCQ Bureau
New Update

Windows application programming has always differed from applications that were run from the Web. That is, not only the languages, but even the programming model were completely different. 

Advertisment

There were previous attempts to bridge this gap. For instance, HTAs (HTML Applications) let you create HTML + Script based files that can be run directly on the user’s machine as a standalone application. Also using Visual Studio.NET, you could program for the Web as easily as you could for the desktop–by simply writing event handlers for buttons and other controls using the same language as you would for the desktop application (VB.NET or C#).

However, internally both were quite different in the way the code was written and structured. For instance, take a look at the first code block below which is for a simple Windows application that writes “Hello, World” when you click a button on the window.






Public Class Form1


Inherits System.Windows.Forms.Form


#Region “ Windows Form Designer generated code “


...


Friend WithEvents Label1 As System.Windows.Forms.Label


Friend WithEvents Button1 As System.Windows.Forms.Button


Private Sub InitializeComponent()


...


Me.Label1.Location = New System.Drawing.Point(8, 16)


Me.Label1.Name = “Label1”


Me.Label1.TabIndex = 0


...


Me.Button1.Location = New System.Drawing.Point(8, 48)


Me.Button1.Name = “Button1”


Me.Button1.TabIndex = 1


Me.Button1.Text = “Click Me!”


...


Me.ClientSize = New System.Drawing.Size(292, 266)


Me.Controls.Add(Me.Button1)


Me.Controls.Add(Me.Label1)


Me.Name = “Form1”


Me.Text = “Form1”


Me.ResumeLayout(False)


End Sub





#End Region

























Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



Label1.Text = “Hello, World!”


End Sub


End Class

Advertisment

The code above declares two controls (a label and a button), sets some properties and places them on the form. The button also has an event handler for the Click event which changes the Text property of the label to “Hello, World”. The same stuff in ASP.NET looks like the following.






<%@ Page Language=”vb” AutoEventWireup=”false” Codebehind=”PCQTest.Aspx.vb” Inherits=”indevNET.PCQTest”%>


PCQTest



























Advertisment

And the code-behind has:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



Label1.Text = “Hello, World”


End Sub





As you can see, the same functionality is given by two completely different programming layouts. Windows programming has all the properties and code in a single place with explicit coding. Whereas the Web application is declarative using markup and with a code behind that lets you code in a different place.


XAML (pronounced ZAMEL) comes in at this point to solve this conundrum and give a unified programming model for both Web and Windows. Although XAML will be available from Microsoft only in the next version of Windows, codenamed Longhorn, there are ways you can do XAML programming right now itself. For instance, Xamalon lets you create XAML applications for WinXP right now.

Advertisment

XAML, which stands for eXtensible Application Markup Language, uses an XML-derived syntax to build applications using declarative statements. Take a look at the XAML code below for an example.

This simply creates an application with a single OK button with a blue background. Similarly, you can easily set more properties, either inline in the main tag (just like in HTML) or explicitly as shown above.

Advertisment

How does XAML work? To put it simplistically, each control in the .NET framework for Longhorn is ‘mapped’ to a XAML tag. Each property is also mapped as the tag’s attribute. So you can declare the entire UI for the Windows application in the declarative statements. You can even do more advanced things by setting some controls to react or manipulate other controls for changing orientation, creating animation effects and more without writing any kind of ‘normal’ code! Executing a XAML

file converts the tags to C# classes in the background and then run within the .NET framework context. 

Of course, when you do need to add more functionality to the application, you can have inline C#/VB.NET code or as in ASP.NET, create the code in a ‘code-behind’ file and work on that instead. This means that a UI designer can work on the Windows application UI using the XAML file, and the developer can work with the backend code just like what one does with large Web based projects now! 

This is the point at which we see the integration of Windows and Web-based applications. Already Longhorn has many new integration features, that let you forget about which technology you are using, and instead just get the best out of all of them. However, since Longhorn is still in very early stages, it would be interesting to keep an eye on all the new things happening there.

Vinod Unny,

Enterprise InfoTech

Advertisment