WPF Guide II (XAML overview)

Source: Internet
Author: User

In the first few days of reading the WPF document, I came up with a wrong idea: Isn't WPF a XAML code? At that time, I felt depressed. I had to get this thing when I learned about WPF. The first thing we feel is that WPF is very complicated. Although you are not familiar with and understand WPF much, it is indeed a wrong idea.

Mr. Charles Petzold once wrote an article about The relationship between WPF and XAML (The Two APIs ). The article explains why WPF is complicated: Since WPF has two sets of APIS, one for common encoding access (such as C #, VB. NET, and other languages supported by. NET. The other is the XML-based API, called the Extensible Application Markup Language ).

XAML separates the UI code from the logic code of the application. In. NET 3.0 and Windows Vista, XAML and WPF build the entire UI together. Since XAML is based on XML, every XAML code must be a complete XML file. XAML inherits all XML definitions and rules. The difference between XAML and other XML extensions is what it means. Each XAML element is a. net clr class. XML makes it easy for us to extend and operate XAML. Using the relationship of WPF in XAML, developers can independently design beautiful UIS, and perhaps more beautiful unions will emerge. We can write the program logic in a separate file or embed it in an XML file inline.

 

There should be three XML functions that are most used in XAML: namespaces, attributes, and child elements.

 

Let's take a look at a simple XAML example:

<Window x: Class = "FirstXAML. Window1"

Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"

Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"

Title = "FirstXAML" Height = "200" Width = "300"

>

<Canvas>

</Canvas>

</Window>

Xmlns is the namespace in XML. In W3C, xmlns is defined as follows:

XML namespaces provide a simple method for qualifying element and attribute names used in Extensible Markup Language documents by associating them with namespaces identified by URI references.

In short, xmlns provides a method to define the namespace referenced by URI as the default namespace for the elements and attributes of the current XML file. The current XML file is our XAML file. Its default namespace is http://schemas.microsoft.com/winfx/2006/xaml/presentation.

Then there are attributes and child elements. In addition to Property, XML can also be used to represent attributes. This is also true in XAML. Let's look at a simple example:

<Button Width = "6">

<Button. Background> White </Button. Background>

</Button>

In this example, attributes and child elements are used to specify attributes. The Width is directly represented by attributes, and the Background attribute is represented by child elements. In most cases, but not all, you can choose either of the two representation methods.

XAML is compiled into a BAML (Binary Application Markup Language) file. Generally, BAML files are smaller than XAML, And the compiled BAML files are Pre-tokenized, so that you can load and analyze the XAML more quickly during running. These BAML files are embedded into the Assembly as resources. At the same time, generate the corresponding code (the file name is **. g. cs or **. g. vb). These codes generate the named Attribute fields based on The XAML element. And the constructor that loads BAML.

Finally, I have attached some translated terms about the advantages of XAML, which may be more intuitive:

In addition to the advantages of markup language and XML, XAML also has the following advantages:

Designing the UI with XAML is simpler

XAML requires less encoding than other uidesign techniques.

The UI designed by XAML facilitates transfer and submission in other environments. For example, in Web or Windows Client.

It is very easy to design a dynamic UI with XAML

XAML brings a new revolution to the uidesigner. Now, all designers no longer need the. NET development knowledge to design the UI. In the near future, end users will be able to see more beautiful UIS.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.