In layman's language of WPF XAML

XAML is what?

XAML is Microsoft created a development language, you can expand Application Markup Language. Played HTML + CSS + JavaScript role.

Create a default WPF program

PS practice: We usually direct download Visual Studio and create a solution / project to develop the program, in fact, we use the IDE compiler configured the parameters for the code that we want to develop, and ready for some basic source code, province we go to manual configuration)

Create a default, we will see a project in the current solution. It contains the following branches:

1.Properties branches: the main contents inside to use the resources (icons, pictures, static string) and configuration information

2.References branches: the mark of the current project reference to the class library / other projects

3.App.xaml branches: the main program in the Windows system environment, a program is a process (Process). A GUI process requires a form as the main form. App.xaml role is to file a statement of who processes the program and the main form would be.

The main form of the program: 4.Window1.xaml branch

XAML code analysis

There will be a .xaml.cs file we will see a partial class .xaml.cs file will be corresponding next .xaml. And winform is not very similar. Take Window1.xaml and Window1.xaml.cs two files to see. There is a partial class MainWindow under Window1.xaml.cs, in accordance with prior experience WinForm Window1.xaml is not it also corresponds to the partial class MainWindow it? Let's assume that it is, we will see a line of code inside

Title="MainWindow" Height="450" Width="800"

We perform such operations in the code behind:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var aa = this.Title;
            var bb = this.Height;
            var cc = this.Width;
            Debug.Print(aa.ToString() + "==========" + bb.ToString() + "============="+cc.ToString());
        }
    }

The results were as follows:

MainWindow==========450=============800

This fully confirms our guess: an object in memory when a corresponding label statement XAML element, the outermost label element is the partial class code-behind.

In the hierarchical relationships between objects in XAML, either side by side or are included, all reflected in the relations tab. That class in the background, we can be referenced by using a different name space, and in the XAML code, we have to define the name space by xmlns features the following format:

xmlns: [optional map prefix] = "namespace." Look at the default XAML code:

<Window 
        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:MyFirstWpfApplication"
        x:Class="MyFirstWpfApplication.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        
    </Grid>
</Window>

We can find five lines of code begins with xmlns, that is, "cites five default namespace."

Where x: and mc: the beginning of the statement quoted above indicates that the call name space.

x: It contains classes that are related to parse XAML language.

x: Class = "MyFirstWpfApplication.MainWindow" represents the current label the Window class name resolves to C # class. This is also the beginning of our proven it were a partial class background. It is x: under the Attribute, only for the root, and the root node type to x: Class type indicated by (and is a division of classes) consistent.

Other Attribute under Supplementary x namespace:

x: Name: tell the compiler also for this example declares a reference to generate an instance variable corresponding to the label, the name is the variable x: Name value. As well as the Name property XAML tags corresponding to the object is also set to x: Name, and registered with the UI tree, easy to find.

x: FieldModeifer setting elements accessible level

x: key using key-value pairs

XAML syntax

xaml document is a tree structure

xaml in the assignment of an object property syntax: First xaml code can not run logic programming, and when we create a label object when necessary to initialize its properties have meaningful use. Object property assignment in two ways

1. Use simple string assignment. That simple assignment Attribute = Value syntax, grammar restrictions due xaml, Value can only be a string value.

2. Use the property element complex assignment. Attribute element refers to an element of a label to a tag corresponding to the property, i.e., expressed in elemental form to an example of the properties.

XAML syntax for namespace references

xmlns: [map name] = "clr-namespace: name of the class library namespace; assembly = library filename"

 

Guess you like

Origin www.cnblogs.com/jingjingweixiao/p/10947386.html