The study concluded, "in layman's language WPF" XAML Markup Language two

x namespace

"X name space" that X is mapped to XML namespace when he took the name (if using the letters y, that he called "y the namespace"), x namespace members (x.name, X.class) is dedicated to XAML compiler looking for guiding XAML compiler to compile the code into XAML CLR code. Almost all the WPF XAML code that contains all you need to reference xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" namespace

x namespace are what

x namespace mapping is xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml", which he included in the class were associated with parsing XAML language, so it can be called it "XAML" namespace, and C # languages, XAML also has its own compiler. XAML language is parsed and compiled, the final form of Microsoft intermediate language stored in an assembly. In parsed and compiled XAML language process, we often need to tell the compiler some important information, such as compiling the results of XAML code should be which compiled the results of the C # code with the merger, the use of XAML declared elements are public or privated access levels and so on. These allow programmers to be able to communicate with XAML compiler tools stored in the intermediate x name.

Tools x namespace contained

name Species (the form in XAML)
x:Array Markup Extension
x:Class Attribute
x:ClassModifier Attribute
x:Code XAML directive element
x:FieldModifier Attribute
x:Key Attribute
x:Name Attribute
x:Null Markup Extension
x:Shared Attribute
x:Static Markup Extension
x:Subclass Attribute
x:Type  Markup Extension
 x:TypeArguments  Attribute
x:Uid Attribute
x:XData XAML directive element

 

They may be divided into Attribute, XAML markup extension instruction elements and three

x namespace Attribute

Earlier we already know, Attribute and Property are two levels of things. Attribute-level language is something that is to the compiler to see, things Property is object-oriented level, is to use programming logic, and a XAML Attribute label's most corresponds Property objects. When programmed using XAML, if you want to add to it a special tag which affects parsing XAML compiler for him, this time you need to add some extra Attribute for him, for instance, you want to tell the compiler to compile XAML which results compiled C # classes merger, this time must be added to the label x: class = "target class name" has been informed of such a Attribute XAML compiler. x: Attribute is not a member of this Class object, but we take it out hard stick up from x x namespace: Class
of this role is to tell Attribute XAML compiler to compile the results with the specified code behind XAML tag the class merged.

  In using x: Class must comply with the following requirements when:

    1, this can only be used for root Attribute

    2, the use of x: Class type to the root of x: Class type value indicated consistent

    3, x: Class type value indicated must use partial (partial class) keywords declaration.

  x:ClassModifier

    Attribute this role has access level used to tell how XAML compiled by the label compiled generated classes, when using this tag Note:
    1, the notes must have x: Class Attribute.
    2, x: a value and must ClassModifier x: Class indicated access class consistent level of control.   

  x:Name

  x: Name has two action
    1, to tell the compiler XAML, when a tag with x: x is generated in addition to the outer label corresponding to examples of this example also declare a reference variable Name, the variable name: Name of the value of
    2, the object corresponding label XAML Name attribute (if any) is also set to x: Name, registered and this value to the UI tree, easy to find using

  So in the XAML code should be used Name or x: Name it? Name attribute definition FrameworkElement class, this class is the base class for controls WPF, the WPF so all controls have the Name property. When an element has a Name attribute, you use the Name or x: Name effect is the same, such as <Button x: Name = "btn" /> and <Button Name = "btn" />, XAML compiler movements are btn declared as a variable of type Button Button and a reference type instance, and the Name property of this instance is also btn. In this case, Name and x: Name is interchangeable, but not both in one element. For those elements no Name property, in order to create a reference when XAML statement also access variables in C # code, we can only use x: Name. Because x: Name of function covers the functionality of the Name property, so all use x: Name to enhance the uniformity and readability of the code

  x:FieldModifier

  Using x: After Name, XAML examples of labels having on their corresponding reference variable, and these variables are reference fields of the class. Since it is the class of the field will inevitably have to look at their level of access. By default, these variables are referenced field class. Since it is the class of the field will inevitably require access level. By default, the access level fields are in accordance with the object-oriented principle of encapsulation been set to internal. In programming, we sometimes need to access from one assembly to another assembly element form, this time you need to access the control variables to the public reference level, x: FieldModifier is used to change the reference in XAML variable access levels. It is noteworthy, because the x: FieldModifier reference variable is used to change the access level, so use x: FieldModifier premise is that this label also uses x: Name, otherwise how can the reference variables it.

  x:Key

  The most natural way than the use of "key-value" in the form of a. In XAML file, we can put a lot of content need to use multiple resources extracted in the dictionary (Resource the Dictionary), the need to use this time to use the resources of its Key retrieved it. x: Key role is labeled as a resource index for retrieval, in WPF, almost every element has its own Resources property that is a collection of a "Key-Value" style, as long as the elements in this collection this element becomes a resource dictionary entry, of course, in order to be able to retrieve this condition, you must add it to x: Key. Resources (Resources) is important only in WPF, XAML content need to be reused, such as Style, Template and various animation and so need to put resources in. 

 1 <Window x:Class="WpfApp2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 3 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 5 xmlns:local="clr-namespace:WpfApp2"
 6 xmlns:sys="clr-namespace:System;assembly=mscorlib"
 7 mc:Ignorable="d"
 8 Title="Demo" Height="200" Width="300">
 9 <Window.Resources>
10 <sys:String x:Key="myString">Hello World</sys:String>
11 </Window.Resources>
12 <StackPanel Orientation="Vertical">
13 <TextBox Margin="10" Text="{StaticResource ResourceKey=myString}" x:Name="txtSrc"></TextBox>
14 <TextBox Margin="10" x:Name="txtDes"></TextBox>
15 <Button Margin="10" Click="Button_Click">Show</Button>
16 </StackPanel>
17 </Window>

 

Use the String class in XAML, we use the xmlns: sys = "clr-namespace: System; assembly = mscorlib" references mscorlib.dll, and put them in the System namespace is mapped to the sys namespace in XAML. We then use the property tag syntax adds a string to the Window.Resources years, and it's x: Key to myString. StackPanel form contains two TextBox and a Button, a TextBox when set the Text property for the first, we used the myString this resource, therefore, run a program that we can see the first TextBox shows the character of resources value of a string

Not only can access the resource in XAML, in C # you can also visit the following is Button.Click event handler:

private void Button_Click(object sender, RoutedEventArgs e)
{
var myString = this.FindResource("myString") as string;
txtDes.Text = myString;
}

Resources have to call an object's attributes FindResource method can retrieve his resources in the resource dictionary, after retrieving the resource and then restore it to the correct data type can be used. After clicking the button, you can see TxtDes text box show the same string.

   x:Shared

   In use x: Key time we know that once we put some resources into the dictionary object as they could be retrieved after repeated use, so whenever we retrieve an object, we get exactly the same objects, or is the subject of multiple copies of it? Then we look to the x: Shared assigned what the value, x: Shared and must be x: Key with the use, if x: Shared value is True, then every time you retrieve the object, we enjoy are the Shared a new copy of the value is False, each time we crawl to this object, we enjoy are the object: the same object, or if x. XAML compiler will add x as a resource to hide: Shared = "True", that is, the default is the same case we get resources

X extensible markup namespace

  Markup Extension (Markup Extension) MarkupExtension fact is that some kind directly or indirectly derived class. x namespace contains some such classes, so often they are called extended x mark in the name space.

  x:Type

  x: Type of value is the name of a data type, under normal circumstances, examples of the type of data that we operate in programming references, but sometimes we will use the data type itself. When we want to express a certain type of data you need to use in XAML x: Type markup extension, such as a property of a class, when its value requires a data type, this is when we will use property assignment in XAML x: Type.
First we create a Button derived class

 1 class MyButton:Button
 2 {
 3 public Type UserWindowType { get; set; }
 4 
 5 protected override void OnClick()
 6 {
 7 base.OnClick();
 8 
 9 var winType = Activator.CreateInstance(UserWindowType) as Window;
10 if (winType == null)
11 return;
12 winType.Show();
13 }
14 }

This class has a property of type Type that UserWindowType, we need to assign a data type to him, but this class also override the base class's OnClick event, in addition to the Click event as a trigger like the base class, it will also use create an instance of a type UserWindowType stored, if an instance when the instance of the Window class type (or derived class), then put the form is displayed
and then we add a derived class called Window MyWindow in the project.

<Window x:Class="WpfApp2.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:WpfApp2"
mc:Ignorable="d"
Title="MyWindow" Height="200" Width="300">
<StackPanel Orientation="Vertical" x:Name="mainPanel" >
<TextBox Margin="10"></TextBox>
<TextBox Margin="10"></TextBox>
<Button Margin="10">ClickMe</Button>
</StackPanel>
</Window>

Finally, the custom button to the main window appears, and as the MyWindow MyButton.UserWindowType assigned to a data type attribute

<Window x:Class="WpfApp2.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:WpfApp2"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="Demo" Height="200" Width="300">
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<local:MyButton Width="200" UserWindowType="{x:Type TypeName=local:MyWindow}">Show</local:MyButton>
</Grid>
</Window>

Note: Because MyButton and MyWindow two custom classes are included in the namespace of the current project, so the current project namespace prefix references come in and use local map, so use them MyButton and MyWindow also add local prefix
compile and run the program, click the button on the main form, the custom form will be displayed. We can also write a few custom form class to extend this program, when you need to replace only the value UserWindowType in XAML inside it.

 

  x:Null

   Sometimes we need to assign a null value to a property in C # language, use the keyword to represent null null values, to represent the null value in XAML is x: Null. Most of the time we do not explicitly assign null value to a property, but you need to explicitly set a null value if a property has a default value and we do not need this default.

 1 <Window x:Class="WpfApp2.MainWindow"
 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6 xmlns:local="clr-namespace:WpfApp2"
 7 xmlns:sys="clr-namespace:System;assembly=mscorlib"
 8 mc:Ignorable="d"
 9 Title="Demo" Height="200" Width="300">
10 <Window.Resources>
11 <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
12 <Setter Property="Width" Value="60"></Setter>
13 <Setter Property="Height" Value="30"></Setter>
14 <Setter Property="Margin" Value="10"></Setter>
15 </Style>
16 </Window.Resources>
17 <StackPanel Orientation="Vertical">
18 <Button>Hello</Button>
19 <Button>Hello</Button>
20 <Button Style="{x:Null}">Hello</Button>
21 </StackPanel>
22 </Window>

The above example to a Window Style on the resource and put it inside of x: Key and Button TargetType are set to become a type, so that all Button controls on the UI by default quilt with this Style, except after a Button because it explicitly provided to the style x: Null

 

  x:Array

  x: Array action is through his Items property exposed to a user of a known type ArrayList instance, the type ArrayList members by x: Array of specified Type, the following example a x: Array as the data source to a ListBox provide data

 1 <Window x:Class="WpfApp2.MainWindow"
 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6 xmlns:local="clr-namespace:WpfApp2"
 7 xmlns:sys="clr-namespace:System;assembly=mscorlib"
 8 mc:Ignorable="d"
 9 Title="Demo" Height="200" Width="300">
10 <Grid>
11 <ListBox>
12 <ListBox.ItemsSource>
13 <x:Array Type="sys:String">
14 <sys:String>Hello World</sys:String>
15 <sys:String>Hello Simple</sys:String>
16 <sys:String>Hello C#</sys:String>
17 </x:Array>
</18 ListBox.ItemsSource>
19 </ListBox>
20 </Grid>
21 </Window>

Thus, parsing <x: Array> tag when the compiler generates code that calls the method AddChild <x: Array> child element added to individually tag x: Item Array instance in

   x:Static

  x: Static is a very common markup extension, its function is to use static data member types in XAML document. Because XAML can code the logic, the use of x: static members must be accessed Static data types of attributes or fields.

 1 <Window x:Class="WpfApp2.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:WpfApp2"
 7         xmlns:sys="clr-namespace:System;assembly=mscorlib"
 8         mc:Ignorable="d"
 9         Title="{x:Static local:MainWindow.WindowTitle}" Height="200" Width="300">
10     <StackPanel>
11         <TextBlock FontSize="30" Text="{x:Static local:MainWindow.ControlContent}"></TextBlock>
12     </StackPanel>
13 </Window>

  XAML directive element

  XAML element has two instructions

  x:Code

    His role is to contain this should be placed in the code-behind some of the C # code, the benefits of doing so is not to the XAML and C # code is split in two files, if they had not encountered some extreme environments please go to do not be so written, this code is not the biggest problem is to maintain and difficult to debug

  x:XData

    x: XData is a dedicated label, WPF object containing the data referred to the data source, the data for the player object data source is supplied to the user data is referred to as data provided (Data Provider). WPF library contains multiple data providers, which have a class called XmlDataProvider, designed to provide XML based data, if you want XmlDataProvider instance in XAML statement with a total data, then the data will be on the instance XmlDataProvider x: tag's content XData

Guess you like

Origin www.cnblogs.com/insipid/p/11924073.html