++ ++
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
UI and logic.
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 x:Class="MyTest.MainPage"...><Button Click="Btn_Click" /></phone:PhoneApplicationPage>
Mainpage. XAML. CS code hidden file
namespace MyTest{ public partial class MainPage : PhoneApplicationPage { private void Btn_Click (object sender, RoutedEventArgs e) { .... }}
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> <Button.Background> <SolidColorBrush Color="Blue"/> </Button.Background> <Button.Foreground> <SolidColorBrush Color="Red"/> </Button.Foreground> <Button.Content> This is a button </Button.Content></Button>
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> <TextBox Width="300"/></Border>
Of course, you can also set the content attribute of an element as a child element.
<Border> <Border.Child> <TextBox Width="300"/> </Border.Child></Border>
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> <LinearGradientBrush.GradientStops> <GradientStop Offset="0.0" Color="Red" /> <GradientStop Offset="1.0" Color="Blue" /> </LinearGradientBrush.GradientStops></LinearGradientBrush>
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 is mapped
The XAML language namespace is 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 occupy
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 each
The code of the WP7 programming model is hidden. Because of this, even if there is no resource, you can almost always see the X ing :.
- 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 use X: Name frequently
The equivalent attribute defined by WP7. 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
++ ++