Basic XAML syntax terminology tutorial (3)

Source: Internet
Author: User
Tags first string

Use attribute elements and object elements together in an object with content attributes

The XAML specification declares that the XAML processor can enforce the following: the object elements used to fill in the XAML content attributes of an object element must be continuous and cannot be used together. This restriction on mixed use of attribute elements and content is enforced by the wpf xaml processor.

The sub-object element can be used as the first direct tag in an object element, and then an attribute element can be introduced. You can also specify one or more attribute elements, specify the content, and then specify multiple attribute elements. However, once the content is followed by an attribute element, you cannot introduce any further content, but can only introduce other attribute elements.

This content/attribute element sequence requirement is not applicable to internal text used for content. However, this is still a good continuous label style for internal text, because if the attribute elements are staggered with the internal text, it is difficult to intuitively detect a large number of white spaces in the mark.

Additional attributes

Additional attributes are a programming concept introduced in XAML. In this way, attributes can be owned and defined by types, but can be set on any element. The main scheme for attaching attributes is to allow the child elements in the element tree to report information to the parent element, rather than using an object model that is widely shared among all elements. On the contrary, additional attributes can be used by any parent element to report information to child elements.

The syntax used for additional properties is similar to the attribute element syntax on the surface, because you also need to specify a combination of type names and attribute names. There are two important differences:

You can use the type name and attribute name combination even when you use the property syntax to set additional properties. Only property is required to use the property name in attribute syntax.

You can also use the attribute element syntax for additional attributes. However, for the typical attribute element syntax, the type name you specify is an object element containing the attribute element. If you reference an additional Property, the type name is used to define the class for the additional property, rather than containing the object element.

Additional event

Additional events are another programming concept introduced in XAML. events can be defined by types, but handlers can be appended to any object. The type used to define additional events is usually used to define the static type of the service. These additional events are sometimes made public by the route event alias in the type used to publish the service. The handler of the additional event is specified through the attribute syntax. Just like attaching an event, you can extend the property syntax of the additional event to allow the use of type names. event name. The type name is the class that provides the add and remove event handler accessors for the additional event infrastructure, and the event name is the event name.

XML namespace

None of the preceding syntax examples specify namespaces other than the default namespace. In a typical WPF application, the default namespace is specified as the WPF namespace. You can specify a namespace other than the default namespace and still use similar syntaxes. However, if you name a class that cannot be accessed in the default namespace, the name of this class must start with the prefix of the XML namespace used to map the corresponding CLR namespace. For example, it is an object element syntax used to instantiate an instance of the myelement class, which contains the CLR namespace of the class (and possibly the external Assembly containing the namespace) previously mapped to the custom prefix.

Tag Extension

XAML defines a markup extension programming entity that allows the XAML processor to escape from the general processing of properties or object elements and transfer the processing to the Support class. WPF's implementation of the XAML processor uses the markupextension abstract class as the basis for all Markup extensions supported by WPF. When using the attribute syntax, the markup extension character used to identify the XAML processor is a left braces ({), followed by any character other than the right braces. The first string after the left braces must reference a class that provides specific extended behavior. If the substring "extension" is part of the actual class name, the reference can omit this substring. A space may appear after this class. Each character after this space can be used as an input by the implemented extension until the right braces are encountered. When using attribute syntax, the main purpose of marking extensions in WPF is to provide a method to reference other existing objects, or to forward references to objects that will be calculated at runtime. For example, you can specify the {binding} tag extension to replace the value type that is usually used for a given attribute to complete simple data binding. For attributes that cannot use Attribute syntax in other ways, many tag extensions allow the use of attribute syntax. For example, a style object is a relatively complex reference type, which contains several other attributes. Each attribute also uses a byref object (rather than a primitive ). However, a style is usually created as a resource and will be referenced by one of the two tag extensions of the requested resource. This extension delays the calculation of the property value to the resource search. It allows you to provide the value of the style property in the attribute syntax and adopt the style type, as follows:

My button

Here, staticresource is used to identify the staticresourceextension class, which provides the tag extension implementation. The next string mystyle is used as the input of a non-default staticresourceextension constructor. In this constructor, parameters extracted from the extended string are used to declare the requested resourcekey. Mystyle should be the X: Key Attribute Value of the style defined as a resource. Staticresource tag extension usage requires that this resource be used to provide the style attribute value through the static resource search logic during loading.

Optional and not recommended XAML usage

Optional use of attribute Elements

The optional usage of attribute elements includes "spell out", which is considered as an implicit element content attribute by The XAML processor. For example, when you declare the content of the menu, you can explicitly declare the items set of the menu as the attribute element tag and place each menuitem in, instead of using implicit XAML processor behavior (that is, all sub-elements of the menu must be menuitem and placed in the items set ). Sometimes, this optional usage can help clarify the object structure represented in the tag visually. Alternatively, implicit usage of attribute elements can sometimes avoid marking that is technically functional but visually confusing (for example, nested tag extension in attribute values.

Typename. membername fully qualified attributes

Type name of the attribute. the member name format is actually more common than simply using routing events. However, in other applications, this format is redundant if it is only for the purpose of label style and readability, you should avoid using it. In the following example, the three references to the background attribute are completely equivalent:

The reason why button. Background applies is that the search for this attribute on the button is successful (the background is inherited from control), and the button is the class of the object element or the base class. Control. Background applies because the control class actually defines background and control is a button base class.

However, the following type name. member name format example is not applicable, so it is displayed as commented out:

Label is another derived class of control. If label. background is specified in the label object element, this usage applies. However, because the label is not a button class or a base class, the specified XAML processor behavior then processes label. background in the form of additional properties. Label. background is not an additional property, so this usage will fail.

Basetypename. membername attribute Element

Similar to how the type name. member name format applies to the attribute syntax, the base type name. member name syntax applies to the attribute element syntax. For example, the following syntax applies:

Here, even if an attribute element is contained in a button, the attribute element is provided in the form of control. background.

However, just as the type name of the attribute. The format of the member name is the same as that of the base type name. The member name is a bad style in the tag, so you should avoid using it to set the style.

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.