[Top] WindowsPhone XAML syntax explanation

Source: Internet
Author: User

++ ++

This article is original on this site. You are welcome to repost it! Reprinted please indicate the source:

Http://blog.csdn.net/mr_raptor/article/details/7227260

++ ++

 

XAMLIs a declarative markup language. XAML simplifies the process of creating a UI for. NET Framework applications. 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. XAML directly uses a set of specific backup types defined in the Assembly to indicate Object Instantiation. XAML implements a workflow through which different Parties can use different tools to process the UI and logic of the application.

Follow Original: http://blog.csdn.net/mr_raptor/article/details/7227260

In text format, a XAML file is an XML file with a. xaml extension. The file can be encoded by any XML encoding, but is typically encoded as a UTF-8.

Features:

1. XML files with. xaml extensions are in the same format as XML files.

2. elements defined in XAML are actually type objects defined in a set of programs.

3. Each. xaml file in a WP7 application has a hidden code file (XAML file name +. cs)

 

Follow Original: http://blog.csdn.net/mr_raptor/article/details/7227260

XAML files and code hidden files

Since the XAML file is only used to represent the UI, the control logic corresponding to the UI is usually the responsibility of the corresponding hidden code file. In the WP7 application. all the xaml files have a hidden code file (XAML file name +. cs), for example, MainPage. the hidden file name of the code corresponding to xaml is MainPage. xaml. cs. The two are connected using the x: Class prefix in the root element of the XAML file. After the control on the UI generates an event, the file namespace and Class name are hidden through the Code specified in the x: Class prefix, And the UI processing logic is found, to process the event (of course, you must specify the event processing method in the event attribute of the control, for example, the following code:

In the root element x: Class, specify the namespace and Class name MyTes. mainPage: Specifies the Button processing method Btn_Click in the Button element. When you press the Button, the automatic callback Code hides the file MainPage. xaml. the Btn_Click event processing method in cs.

MainPage. xaml File

<Phone: PhoneApplicationPage <br/> x: Class = "MyTest. mainPage "<br/>...> <br/> <Button Click = "Btn_Click"/> <br/> </phone: PhoneApplicationPage> <br/>

MainPage. xaml. cs code hidden file

Namespace MyTest <br/>{< br/> public partial class MainPage: PhoneApplicationPage <br/>{</p> <p> private void Btn_Click (object sender, RoutedEventArgs e) <br/> {<br/> .... <br/>}< br/>

Follow Original: http://blog.csdn.net/mr_raptor/article/details/7227260

Attribute syntax

The attributes of an object can be expressed as the characteristics of an object element. The attribute syntax name is the attribute set in the attribute syntax, followed by the value assignment operator (= ). The attribute value is always specified as a string contained in quotation marks.

Attribute syntax is the simplest and most effective attribute setting syntax. It is the most intuitive syntax for developers who have used Markup languages. For example, the following tag creates a button with a red text and a blue background, and a display text specified as Content.

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

Follow Original: http://blog.csdn.net/mr_raptor/article/details/7227260

Attribute object element syntax

Attribute syntax is impossible to implement certain attributes of an object element, because it cannot fully express the objects or information necessary to provide the attribute value within the quotation marks and string restrictions of the attribute syntax. In these cases, you can use another syntax, namely the attribute object element syntax.

The syntax for starting marking attribute element objects is <Type name.Attribute name>. Generally, the content of the tag is an object element of the type, and the attribute uses this element as its value. After the content is specified, an end tag must be used to end the attribute element. The end tag syntax is </Type name.Attribute name>.

<Button> <br/> <Button. background> <br/> <SolidColorBrush Color = "Blue"/> <br/> </Button. background> <br/> <Button. foreground> <br/> <SolidColorBrush Color = "Red"/> <br/> </Button. foreground> <br/> <Button. content> <br/> This is a button <br/> </Button. content> <br/> </Button> <br/>

Follow Original: http://blog.csdn.net/mr_raptor/article/details/7227260

Content Attributes

If the content attribute of a class is the only content attribute in the class, the child element of the object element can be used to set the value of the content attribute. In other words, for content attributes only, you can omit the attribute element when setting this attribute in the XAML tag and generate a more intuitive parent/child form in the tag.

<Border> <br/> <TextBox Width = "300"/> <br/> </Border> <br/>

 

Of course, you can also set the content attribute of an element as a child element.

<Border> <br/> <Border. child> <br/> <TextBox Width = "300"/> <br/> </Border. child> <br/> </Border> <br/>

 

Follow Original: http://blog.csdn.net/mr_raptor/article/details/7227260

Set syntax

The XAML language contains some optimizations to generate more readable markup. One of the optimizations is that if a specific property uses a set type, the items that declare as the child elements in the value of the property in the tag will become part of the set. In this case, the set of sub-object elements is set as the value of the set attribute.

The following example demonstrates the set syntax for setting values for the GradientStops attribute:

<LinearGradientBrush> </p> <LinearGradientBrush. gradientStops> </p> <GradientStop Offset = "0.0" Color = "Red"/> </p> <GradientStop Offset = "1.0" Color =" blue "/> </p> </LinearGradientBrush. gradientStops> </p> </LinearGradientBrush> <br/>

Follow Original: http://blog.csdn.net/mr_raptor/article/details/7227260

XAML root element and XAML namespace

A XAML file can only have one root element, so that it can be a correctly formatted XML file and a valid XAML file at the same time. For example, each WindowsPhone application has only one <phone: PhoneApplicationPage> </phone: PhoneApplicationPage> root element.

The root element also contains the xmlns, xmlns: x, and xmlns: phone features, as shown in the following table.

Element name

Meaning

<Phone: PhoneApplicationPage

Start object element of the root element

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

Default XAML namespace

Xmlns: phone = "clr-namespace: Microsoft. Phone. Controls;

Assembly = Microsoft. Phone"

The namespace phone references the namespace in the Assembly Microsoft. Phone.

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

XAML namespace

X: Class = "ExampleNamespace. ExampleCode"

A partial class declaration that connects to a hidden code file that is defined as a partial class

>

The end of the root object element. The object is not terminated because the element contains child elements.

 

These features indicate to the XAML processor which XAML namespaces contain type definitions that mark the backup types to be referenced as elements. The xmlns feature explicitly indicates the default XAML namespace. In the default XAML namespace, object elements in the tag can be specified without the prefix. In most WP7 application solutions, the default XAML namespace is mapped to the WPF namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation. Xmlns: x indicates another XAML namespace, which maps to the XAML language namespace http://schemas.microsoft.com/winfx/2006/xaml.

The use of xmlns to define usage range and name range ing is in line with the XML 1.0 specification. The differences between the XAML name range and the XML name range are that the XAML name range also contains information about how type-supported elements of the name range during type parsing and Analysis of XAML.

Note that the xmlns feature is absolutely required only on the root element of each XAML file. The xmlns definition applies to all child elements of the root element (this behavior also complies with the XML 1.0 specification of xmlns .) At the same time, other elements below the root can have the xmlns feature, which will apply to any child element that defines the element. However, frequent or redefinition of the XAML namespace may make the XAML tag style hard to read.

The implementation of its XAML processor includes the infrastructure that can recognize the core assembly. Therefore, to reference the XAML elements from the WP7 assembly, you only need to declare the default XAML namespace as the default xmlns.

X: prefix

In the preceding root element example, the prefix x is used to map the XAML namespace http://schemas.microsoft.com/winfx/2006/xaml. this namespace is a specialized XAML namespace that supports the construction of The XAML language. In the entire SDK Project template, example, and documentation, the x: prefix is used to map the XAML namespace. The XAML namespace Of The XAML language contains multiple programming structures that will be frequently used in The XAML. The most common x: prefix programming structure is listed below:

  • X: Key: Set a unique Key for each resource in ResourceDictionary. In all x: usage marked by a typical WP7 application, x: Key may account for 90%.
  • X: Class: Provides"Classes hidden by code"Specify the CLR namespace and class name. That is to say, each XAML file is followed by a corresponding code hidden class (in fact, it is the corresponding XAML file name +. cs ). You must have such a class to support code hiding for each WP7 programming model. Because of this, even if there is no resource, you can almost always see the ing x :.
  • X: Name: After the XAML processor processes all object elements, specify the runtime object Name for the instances in the XAML code. Generally, you will often use the equivalent property defined by WP7 for x: Name. This type of property is mapped to the CLR reserve property, so it is easier for application programming. In application programming, you often use the runtime code to find the name element from the initialized XAML. The most common attribute is FrameworkElement. Name.
  • X: Static: enables a reference to return a Static value. This Static value can only be a XAML compatible attribute.
  • X: Type: Construct a Type reference based on the Type name. It is used to specify the Type (for example, Style. TargetType) feature, but attributes often have the function of converting the local string to Type. Therefore, it is optional to use the x: Type flag for extended usage.

 

Custom prefixes and types in XAML

For custom assembly or assembly other than the WP7 core of PresentationCore, PresentationFramework, and WindowsBase, you can specify this Assembly as customXmlnsYou can reference the types in the Assembly in XAML.

The following is a basic example of how a custom prefix works in a XAML tag. The prefix custom is defined in the root element tag and mapped to a specific Assembly packaged with the application. This Assembly contains the NumericUpDown type. By using this prefix, you can declare the Instance Object elements of the NumericUpDown control. When the XAML analyzer resolves to the current element, it uses the namespace specified in the M prefix declaration, you can find the XAML namespace that contains this type to find the location of the Assembly that contains the custom type.

<Phone: PhoneApplicationPage

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

Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"Xmlns: custom = "clr-namespace: numericupdowndownmcontrol; assembly = CustomLibrary"

>

<StackPanel Name = "LayoutRoot">

<Custom: NumericUpDown Name = "numericCtrl1" Width = "100" Height = "60"/>

... </StackPanel>

</Phone: PhoneApplicationPage>

 

 

 

++ ++

This article is original on this site. You are welcome to repost it! Reprinted please indicate the source:

Http://blog.csdn.net/mr_raptor/article/details/7227260

++ ++

 

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.