Microsoft defines XAML as a "Programming Language" of "simple", "General", and "declarative ". This means that we will see it in more places (such
Silverlight), and it is obviously different from its original version of XML (XAML is a language based on XML and follows XML structure rules)
More logic processing methods are provided. If you want to, you can leave the XAML alone to write the WPF program. But this is a bit similar to developing. net using notepad.
The meaning of the program is not easy to use. The definition mode of XAML allows non-programmers to portray the UI in an "easy to understand" way, and we are already familiar with this method, such
Webform, or the Delphi form that I have never forgotten (sometimes I think of it, but I have forgotten the Object Pascal ).
<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1">
<Grid>
</GRID>
</WINDOW>
This is a very simple XAML that defines a blank WPF form (window ). XAML corresponds to. Net code, but this process is completed by a specific XAML compiler and runtime interpreter. When the interpreter processes the above Code, it is equivalent:
New window1 {Title = "window1 "};
Here we can understand the difference between the two. The advantage of using XAML is that we can see the final display effect in the design phase. Obviously this is what the artist needs. You can enter "xamlpad.exe" from the vs command line to see the intuitive effect.
As a "language" Applied to the. NET platform, XAML also supports many concepts that we are familiar with. net.
1. namespace
By default, XAML maps the following. Net namespace to "http://schemas.microsoft.com/winfx/2006/xaml/presentation ":
System. Windows
System. Windows. Automation
System. Windows. Controls
System. Windows. Controls. primitives
System. Windows. Data
System. Windows. Documents
System. Windows. Forms. Integration
System. Windows. Ink
System. Windows. Input
System. Windows. Media
System. Windows. Media. Animation
System. Windows. Media. Effects
System. Windows. Media. Imaging
System. Windows. Media. media3d
System. Windows. Media. textformatting
System. Windows. Navigation
System. Windows. Shapes
In addition to the main namespace that contains the vast majority of the types required by WPF, there is also a XAML-specific namespace (system. Windows. markup) -- "http://schemas.microsoft.com/winfx/2006/xaml ". The syntax for using non-default namespaces is somewhat similar to C # namespace alias. We need to add a prefix, for example, "x" in the following example ".
<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1">
<Grid>
<Textbox X: Name = "txtusername" background = "{X: NULL}"> </textbox>
</GRID>
</WINDOW>
We can also introduce CLR namespace.
<Collections: hashtable
Xmlns: collections = "CLR-namespace: system. collections; Assembly = mscorlib"
Xmlns: SYS = "CLR-namespace: system; Assembly = mscorlib">
<SYS: int32 X: Key = "key1"> 1 </sys: int32>
</Collections: hashtable>
2. Property
You can set the attributes of a XAML element in the following two ways.
Method 1
<Label name = "label10" foreground = "red"> label10 </label>
Method 2
<Label name = "label11">
<Label. content>
Label11
</Label. content>
<Label. Foreground>
Blue
</Label. Foreground>
</Label>
WPF converts an attribute string in XAML to an actual attribute value in the following order.
(1) If the attribute value starts with braces, or the attribute is an element derived from markupextension, tag extension is used.
(2) If the attribute is declared with the specified typeconverter or the conversion feature (typeconverterattribute) is used, it is submitted to the type converter.
(3) try to convert the primitive type, including checking the enumeration name.
<Trigger property = "visibility" value = "collapsed, hidden">
<Setter.../>
</Trigger>
3. typeconverter
WPF provides a large number of Type converters to convert red strings similar to the following example to systemwindows. Media. Brushes. Red.
<Label name = "label10" foreground = "red"> </label>
Equivalent
This. label10.foreground = system. Windows. Media. Brushes. Red;
However, the following code better reflects the actual conversion behavior during the runtime.
VaR typeconverter = system. componentmodel. typedescriptor. getconverter (typeof (Brush ));
This. label10.foreground = (Brush) typeconverter. convertfrominvariantstring ("red ");
For a list of converters, see: MS-help: // Ms. msdnqtr. v90.chs/fxref_system/html/35bffd5f-b9aa-1ccd-99fe-b0833551e562.htm.
4. markupextension
Pair
An extension of XAML to support complex attribute values. These Markup extensions are typically inherited from markupextension and included in braces. WPF
Provides some common tag extensions, such
Nullextension, staticextension, dynamicresourceextension,
Staticresourceextension and binding. Similar to attribute rules, we can usually omit extension.
This suffix. Note that some tag extensions belong to system. Windows. markup, so we need to add a namespace prefix.
<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1">
<Grid>
<Textbox background = "{X: NULL}"> </textbox>
</GRID>
</WINDOW>
We can provide the required construction parameters for the markup extension.
<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1">
<Grid>
<Label content = "{X: static systemparameters. iconheight}"/>
</GRID>
</WINDOW>
This
In this example, we pass the system. Windows. systemparameters. iconheight value as a parameter to "Public
Staticextension (string member )"
Constructor. this parameter is usually called a location parameter. Another parameter is to pass a specific value to the tag extension object property. The attribute name must be specified in syntax, so it is called the name parameter. The following example indicates
Bind the textbox1.text parameter to label. content. When the content of the edit box changes, the label content is automatically synchronized.
<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1">
<Grid>
<Textbox name = "textbox1"/>
<Label content = "{binding elementname = textbox1, Path = text}"/>
</GRID>
</WINDOW>
Tag extension allows nesting and can reference itself. Let's look at another example.
<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1">
<Grid>
<Textbox name = "textbox2" width = "128" text = "{binding relativesource = {relativesource self}, Path = width}"/>
</GRID>
</WINDOW>
In this example, the textbox. text content is bound to the height (width) of its own (Self ).
One problem with Tag extension is the escape of braces. After all, we often need to use it in content display. The solution is to add an additional braces.
<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1">
<Grid>
<Label content = "{}{ Hello, world !} "/>
</GRID>
</WINDOW>
If you think it is ugly, you can also write it as follows.
<Window X: class = "learn. WPF. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1">
<Grid>
<Label> {Hello, world !} </Label>
</GRID>
</WINDOW>
5. Content
XAML is very similar to HTML. We can add any content to element content items, which provides richer UI expression capabilities and is no longer like winform. "What can we do, cannot do anything ".
<Button>
<Hyperlink> click </Button>
Note that content items are not necessarily content items. For example, ComboBox, ListBox, and tabcontrol use items as content items.
6. xamlreader & xamlwriter
Connect
Generally, XAML is compressed into baml (Binary Application Markup Language) during project compilation)
Save it to the resource file. Baml only contains the pure format declaration of xaml and does not have any Execution Code such as events. Remember not to be confused with msil. XAML
The runtime interpreter interprets baml and generates corresponding element objects.
The system. Windows. markup namespace provides xamlreader and xamlwriter types, allowing us to manually manipulate XAML files.
VaR
Window = (window) xamlreader. parse ("<window
Xmlns =/"http://schemas.microsoft.com/winfx/2006/xaml/presentation/"> </WINDOW> ");
Window. showdialog ();
Of course, we can also read from the file stream.
Using (VAR stream = new filestream (@ "test. XAML", filemode. Open ))
{
VaR window = (window) xamlreader. Load (Stream );
VaR button = (button) window. findname ("btnok ");
Button. Click + = (S, ex) => MessageBox. Show ("Hello, world! ");
Window. showdialog ();
}
Test. XAML
<Window
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window2" Height = "300" width = "300">
<Grid>
<Button X: Name = "btnok"> OK </button>
</GRID>
</WINDOW>
Note that the XAML code loaded by xamlreader cannot contain any type (X: Class) or event code (X: Code ).
We can use xamlwriter to restore a compiled baml to XAML.
VaR XAML = xamlwriter. Save (New window2 ());
MessageBox. Show (XAML );
Output:
<Window2
Title = "window2" width = "300" Height = "300"
Xmlns = "CLR-namespace: Learn. WPF; Assembly = Learn. WPF"
Xmlns: Av = "http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<AV: Grid>
<AV: button name = "btnok"> OK </AV: button>
</AV: Grid>
</Window2>
Dynamic Loading of XAML is very useful when using dynamic skin scenarios. You only need to know about it now.