WPF basics & amp; lt; 2 & amp; gt ;__ XAML

Source: Internet
Author: User

This topic describes the functions of Extensible Application Markup Language (XAML) and demonstrates how to use XAML to write Windows Presentation Foundation (WPF) applications. This topic describes the XAML implemented by Windows Presentation Foundation (WPF. XAML is a more extensive language concept than Windows Presentation Foundation (WPF.

Declarative languages supported by Flow Control
XAML simplifies the process of creating a UI for the. NET Framework programming model. You can create visible UI elements in declarative XAML markup, and then use a code hidden file (connected by a division class definition and markup) to separate the UI definition from the runtime logic. In XAML, the mixed code and markup functions are very important, because XML itself is declarative and does not really recommend a model for stream control. The XML-based declarative language is very intuitive and allows users (especially those with Web design and technical background) to create various interfaces from prototype to production. Unlike most other Markup languages, XAML directly presents the instantiation of hosted objects. This general design principle simplifies code and debugging access to objects created using XAML.

A XAML file is an XML file that typically uses the. xaml extension.

The following XAML example demonstrates the necessity of creating a small tag button as part of the UI. The created button obtains the default visual representation through the topic style, and obtains the default behavior through its class design.

XAMLOBJECT element
XAML has a set of rules that map object elements to classes or structures, and map attributes to properties or events, and map the XML namespace to the CLR namespace. XAML elements are mapped to the Microsoft. NET type defined in the referenced assembly, and attributes are mapped to members of these types.

The preceding example specifies two object elements: <STACKPANEL> (with an end tag) and <BUTTON> have multiple attributes. The next section describes attributes ). 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.

Set attributes
In XAML, attributes are set by setting attributes on Object elements using various possible syntaxes. The syntax used for a given attribute varies according to the features of the set attribute.

You can add features or features to an object element by setting attribute values. The initial status of the basic object instance of the object element is based on the default constructor behavior. In general, your application will use some other instances, rather than the full default instance of any given object.

Attribute syntax
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.

Attribute element syntax
For certain properties of an object element, the Attribute syntax is impossible because Property is provided) the object or information required by the value cannot be fully expressed as a simple string. In these cases, you can use another syntax, namely the attribute element syntax. 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 (syntactically. 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. The following example demonstrates the same Property set in the previous Attribute syntax example, but this time all the properties of the Button use the Property) element syntax.

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. This element only represents one child element, but there is no inevitable implicit relationship with the TypeName parent level. In XAML, the <type name. property> directly indicates that Property is an attribute of the type name (set by the content of the attribute element), but it is never an element with a similar name (it happens to have a vertex in the name) but completely different.

Property and class inheritance
The Property that appears as the XAML Attribute of a WPF element is generally inherited from the base class. For example, in the previous example, if you want to view class definitions, reflection results, or documents, the Background attribute is not directly declared on the Button class. On the contrary, the Background is inherited from the base Control class.

The class inheritance behavior of the wpf xaml element is another huge deviation from the basic XML interpretation of the tag. Another reason for using class inheritance (especially when the intermediate base class is an abstract class) is that common architecture types (such as DTD or XSD format) are programmed through XML) it is almost impossible to accurately and completely represent the XAML elements and their allowed attribute sets. In addition, "X" in XAML represents "extensible", and scalability damages the integrity of any given representation of "What is used for wpf xaml.

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 often require the attribute 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.

For example, the following example uses Attribute syntax to set the value of the Style Property. The Style Property uses an instance of the Style class, which is a reference type that cannot be specified in the Attribute syntax string by default. However, in this example, Attribute references a specific tag extension StaticResource. When processing the tag extension, it returns a reference to a style that was previously instantiated as a key resource in the resource dictionary.

A resource is only a markup extension usage enabled by WPF or XAML.

Support for Typeconverter attribute values
In the "Property Syntax" section, we mentioned that attribute values must be set using strings. The basic native processing of how a String is converted to another object type or primitive value depends on the String type. However, many WPF types or members of these types extend the basic string attribute processing behavior. Therefore, instances of more complex object types can be specified as attribute values through strings. At the code level, this processing is done by specifying the CLR type converter that processes the string property values. The Thickness structure type commonly used to indicate the size of a rectangle area (such as Margin) is an example of this type: it has all properties that use this type (Property) A special Attribute syntax that supports Type converters is published for use in XAML markup. The following example uses the Attribute syntax that supports the type converter to provide a value for Margin:

Related Article

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.