What is XAML
The shorthand for XAML (extensible Application Markup lanaguage, pronounced "Zammel") is for instantiation. NET object's markup language. Although XAML is a technique that can be used in many different problem areas, its primary role is to construct a WPF user interface. In other words, a XAML document defines the layout of the panels, buttons, and various controls that make up a window in a WPF application.
WPF problems with the previous graphical user design interface
1. Each graphic element (background and button, etc.) needs to be exported as a separate bitmap. This limits the ability to combine bitmaps and the ability to use dynamic effects, such as anti-aliasing, transparency, and shadow effects.
2. Quite a lot of the user interface logic requires the developer to embed in the code, and the graphic designer has no control over any of the details.
3. There is no inherent connection between different graphic elements, often using mismatched image collections.
4. The quality will inevitably be lost when resizing the graphics. The bitmap-based interface relies on resolution, which means it cannot accommodate large displays and high-resolution display settings.
XAML variants
We use XAML to represent the entire XAML language, which is a generic XML-based syntax that is specifically used to represent one. NET object tree, which includes the following collections:
1. WPF XAML contains content elements that describe WPF.
2. XPS XAML is part of the WPF XAML, which defines an XML representation of a formatted electronic document.
3. Silverlight XML is a WPF XAML subset for Microsoft Silverlight applications.
4. WF XAML contains elements that describe the content of WF (work flow, workflow).
XAML compiling
WPF uses BAML (binary application Markup lanaguage, Binary Application Markup Language) to overcome this disadvantage. BAML is not a new thing, it is actually a binary representation of XAML, all XAML is converted to BAML, and these baml are embedded in the final DLL or EXE assembly as resources. Most programmers do not have to consider the conversion of XAML to BAML, because the compiler will perform this work in the background. But you can also use an unedited XAML, which might make sense for situations where you need to provide some immediate user interface (such as extracting content from a database as a piece of XAML tags).
XAML Standard
1. Each element in a XAML document is mapped to an instance of a. NET class, and the name of the element corresponds exactly to the class name.
2. As with all XML documents, another element can be nested within one element.
3. Attributes (property) for each class can be set through attributes (Attribute), in which case the attribute is not sufficient to complete both the use of nested tags (tag) through special syntax.
Namespace for XAML
The xmlns attribute is a special feature in XML that is specifically used to declare namespaces, which appear in all WPF XAML that you create.
1. xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" is the WPF core namespace. It contains all of the WPF classes, including the controls used to build the user interface.
2. xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" is a XAML namespace. It includes a variety of XAML practical features that can affect how documents are interpreted. The namespace is mapped to the prefix x. This means that you can use this namespace (for example,:<x:elementname>) by placing the namespace prefix x before the element name.
"x "prefix/xaml language XAML Common structures for namespaces:
X:key: Sets a unique user-defined key for each resource in XAML ResourceDictionary.
x:class: Specifies the code namespace and code name for the XAML page that provides the code-behind class. For example <window x:class= "Windowapplication1.window1" >.
x:name: After the object elements defined in XAML are processed, specify a runtime object name for the objects that exist in the run-time code. You can consider setting x:name in XAML as declaring a named variable in your code. Example: <grid x:name= "Grid1" >
X:uid: Identifies some elements that should be used with localized resources with some attribute values for that element. such as resource files:
To associate a control to a resource file: <textblock x:uid= "Greeting" text= ""/>
XAML the properties and events in
1. Type converter : In order to correlate string values and non-string attributes, the XAML parser needs to perform the conversion. The conversion is performed by a type converter, which provides a way to use a specific. NET data type into any other. NET type or convert any other. NET type to the specified data type. XAML looks for a type converter in two steps:
A. Check the property declaration to find the TypeConverter attribute.
B. If there is no TYPECONVERTER,XAML parser in the property declaration, the class declaration of the corresponding data type will be checked.
Note: Like all XML-based languages, XAML is case-sensitive, which means that <button> cannot be substituted for <button> however, type converters are generally not case-sensitive, which means that foreground= "white" and foreground= "White" have the same effect.
2. Complex properties
Although type converters are easy to use, they do not solve all the actual problems, and fortunately XAML provides another option: attribute element syntax (property-element-syntax). With attribute element syntax, you can add a child element with the name form Parent.propertyname. For example:
<Grid>
<Grid.Background>
Blue
</Grid.Background>
</Grid>
What really works is the period (.) in the element name. This period distinguishes this attribute from other types of nested content. This period distinguishes this attribute from other types of nested content. Let's look at the nested properties:
<Grid>
<Grid.Background>
<LinearGradientBrush>
<LinearGradientBrush.GradientStops>
<gradientstop offset= "0.00" color= "Red" ></GradientStop>
<gradientstop offset= "0.50" color= "Indigo" ></GradientStop>
<gradientstop offset= "1.00" color= "Violet" ></GradientStop>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Grid.Background>
</Grid>
Any XAML tag collection can be replaced with a series of code statements that perform the same task, and the label shown above is equivalent to the following code:
var brush = new LinearGradientBrush ();
var gradientStop1 = new GradientStop ();
Gradientstop1.offset = 0;
Gradientstop1.color = colors.red;
Brush. Gradientstops.add (GRADIENTSTOP1);
var gradientStop2 = new GradientStop ();
Gradientstop2.offset = 0.5;
Gradientstop2.color = Colors.indigo;
Brush. Gradientstops.add (GRADIENTSTOP2);
var gradientStop3 = new GradientStop ();
Gradientstop3.offset = 1;
Gradientstop3.color = Colors.violet;
Brush. Gradientstops.add (GRADIENTSTOP3);
Grid1. Background = brush;
3. Markup Extensions
Markup extensions allow XAML file access to declare the value or behavior of an element based only on the type of support, and for most properties, XAML property syntax can work very well. In some cases, however, you cannot hardcode a property value, for example, you might want to set the property value to an already existing object, or you might want to set property values dynamically by binding one property to another, in which case you need to use markup extensions-a specialized syntax for setting properties in an unconventional way.
Markup extensions can be used in nested tags or XML attributes, and they are always surrounded by curly braces {} When used in attributes. For example:
<button foreground= "{x:static Systemcolors.activecaptionbrush}" ></Button>
Markup extensions use the { Markup extension class parameter} syntax. In the example above, the markup extension is the StaticExtension class (according to the Convention, you can omit the last word extension when referencing an extension class). The x prefix indicates that the StaticExtension class is found in the XAML namespace. The final result of the above XAML is the same as the following code:
Cmdbtn.foreground = Systemcolors.activecaptionbrush;
Because markup extensions are mapped to classes, they can also be used as nested properties, as follows:
<Button>
<Button.Foreground>
<x:static member= "Systemcolors.activecaptionbrush" ></x:Static>
</Button.Foreground>
</Button>
4. Additional Properties
Attached properties (attachedproperty) are the programming concepts introduced in XAML, whereby special types can own and define properties, but set properties to attributes or attribute elements on any element. It works because each control has a sub-intrinsic property (for example, the text box has its specific font, text color, which are specified by the fontfamily, foreground property). When a control is placed in a container, additional characteristics are obtained based on the container's type control.
Attached properties use a named form that contains two parts: a definition type. property name. For example, you place individual controls on each row of the grid by attaching properties:
<textbox grid.row= "0" >some words</textbox>
<textbox grid.row= "1" >some words</textbox>
Attached properties are not really properties at all, they are actually converted to method calls. The XAML parser uses the following form of call static method: Definingtype.setpropertyname (). For example, in the XAML code above, the definition type is a Grid class, and the property is row, so the parser calls the Grid.setrow () method, specifically: Grid.setrow (txtans,0).
5. Nesting elements
XAML documents are arranged into a huge nested element tree, and XAML lets each element decide how to handle nested elements, which use one of the following three mechanisms, and the Order of evaluation is the order of the three mechanisms listed below:
A. If the parent element implements the IList interface, the parser calls the Ilist.add () method, and the method passes in the child element as a parameter.
B. If the parent element implements the IDictionary interface, the parser calls the Idictionary.add () method and passes the child element as a parameter for the method. When using a dictionary collection, you must also set the x:key attribute to specify the key name for each entry.
C. If the parent element is decorated with the Contentproperty attribute, the parser uses the child element to set the corresponding property.
6. special characters and blanks
XAML is constrained by XML rules, and if we try to use some special characters, such as,&,< and, you'll get into trouble because the XAML parser thinks you're dealing with something else. Sometimes this is not the desired result, in which case you need to use a specific transformation for the element, such as <L instead of <, > substitution; When you want the text to contain a series of spaces, you need to use the xml:space= "preserve" attribute for the element.
XAML of Events
Attributes can also be used to associate an event handler with the following syntax: event name = "Event handler method name", for example:
<button ... click= "Cmdbtn_click" ></Button>
Here is one and the required method:
private void Cmdbtn_click (object sender, RoutedEventArgs e)
{
Do something
}
When adding an event handler attribute, the only sense of VS can provide great help, and the VS will display a drop-down list of all the appropriate event handlers in the code-behind class that the user can select and create this event handler.
I recently started learning WPF and devexpress to improve my C/s architecture programming capabilities. In the learning process will have some experience, so will write some blog to record these ideas, interested friends can communicate with me to learn. So let's start the journey from here to WPF and DevExpress!
QQ Group: 32745894, welcome everyone to join the discussion!
WPF Tour (ii)-XAML