[XAML learning materials] XAML Overview 1

Source: Internet
Author: User

Note: many of the documents in this article are from msdn. This document is only for the Notes left by the author for learning XAML, not for my personal writing !! May be Excerpted from various websites and documents. If you have any offense, please point out that I will handle it as soon as possible !!

XAML object element

XAML has a set of rules.Map object elements to classes or structures,Map attributes to properties or events,And map the XML namespace to the CLR namespace. XAML element ing is referencedProgramMicrosoft. NET types defined in the Set, and attributes are mapped to members of these types.

1 < Stackpanel >
2 < Button Content = "Click me" />
3 </ Stackpanel >

The preceding example specifies two object elements: <stackpanel> (with an end tag) and <button/>. The stackpanel and button strings are mapped to the names of a class defined by WPF and part of the WPF assembly. When marking an object element, you can create an instruction for the XAML processing to create a new instance of the specified class when loading the XAML page.Each instance is created by calling the default constructor of the base class or structure and storing the results.. To be available as an object element in XAML,This class or structure must expose a common default (No parameter) constructor.

Summary: Each element corresponds to a WPF class, and the attributes of each element correspond to the attributes of the class. Of course, the event should also be the same. To render a class in XAML, you must have a default constructor without parameters, because each instance is created by calling the default constructor of the basic class and structure.

 Set attributes

Every. Net developer is no stranger to ASP. NET within my sight. Of course, it is no stranger to the markup language. Of course, there is no difference between setting attributes in XAML and other Markup languages. In XAML, attributes can often be expressed as attributes ). Attribute syntax is the simplest Property setting syntax and will become the most intuitive syntax for developers who used markup language in the past. For example, the following tag creates a button with a red text and a blue background, and a display text specified as content.

<ButtonBackground= "Blue"Foreground= "Red"Content= "This is a button"/>

For some attributes, it is difficult to directly use the attribute format. For example, some attribute values cannot be expressed in simple strings. In this way, a more powerful representation is required, that is, the attribute element notation.Attribute element syntax uses the marked content to set attributes that Contain element references. Generally, the content is an object of the attribute value type (the value setting instance is usually designated as another object element ).The syntax of the attribute element is <type name. Attribute>. After the content is specified, an end tag must be used to end the attribute element, just like any other element (Syntax: </type name. Attribute>. For attributes that support both attribute and property element syntax, although the nuances of these two syntax (such as blank processing) are slightly different, but their results are usually the same. If attribute syntax can be used, attribute syntax is generally more convenient and can be used to implement more streamlined tags, but this is just a style problem, it is not a technical limitation.

Note: The property element Syntax of XAML represents a huge deviation from the basic XML interpretation of the tag. For XML, <type name. Attribute> represents another element, which only represents one child elementTypenameThere is no inevitable implicit relationship between the parent level. In XAML, the <type name.Property> Direct representationPropertyIt is an attribute of a type name (set by the content of the attribute element), but it is never an element with a similar name (a point in the name happens) but completely different.

Reference Value and tag Extension

Tag extension is a XAML concept. In the attribute syntax, curly braces ({And}) indicate markup extension usage. This usage indicates that the XAML processing should not treat the attribute value as a string or directly convert the value to a text string as usual. When attributes use reference type values, these attributes are often requiredAttribute element syntax (always create a new instance) or reference by marking extended objects. Tag extension usage may return existing instances, so it can be more diversified or produce less Object System overhead.

When tag extension is used to provide attribute values, the attribute values should be provided by logic in the associated tag extension's backup class. The most common markup extension in WPF Application Programming is binding (used for data binding expressions) and resource reference staticresource and dynamicresource. By using tag extension, you can use Attribute syntax to provide reference values for properties even if property does not support attribute syntax for direct Object Instantiation; or make the specific behavior conform to the general behavior requirements that the property type value must be filled with the XAML property.

Summary: When the attribute value needs to reference an object, use the tag extension method, and "{}" is the syntax of this class, mark the application of the extension object in.

I haven't written it for a long timeArticleToday, I suddenly want to learn about WPF. I found that the syntax of XAML is similar to that of HTML. I used WPF to replace windowform when developing a small program, however, it is found that the style of each attribute may be quite large if it is used. When looking for style usage, I found that I really want to learn about the syntax basics of XAML and WPF, so I want to record some articles. This article is from the msdn document. Where can you view it? Here is just a note!

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.