We know that for the description of interface elements, the XAML of WPF is not the first, HTML is much earlier, and the DFM of Delphi is also one. The separation of Interface Description and interface interaction logic has many advantages, such as visualization and interface reuse. Microsoft always wants to unify the world, and the emergence of WPF is also such an ideal. Of course, this kind of ideal is also supported by actual needs. For the application architecture, the traditional Cs and Bs are both integrated, so the interface design under the integration of these two modes also has their needs, it facilitates the conversion and integration of the two modes. WPF uses XAML as the description language for UI rendering. As a language, XAML itself has nothing to pay special attention to. We only need to follow this rule. The basic idea of the XAML language is more HTML than that. It only adopts the XML format and still follows the tree structure. Each UI element in XAML actually represents an instance of a background UI class. Therefore, we can also regard the representation of this class instance in XAML as a serialization of this instance object.
Object serialization actually refers to object attributes (static or dynamic features of an object, including attributes and fields ), it is saved as a byte stream (or bit stream) in a specific format for storage and transmission. The process of restoring this object to an instance in the memory is deserialization, And the deserialization process is also the process of dynamic Object Instantiation. For XAML, more is instantiation (deserialization), which needs to be completed by the XAML parser. Because XAML only contains attributes and events, it is much more complicated than simple object serialization. Using traditional Object deserialization, the UI element cannot be instantiated in memory. However, it is similar in principle, but the parsing rules of XAML are more complicated.
Of course, when the program runs, the features and behavior (methods) of the UI element objects must be combined to form a real UI object instance. The process of generating the corresponding object instance by parsing the XAML is the process of dynamic Object Instantiation. Here, the reflection mechanism is required to generate an object instance (for example, in Delphi, the compiler completes the parsing directly and compiles it into the Execution Code, therefore, the runtime Parsing is not required, so reflection mechanisms are not required, although it also has some prototype of the current reflection mechanism ). The reflection mechanism consists of two parts: Metadata and dynamic instantiation. The metadata records the information of the class, such as the class name and the member information of the class. Dynamic instantiation is not complex, that is, it dynamically constructs an instance of the corresponding object (this information comes from the XAML, the attribute information obtained from XAML is improved by using the value assignment mechanism in the reflection mechanism. If you think of a table as a class and a record class as an object, metadata is similar to the structure information of a table, constructing a record of a table based on the provided information is actually equivalent to Object Instantiation. To dynamically construct an instance of a class in DOTNET, you can use the createinstance method of activator or the createinstance method of the Assembly class.
Although the XAML parsing process is not very clear, we can also use Object serialization to understand this process. I wrote this part in the previous blog, so I will not go into details here. However, the understanding of this process is critical, because many seemingly mysterious and profound things actually happen in this process. In fact, for the parsing of XAML, we can use the XML access and reflection technology provided by Vs, but it does not have any practical significance.
For specific syntax rules of XAML, you can refer to the relevant documents. To tell the truth, I am not particularly familiar with it and do not want to memorize it. Because we only need to figure out the internal mechanism of this implementation.
In addition, you need to remember that XAML is only the attribute description of the UI element class. We cannot use the XAML information to generate a new class, the classes corresponding to the UI elements in these XAML must all exist in the background code. We just instantiate this object.
Topic: it is very difficult to define a language like XAML. However, once such a language rule is determined, it is not difficult to parse and apply it. The real masters in the world are the creators of these (game) Rules.