XAML in WPF, like C #, has its own independent compiler. XAML is parsed and compiled, eventually forming Microsoft's intermediate language stored in the assembly. In parsing and compiling the language of XAML, we often need to tell the compiler some important information, such as what the compiler results of the XAML code should merge with the C # code, the element master public with the XAML declaration, the private access level, and so on. These tools that allow programmers to communicate with the XAML compiler exist in the X namespace.
There are three types of tools under the X-namespace:
One, attribute type of tool
II. tools for label extension types
III. tools for XAML directive element types
We can see that they are divided into three categories: attribute, Markup extension, and XAML directive element.
attribute is a language-level thing, it's for the XAML compiler, the property is object-oriented, and it's for programming logic. When programming with XAML, if you want to add some special markup to the XAML compiler to influence his parsing, you need to attribute him.
Some of the common attribute
x:class
This attribute primarily tells the XAML compiler that the compiled XAML will be merged with which class.
Use x:class Note: This attribute can only be used for the root node, and the class that points to must use the keyword partial
x:ClassModifier
This attribute primarily tells the XAML compiler what level of access control is generated for a class with tag compilation.
x:name
This is the same as the ID in WinForm.
x:FieldModifier
This is used to change the reference variable access level in XAML.
x:key
The most natural way to search is to use the form of "key-value" pairs. In a XAML file, we can extract a lot of content that needs to be used more than once in a resource dictionary and use that key to retrieve it when needed.
x:shared
When we learned to use x:key, we already knew that once we put some objects into a resource dictionary, we could retrieve them and reuse them, so that whenever they retrieved an object, would we get the same object, or would it be multiple copies of the object? This depends on how the x:shared is set, the default is true, and we get the same object. If it is false, then we get a new copy of the object.
X:type
As its name implies, the value of X:type is the name of a data type. The data type itself is also used in programming.
X:null
In the C # language, we use the NULL keyword to represent null values, and in XAML we use x:null to represent null values.
X:array
The function of x:array is to expose a known ArrayList instance to the consumer through its Items property, and the ArrayList member type is indicated by the X:array type.
x:static
Using static members of a data type in a XAML document
X:code
X:code can put logic code in XAML to run
x:XData
That's the data source, and the content within that tag can be used as a data source.
This is everything in the X space.
Let's introduce them separately!
Attribue in the X name space
Mention attribute, do not think of me when learning programming, the concept of attributes, in English technical articles exist in the attribute and property of the two concepts, in Chinese translation can be translated as "attributes." Since they are all "attributes", what is the difference between them? By looking at the data, there is an explanation: attribute and property are two levels of things, attribute is the language level of things, is to the compiler to see, and the property is object-oriented level of things, is for programming logic use. The more appropriate translation for both is that Atrribute is translated as "feature" and the property is translated as attribute. Well, let's take a look at the attribute in the X namespace.
1, x:class The role of this attribute is to tell the XAML compiler to merge the compiled results of the xmal tag with the classes specified in the background code. Some requirements must be followed to use the attribute:
- The Atrribute can only be used in the root node of XAML
- The type of root node using x:class is consistent with the type indicated by the value of x:class
- The type indicated by the value of x:class must use the partial keyword when declaring
The sample code is as follows:
XAML code for the foreground:
<window x:class= "Wpfapplication3.window9"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Title= "Window9" height= "width=" >
...........................................................
The CS code in the background:
Public partial class Window9:window
{
public Window9 ()
{
InitializeComponent ();
list<student> students = new list< Student> () {
New Student () {id=1,name= "Tim", age=21},
New Student () {id=2,name= "Tom", age=22},
New Student () {id=3,name= "Jim", age=23}
};
This.listBox1.ItemsSource = students;
This.listBox1.DisplayMemberPath = "Name";
Binding bind = new binding ("Selecteditem.name") {Source = This.listbox1};
This.textBox1.SetBinding (Textbox.textproperty, bind);
}
}
2, x:classmodifier the function of this attribute is to tell the XAML compiler what kind of access modifiers the generated classes are decorated with. It is important to note that when using the attribute:
- Labels that use the Attribute must have x:class Attribute
- The value of x:ClassModifier must be consistent with the access modifier of the type indicated by the value of x:class
For example, if you use x:classmodifier= "public" in the window tag in XAML code, you must first have a attribute-value such as x:class= "" in the tag. The class of CS code corresponding to XAML must also be public.
3, x:name about this attribtue, I give it a definition of the equivalent of a person's name, used to distinguish. Give such an example! If the real life, a teacher to a class for the first time to class, at this time she does not have the information of students, so you can separate out each classmate? If the teacher has the information of the students, then the teacher can according to read the name to find the corresponding classmate, that x:name has such a role!
The role of x:name is two:
- Tells the XAML compiler that when a tag has a x:name property, it declares a reference variable for that instance, in addition to generating the corresponding instance for the tag, and the variable name is the value of x:name
- If the x:name label has a property of name, set the value of the Name property to the value of x:name and register the value in the UI tree for easy lookup purposes
4, x:fieldmodifier about this attribute, is used to set the reference variable of the access modifier. However, you must use the x:FieldModifier property in a label that uses x:name. Because x:fieldmodifier corresponds to a value that sets the access modifier for the reference variable generated by x:name.
5, x:key in any XAML we can define resource, we can extract some content placed in resource, of course, each kind of content must have their own unique identity, that x:key is this role, in the use of certain content, we can be based on x:key To retrieve the corresponding resource.
6, x:shared in the use of attribute x:key retrieve objects, will get an object, if the retrieved object is not used x:shared tag, then the first time to retrieve an object, when the second use can only get the first time to retrieve a copy of the object. Constant use of the same retrieval object, in-memory replicas will increase, but also increase the memory consumption. If the x:shared= "true" label is used in the retrieved object, the same object is used regardless of the object that was retrieved multiple times. This reduces the memory consumption!
Two, the markup extension in the X namespace
Markup extensions (Markup Extension) are actually derived classes that are directly or indirectly markupextension classes that exist in the X namespace, so they are often referred to as markup extensions within the X namespace.
Let's take a look at common markup extensions!
1, X:type literally open, the value of x:type should be a data type name. In general, we operate in programming with instances of data types or references to instances, but sometimes we use the data type itself. Let's look at an example here!
First create a derived class for the button:
Class Mybutton:button
{
Public Type Userwindowtype {get; set;}
protected override void OnClick (
{
Base. OnClick ();
Window win = Activator.CreateInstance (this. Userwindowtype) as Window;
if (win! = null)
Win. ShowDialog ();
}
There is a type of property in the class, that is, Userwindowtype, at which point you need to assign a data type as a value. The OnClick method of the parent class is also overridden in the class, at which point the click Time can be activated as the parent class, and an instance of the type stored by the Userwindowtype is created.
<window x:class= "Wpfapplication.window1"
Xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
Xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Title= "Window1" width= "height=" >
<stackpanel background= "LightBlue" >
<textbox/>
<textbox/>
<button content= "Confirm"/>
</StackPanel>
</Window>
At this point, we add the custom button to the window and copy the WINDOW1 as the data type to the Userwindowtype property of the custom button.
<window x:class= "Wpfapplication.window2"
Xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
Xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Title= "Window2" width= "height=" >
<StackPanel>
<local:mybutton content= "Show" userwindowtype= "{x:type Typename=local:window1}"/>
</StackPanel>
</Window>
This is the use of x:type!
2, x:null the role of the markup extension is to assign a null value to an attribute. We know that NULL is represented in C # with nulls, so null values are represented in XAML with X:null. Let's also look at an example:
<Window.Resource>
<style targettype= "button" >
<setter property= "Background" value= "LightBlue"/>
</Style>
</Window.Resource>
<StackPanel>
<button content= "Button1"/>
<button content= "Button2"/>
<button content= "Button3"/>
<button content= "Button4" style= "{x:null}"/>
</StackPanel>
The result of the above code is that the background color of the first three buttons is light blue, and the last button is the default color. Know the usage of x:null!
3, x:array the function of the attribute is to report a type-known ArrayList instance to the consumer through its Items property, and the type of the member in the ArrayList is indicated by a x:array. Here's an example to illustrate it!
<ListBox>
<ListBox.ItemsSource>
<x:array type= "Sys:string" >
<sys:String>1</sys:String>
<sys:String>2</sys:String>
<sys:String>3</sys:String>
</x:Array>
</ListBox.ItemsSource>
</ListBox>
Run the program and you'll know the effect!
4, x:static is a very common markup extension, its main function is to use the Static members of the data type. Mainly used in the internationalization of the program support. Also use examples to speak:
public class Chinese
{
public static string btnsure = "ack";
}
public class 中文版
{
public static string btnsure = "OK";
}
Depending on the language you choose, the language in which the content is presented differs
<button content= "{x:static ocal:Chinese.btnSure}"
Third, XAML directive element
X:code is primarily used to write C # code in XAML
WPF Foundation----namespaces