Extensible Application Markup Language (XAML)

Extensible Application Markup Language (XAML)

Extensible Application Markup Language is generally speaking case sensitive. Its purpose is to resolve backing types, WPF- XAML is case sensitive as the same rules that the CLR is case sensitive. It’s to create and initialize .NET objects with hierarchical relations, as based on simple language XML. XAML was originally invented for WPF so that it can be used to create any kind of object tree.

XAML is used to create user interfaces in WPF, Silverlight. XAML is mainly used for designing GUIs, however, it can be used for other purposes as well to declare workflow in Workflow Foundation. All classes in WPF have excessive usage of properties and parameters with fewer constructors so that they perfectly fit for XML languages like XAML.

Advantages of XAML

All we can do in XAML can also be done in code. XAML is just other way to create and initialize objects. We can use WPF without using XAML. It’s depend on the developer if he want to declare it in XAML or write it in code. Declare our UI in XAML has some advantages:

  • XAML code is easy to understand because it is shorter.
  • Individual designer code and logic.
  • In Expression Blend for Graphical designing tools it is used as input.
  • To separate the role of designer and developer .

Namespaces

In the beginning of every XAML file you need to include two namespaces.Which are:-

  • First is http://schemas.microsoft.com/winfx/2006/xaml/presentation. It is mapped to all WPF controls in System.Windows.Controls.
  • Second is http://schemas.microsoft.com/winfx/2006/xaml it is mapped to System.Windows.Markup that defines the XAML keywords.

Assembly level is attributed by XmlnsDefinition which is union of an CLR namespace and a XML namespace. By using clr- namespace as a prefix You can also directly include a CLR namespace in XAML by using the clr-namespace: prefix.

<Window xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”>

</Window>

XAML in WPF

WPF feature which not most widely known is XAML and it is also among the most misunderstood features. If you have exposure to WPF, then you must have heard of XAML.

Note – The two less known facts about XAML –

  1. WPF doesn’t need XAML.
  2. XAML doesn’t need WPF.

They are in fact separable pieces of technology.

WPF with XAML –

Example: A button is created with some properties in XAML.

<Window x:Class = “WPFXAMLOverview.MainWindow”
xmlns = “http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x = “http://schemas.microsoft.com/winfx/2006/xaml”
Title = “MainWindow” Height = “360” Width = “600”>
<StackPanel>
<Button x:Name = “button” Content = “Click Me” HorizontalAlignment = “Left”
Margin = “175” VerticalAlignment = “Top” Width = “70” />
</StackPanel>
</Window>
WPF without XAML (Using C#)

In case we not to use XAML in WPF, then we can achieve the same GUI result with procedural language as well.

Example: We will create a button in C# without using XAML.

using System.Windows;
using System.Windows.Controls;

namespace WPFXAMLOverview {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>

public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
// Create the StackPanel
StackPanel stackPanel = new StackPanel();
this.Content = stackPanel;

// Create the Buttons
Button BUTTON = new Button();
button.Content = “Click Me”;
button.HorizontalAlignment = HorizontalAlignment.Left;
button.Margin = new Thickness(175);
button.VerticalAlignment = VerticalAlignment.Bottom;
button.Width = 70;
stackPanel.Children.Add(BUTTON);
}}}

As you can see is the XAML version much shorter and clearer to read. And that’s the power of XAMLs expressiveness.

  • Designing of UI element is as simple as with XAML.
  • Declare the objects in XAML or define them using code.
  • In WPF designing , XAML is optional, but despite this, it is at the heart of WPF design.
  • The aim of XAML is create user interface elements directly.
  • In WPF, XAML aim is to control all visual aspects of the user interface from mark-up.

Leave a comment