Understanding XAML

In 2006, Microsoft released WPF, which was the first technology to use XAML. It is used even today to create desktop applications.

Note

Microsoft Visual Studio 2017 is a WPF application.

XAML can be used to create:

  • UWP apps for Windows 10, Windows 10 Mobile, Xbox One, and Microsoft HoloLens
  • Windows Store apps for Windows 8 and 8.1
  • Windows Presentation Foundation (WPF) applications for the Windows desktop, including Windows 7 and later
  • Silverlight applications for web browsers, Windows Phone, and desktop

Note

Although Silverlight is still supported by Microsoft, it is not being actively developed, so it should be avoided.

Simplifying code using XAML

XAML simplifies C# code, especially when building a user interface.

Imagine that you need two or more buttons laid out horizontally to create a toolbar. In C#, you would write this code:

    var toolbar = new StackPanel(); 
    toolbar.Orientation = Orientation.Horizontal; 
    var newButton = new Button(); 
    newButton.Content = "New"; 
    newButton.Background = new SolidColorBrush(Colors.Pink); 
    toolbar.Children.Add(newButton); 
    var openButton = new Button(); 
    openButton.Content = "Open"; 
    openButton.Background = new SolidColorBrush(Colors.Pink); 
    toolbar.Children.Add(openButton); 

In XAML, this would be simplified to the following lines of code. When this XAML is processed, the equivalent properties are set, and methods are called to achieve the same goal as the preceding C# code:

    <StackPanel Name="toolbar" Orientation="Horizontal"> 
      <Button Name="newButton" Background="Pink">New</Button> 
      <Button Name="OpenButton" Background="Pink">Open</Button> 
    </StackPanel> 

XAML is an alternative (better) way of declaring and instantiating .NET types.

Choosing common controls

There are lots of predefined controls that you can choose from for common user interface scenarios. Almost all versions of XAML support these controls.

Control(s)

Description

Button, Menu, Toolbar

Executing actions

CheckBox, RadioButton

Choosing options

Calendar, DatePicker

Choosing dates

ComboBox, ListBox, ListView, TreeView

Choosing items from lists and hierarchical trees

Canvas, DockPanel, Grid, StackPanel, WrapPanel

Layout containers that affect their children in different ways

Label, TextBlock

Displaying read-only text

RichTextBox, TextBox

Editing text

Image, MediaElement

Embedding images, videos, and audio files

DataGrid

Viewing and editing bound data

Scrollbar, Slider, StatusBar

Miscellaneous user interface elements

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.246.148