The letter X is actually the first letter of XML, and the X namespace corresponds to this statement: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml", which contains classes related to parsing the XAML language and is divided into three main categories: Attribute, tag extension, instruction elements
1,Attribute
1.1,x:Class
The purpose of this Attribute is to tell the XAML compiler to merge the results of compiling XAML tags with the classes specified in the background code, as detailed in my previous article (Introduction to XAML)
1.2,x:ClassModifier
The purpose of this Attribute is to tell the XAML compiler what level of access control the classes generated by tag compilation have.
Note: The access control level of a class in XAML must be the same as that of a class in the background, because these two classes are actually one class, only modified with the partial keyword.
1.3,x:Name
The purpose of this x:Name is to define a reference variable for the XAML tag, which means the same as C#defining a reference variable.
What is the difference between the x:Name and Name attributes?The answer is no difference.But for tags that don't have a Name attribute, you can only use x:Name.X:Name is used more widely than Name, so in general, we recommend x:Name
1.4,x:FieldModifier
x:ClassModifier defines an access control level for a class, so you know by name that x:FieldModifier defines an access control level for a field.For example:
<TextBox Text="Hello" x:Name="txt1" x:FieldModifier="public"/>
1.5,x:Key
The purpose of x:Key is to put an index on the resource for retrieval by key-value.This resource is defined in <Window.Resources> in XAML.
For instance:
<Window x:Class="WpfApp4.xKey" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp4" xmlns:sys="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" Title="xKey" Height="450" Width="800"> <Window.Resources> <sys:String x:Key="myString">myKey</sys:String> </Window.Resources> <StackPanel> <TextBox Text="{StaticResource ResourceKey=myString}"/> </StackPanel> </Window>
Resources can be accessed not only in XAML, but also in C#background code, as follows:
var str = this.FindResource("myString").ToString(); MessageBox.Show(str);
1.6,x:Shared
share is translated into Chinese for sharing. The purpose of this Attribute is to tell the XAML compiler whether the resource retrieved each time is the same object or a copy of the object, much like the static keyword.It must be used with x:key, which by default is x:Shared=true.
2. Extension Markup
2.1,x:Type
Type means data type at the programming language level, and x:Type means data type itself.This may be hard to understand. Let's take an example:
Let's create a new subclass of the Button class:
public class MyButton:Button { public Type UserWindowType { get; set; } protected override void OnClick() { base.OnClick(); Window win = Activator.CreateInstance(UserWindowType) as Window; if(win!=null) { win.ShowDialog(); } } }
Create a new myWindow form:
<Window x:Class="WpfApp4.MyWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp4" mc:Ignorable="d" Title="MyWindow" Height="600" Width="800"> <StackPanel> <Button Content="OK"/> </StackPanel> </Window>
The main form XAML code:
<Window x:Class="WpfApp4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp4" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <StackPanel> <local:MyButton Content="Show" UserWindowType="{x:Type local:MyWindow}"/> </StackPanel> </Window>
<local:MyButton Content="Show" UserWindowType="{x:Type local:MyWindow}"/>, this statement first uses the MyButton class, then assigns the property UserWindowType, UserWindowType=MyWindow, because the data type of this property is Type, there is no problem assigning a form class itself to this property.Single Show button, calls the OnClick function defined in the background.Let's look at this function:
protected override void OnClick() { base.OnClick(); Window win = Activator.CreateInstance(UserWindowType) as Window; if(win!=null) { win.ShowDialog(); } }
Because UserWindowType is assigned MyWindow in the XAML code of the main interface, calling Activator.CreateInstance creates an instance of MyWindow, because MyWindow inherits from Window, clicking the button displays the MyWindow form.
2.2,x:Null
Null represents a null value, and most of the time we don't need to explicitly assign a Null value to an attribute, but if an attribute has a default value and we don't need it, we need to explicitly set a Null value.Let's take an example:
<Window x:Class="WpfApp4.MyWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp4" mc:Ignorable="d" Title="MyWindow" Height="600" Width="800"> <Window.Resources> <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}" > <Setter Property="Width" Value="60"/> <Setter Property="Height" Value="36"/> <Setter Property="Margin" Value="5"/> </Style> </Window.Resources> <StackPanel> <Button Content="OK"/> <Button Content="OK" Style="{x:Null}"/> <Button Content="OK"> <Button.Style> <x:Null/> </Button.Style> </Button> </StackPanel> </Window>
In the resource we define a Style for the Button button. If you do not set Button's style to Null, then all Button's Styles for the default Style are Styles set in the resource.The code works as follows:
The third button assigns a value to Style in another way. The effect is the same as the second button, but the writing is cumbersome.
2.3,x:Array
It exposes an ArrayList instance of a known type to the consumer through its Items property, and the type of members within the ArrayList is specified by the Type of x:Array.Let's take an example:
<Window x:Class="WpfApp4.xArrayTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp4" xmlns:sys="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" Title="xArrayTest" Height="450" Width="800"> <Grid> <ListBox Margin="5"> <ListBox.ItemsSource> <x:Array Type="sys:String"> <sys:String>tim</sys:String> <sys:String>tom</sys:String> <sys:String>victor</sys:String> </x:Array> </ListBox.ItemsSource> </ListBox> </Grid> </Window>
Here the Type of x:Array is sys:String, or string.If you want to use a string, also refer to the System namespace: xmlns:sys="clr-namespace:System;assembly=mscorlib", which corresponds to: using System in C#;
2.4,x:Static
Its function is to use static members of data types in XAML documents. Here's an example:
XAML code:
<Window x:Class="WpfApp4.xStaticTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp4" mc:Ignorable="d" Title="{x:Static local:xStaticTest.WindowTitle}" Height="450" Width="800"> <StackPanel> <TextBlock FontSize="32" Text="{x:Static local:xStaticTest.ShowText}"/> </StackPanel> </Window>
Background code:
public static string WindowTitle = "Mountain High Moon Small"; public static string ShowText { get { return "When the water subsides, the rocks emerge"; } }
Running effect:
This should be understandable, I won't explain much.
2.5, XAML instruction elements
x:Code and x:XData.Its purpose is to include some logical code that should have been placed in the background code.The advantage of this is that you don't have to separate the XAML code from the C#code in two files, but this is not very recommended unless you encounter some extreme situations.Because doing so violates the original intent of WPF.