WPF and Extensible Application Markup Language (XAML)are closely tied to each other, in fact many developers are convinced the XAML is Microsoft’s UI definition language. The truth is more nuanced than that however. This video looks that the history of XAML and shows that XAML is not limited to UI frameworks.
- [Instructor] Back in 2003 Microsoft envisioned a new set of APIs to power Windows. This new version at Windows was known as Longhorn and it consisted of three core ideas called pillars. WinFS was a new XML based file system. Indigo was a new communication protocol. And Avalon was the new display and UI composition layer. Avalon got a new name and in 2006 was released as Windows Presentation Foundation, also known as WPF. At the time of the release, WPF was the most powerful system in the Windows ecosystem for creating desktop applications.
WPF contained many innovations. For example its rendering engine was based on DirectX which allowed it to render more sophisticated interfaces than its predecessor, Windows Forums. Another innovation was using XML to define the UI layer of the desktop application. Microsoft created a special variant of XML for WPF known as Extensible Application Markup Language, also written as XAML. This is usually pronounced as "zamal." One fascinating part of XAML is the way that Microsoft implemented it.
They built it to be a generalized way to represent objects. Stated another way, it's an XML language to create and instantiate .NET objects. Isn't that interesting? The usual explanation found in many articles is that XAML is a UI definition language. Well that's true, in a sense, because it was created in conjunction with WPF and WPF is a UI rendering engine and it's a framework that is optimized for creating desktop applications. But XAML is more flexible than that.
It's not tied exclusively to UI development. In fact, it can work with any object-oriented managed API. There are dialects of XAML that work with .NET objects, and others that work with Windows RunTime objects. It's true that most of the XAML dialects produced by Microsoft are associated with UI frameworks, but that's not a requirement of XAML. Later in this chapter I'll show you all the places where XAML is used by Microsoft. XAML was built to be as direct a mapping from XML to .NET as possible. Look through the XAML specification and you'll find it has ways to map .NET namespaces to XML namespaces.
The XAML spec also has rules that map .NET types to elements. There are other rules in the spec that link .NET properties and events to XML attributes. Here's an example, on the top is some XAML that creates a section of UI in a WPF application. There is a StackPanel element that contains two children, the TextBlock and the CheckBox. The attributes in the XAML file are mapped to properties. There is one setting the Text property, another setting the IsChecked property, and one more, setting the Content property.
In the bottom example, there is the equivalent C# code to instantiate the three UI classes and set their properties. As you go through this course you'll see more details of what the mappings between the .NET types and the XAML markup. The course focuses on the XAML parts and how they work, but naturally there'll be times when the discussion turns to WPF or the other XAML dialects because most XAML implementations are a key part of Microsoft's UI frameworks.
- What is XAML?
- What frameworks use XAML?
- Working with XAML and Visual Studio
- Exploring XAML namespaces
- Instantiating objects
- Subscribing to events
- Using XAML in Windows Presentation Foundations, Universal Windows, and more