Wednesday, April 24, 2024 10:35

Table of contents >> Graphical User Interface > XAML

XAML

XAML (Extensible Application Markup Language) is the language behind the visual presentation of an application that you develop in Microsoft Expression Blend, just as HTML is the language behind the visual presentation of a Web page. Though it is not this website’s purpose to teach XAML, and though programmers can develop a program’s interface almost entirely without knowing XAML, in this lesson I will talk briefly about what XAML is, what does it look like, what it does and how we can use it.

So, what is XAML? The short definition is that XAML is a simple XML based declarative language in the sense that it defines the WHAT and HOW you want to do. The main advantage of XAML is the separation of how the program looks and how it behaves. The main disadvantage of WinForms was that the functional code and the visual interface were both created in C# or VB.NET by the programmer, and they were not interchangeable. In addition, it required more effort from the programmer, to create both the GUI and the functionality, which many coders loathed, since they were not designers.

With the arrival of XAML, this problem was finally solved. XAML was decoupling the visual interface from the code behind, making them two separate processes. This meant that a qualified designer could design the GUI in the same time a coder was developing the functionality, and that the two components would interact with each other. Another beneficial consequence of this was that different GUI’s could be created for the same functional code.

As a schematic, it looks like this:

XAML

In the previous lesson, I was explaining that the file MainWindow.xaml is displayed as a set of three main panels, one showing the visual representation of our GUI, the second displaying the XAML representation of the same visual interface, and the third panel indicating any errors we might develop in the process of creating our interface.

If you take a look in the second panel, you will notice some codes similar to these:

I know the above codes don’t tell you much, and it is understandable. For the most part, you, as programmers, won’t even have to understand them. Though, even if you have no idea about XML at all, you should recognize a pattern that is similar to our C# codes: there are structures with a beginning and an end, containing a body, and they are easily identified by indentation. You can clearly see that we have a Window object enclosed between <Window>…</Window> and that this object contains another object called Grid, also delimited by a header and an ending: <Grid>…</Grid>. For those who have coquetted with web design before, this even looks very similar to some strange HTML.

XAML works on a parentchild structure basis. There are parent objects – in our case Window, which contain child elements, like our Grid. By default, a grid is set as the first element after page. By clicking on various elements in XAML, you will notice changes in the visual design panel, indicating the current selected visual object:

select en

Since we now know that the Grid is an object that can contain other objects, lets add a child to it, namely a button, since this is the most familiar control in every interface. As I said in the previous lessons, there are more than one ways in which we can add controls to our GUI. The first one is by Drag and Drop. On the left side of Visual Studio, click on Toolbox, then click and hold over Button, then drag that Button over the surface of our window, and release it, like this:

Visual Studio toolbox drag and drop

You will notice three things: our visual design window now contains a Button control, the XAML codes have been slightly modified (a new line was inserted as a child of the Grid), and the selection inside the XAML editor has been changed to indicate that the newly created button is the selected element.

That’s it! You have finished adding a control to the GUI of your program! That’s why I was saying in a previous lesson that it is not mandatory to learn XAML, since we can at the very worst design our GUIs entirely by drag and drop, without writing a single line of XAML (though I do not recommend this, and we will later see why).

Now, let’s talk about a second way of adding a control to our Window. Position the cursor inside the XAML editor, right under the line where the button was created, and type this line:

like this:

xaml manual

When you are done, you will notice a new control has been added to our interface, this time, a text Label. Do not worry yourselves with the codes involved, for the moment, we will briefly explain them in future lessons.

One final thing that should be mentioned. You should pay attention to the small toolbar that separates the Visual Design from the XAML Editor.

xaml interface Visual Studio

There are a few buttons there that let you modify the zoom level of your GUI designer, swap the visual and editor area, and many other features that will help you by speeding up the designing process.

Tags: , ,

Leave a Reply



Follow the white rabbit