[WPF Basics] [1] _ Architecture

Source: Internet
Author: User
1. Architecture of WPF

This topic provides guidance on Windows Presentation Foundation (WPF) class hierarchies, covering most of the main subsystems of WPF and describing how they interact. This topic also describes in detail some of the choices made by the WPF architect.

System. Object

The main programming model of WPF is through hostingCodePublicOpen. In the early stages of WPF design, there was a lot of debate about how to define managed and unmanaged components of the system. CLR provides a series of functions to make development more efficient and reliable (including memory management, error processing, and general-purpose type systems), but this requires a price.

Describes the main components of WPF. The red part of the graph (presentationframework, presentationcore, and milcore) is the main code part of WPF. Only one of these components is an unmanaged component-milcore. Milcore is written in unmanaged code to achieve close integration with DirectX. All displays in WPF are completed by the DirectX engine, which enables efficient hardware and software presentation. WPF also requires precise control over Memory and execution. The composite engine in milcore is greatly affected by performance, and many advantages of CLR need to be abandoned to improve performance.

Later in this topic, we will discuss communication between managed and unmanaged parts of WPF. The following describes the rest of the managed programming model.

System. Threading. dispatcherobject

Most objects in WPF are derived from dispatcherobject, which provides the basic structure for processing concurrency and threads. Scheduling-based WPFProgramMessage system. The method is similar to the common Win32 message pump. In fact, the WPF scheduler uses the USER32 message to execute cross-thread calls.

To discuss concurrency in WPF, we must first understand two core concepts: scheduling program and thread Association.

In the design phase of WPF, the target tends to be executed by a single thread, but this is not an "associated" model with the thread. When a component uses the identity of the execution thread to store a certain type of state, thread Association will occur. The most common form is to use local thread storage (TLS) to store the state. Each logical thread required to be executed by thread Association is only owned by one physical thread in the operating system, which occupies a large amount of memory. Finally, the thread processing model of WPF maintains synchronization with the existing USER32 thread processing model executed by a single thread associated with the thread. The main reason is interoperability-systems like ole 2.0, clipboard, and Internet Explorer all require a single thread Association (STA) for execution.

Assume that you have an object with a sta thread, you need to communicate between threads and verify that you are on the correct thread. This is the role of the scheduler. A scheduler is a basic message scheduling system with multiple queues sorted by priority. Message examples include the original input notification (move the mouse), frame function (layout), or user command (execute this method ). Derived from dispatcherobject, you can create a CLR object with the sta action and get a pointer to the scheduler at creation.

System. Windows. dependencyobject
One of the main architecture principles used to generate WPF is the preferred attribute rather than the method or event. Attributes are declarative, making it easier to specify intent rather than action. It also supports model-driven or data-driven systems to Display User Interface content. The expected result of this idea is to create more attributes that you can bind to better control the behavior of the application.

To get more information from an Attribute-driven system, an attribute system with richer content than CLR is required. A simple example of this richness is the change notification. To implement bidirectional binding, You need to bind a change notification to both parties. To associate behavior with attribute values, you must be notified when the attribute values are changed. Microsoft. NET Framework has an inotifypropertychange interface that allows an object to publish a change notification (however, this is optional ).

WPF provides a rich property system derived from the dependencyobject type. The property system is actually a "dependency" property system because it tracks the dependency between attribute expressions and automatically re-verifies the attribute value when the dependency changes. For example, if you have an inherited attribute (such as fontsize), the system is automatically updated when the parent attribute of the element that inherits the value is changed.

The basis of the WPF property system is the concept of property expressions. In the first version of WPF, the attribute expression system is disabled, and expressions are provided as part of the framework. The expression causes the attribute system not to have hard-coded data binding, style adjustment or inheritance. Instead, these functions are provided by the layers behind the framework.

The attribute system also provides sparse storage of attribute values. Because an object can have dozens of attributes (if less than attributes), and most of the values are in its default state (inherited, set by style, etc ), therefore, not every instance of an object must have the full weight of each attribute defined on the object.

The last new feature of the property system is the concept of additional properties. WPF elements are generated based on the principles of combination and Component reuse. Some contained elements (such as grid layout elements) usually need other data on the child element to control its behavior (such as row/column information ). Any object can provide attribute definitions for any other object, rather than associating all these attributes with each element. This is similar to the "expando" function in JavaScript.

System. Windows. Media. Visual

After defining a system, the next step is to draw pixels to the screen. The visual class is used to generate a tree of visual objects. Each object can selectively contain metadata about drawing commands and how to present these commands (editing, transformation, etc. The visual design is extremely lightweight and flexible, so most of the functions are not open to the API and are highly dependent on protected callback functions.

Visual is actually the entry point to the WPF combination system. Visual is the connection point between the following two subsystems: hosted API and unmanaged milcore.

WPF displays data by traversing an unmanaged data structure managed by milcore. These structures (called composite nodes) represent the hierarchical display tree, where each node has a rendering command. You can only access this tree through the message passing Protocol (as shown on the right ).

When you program WPF, you will create visual elements and derived types that communicate internally with the composite tree through this messaging protocol. In WPF, you can create one or more composite nodes without creating a composite node for each visual.

Pay attention to a very important architectural detail-the entire tree of the visual object and the drawing command must be cached. In terms of graphics, WPF uses a retained rendering system. This allows the system to repaint at a high-refresh rate without the combined system blocking callback to user code. This helps prevent applications from responding.

Another important detail that is not very noticeable in the graph is how the system actually performs the combination.

In USER32 and GDI, the system works on an instant mode editing system. When a component needs to be rendered, the system creates a cutting boundary that does not allow the component to touch pixels outside the boundary. Then, the component is required to draw pixels in the box. This system works well on systems with limited memory, because when some content changes, you only need to deal with the affected components-there will be no two components working on a pixel color change.

WPF usesAlgorithm"Draw the model. This means that each component is not edited, but presented from the back to the front of the displayed content. This allows each component to be drawn on the display content of the previous component. The advantage of this model is that you can generate partially transparent complex shapes. Compared with today's modern graphics hardware, this model is relatively fast (except when USER32/GDI is created ).

As described above, a core principle of WPF is to move to a more declarative and "Attribute-centric" programming model. In a visualization system, this is manifested in two situations that require attention.

First, if you want to retain the pattern graphics system, it is actually moving from the imperative drawline/drawline type model to the data-oriented model new line ()/New Line (). With this data-driven rendering movement, you can perform complex operations on the rendering commands expressed by attributes. The Type derived from drawing is actually the object model used for rendering.

Second, if you evaluate an animation system, you will see that it is almost completely declarative. Developers are not required to calculate the next position or next color. You can represent an animation as a set of attributes on an animation object. As a result, these animations can represent the intent of a developer or designer (moving the button from one position to another within five seconds ), the system can determine the most effective way to complete this task.

System. Windows. uielement

Uielement defines core subsystems, including layout, input, and event.

Layout is a core concept in WPF. In many systems, there may be a set of fixed layout models (HTML supports three layout models: stream, absolute, and table), or there may be no layout model (USER32 actually only supports absolute positioning ). WPF assumes that developers and designers want to have a flexible and scalable layout model, which may be driven by attribute values rather than imperative logic. At the uielement level, a two-phase model with the measure and arrange processing procedures is introduced as the basic layout protocol.

Measure allows the component to determine the size to be used. This phase is independent of arrange, because in many cases, the parent element requires the child element to measure several times to determine its optimal position and size. The fact that the parent element requires the child element to measure is another key principle of WPF-content size. All controls in WPF support the ability to adjust the original content size. This makes localization easier and allows the Dynamic Layout of elements during the hour. The arrange stage allows the parent element to locate and determine the final size of each child element.

It usually takes a lot of time to discuss the output end of WPF (visual and related objects ). However, there are also many innovations at the input end. The most basic change to the WPF input model may be a consistent model. Input events are routed through the system using this model.

The input is sent as a signal on the driver of the kernel-mode device and routed to the correct process and thread through a complex process involving the Windows Kernel and USER32. Once the USER32 message corresponding to the input is routed to WPF, it is converted to the original input message of WPF and sent to the scheduler. WPF allows the conversion of original input events to multiple actual events, and allows the implementation of a function similar to "mouseenter" at a lower system level while ensuring that the transfer is in place.

Each input event is converted to at least two events-"preview" events and actual events. All events in WPF have the concept of routing through the element tree. If an event traverses the tree from the target to the root, it is called "Bubbling". If it traverses down from the root to the target, it is called a "Tunnel ". Enter the preview event tunnel so that any element in the tree has the opportunity to filter events or take actions on events. Then, the general (non-Preview) event will bubble up from the target to the root.

The split tunnel and bubble phases make shortcut keys and other functions consistent in the composite world. In USER32, you can use a global table to implement shortcut keys. The table contains all the shortcut keys you want to support (CTRL + N ing to "new "). In the Application scheduler, you can call translateaccelerator, which explores input messages in USER32 and determines whether any messages match registered shortcuts. In WPF, the above content does not work, because the system is completely "composable"-any element can process and use any shortcut keys. Using this two-phase model for input will allow the component to implement its own translateaccelerator ".

To further deepen this function, uielement also introduces the commandbindings concept. The WPF command system allows developers to define features in the form of command endpoints (an icommand function. Command binding allows the element to define the ing between the input script (CTRL + n) and the command ("new. Input scripts and command definitions are extensible and can be linked together during use. This makes some operations (for example, allowing end users to customize the Key Binding they want to use in the Application) irrelevant.

This topic focuses on the functions centrally implemented by the presentationcore function of WPF. When WPF is generated, a clear division between the basic part (such as the Protocol with the layout of measure and arrange) and the framework part (such as the implementation of a specific layout of the grid) is expected. The goal is to provide a low-level scalability point in the stack, which allows external developers to create their own frameworks as needed.

System. Windows. frameworkelement

You can view frameworkelement in two different ways. It introduces a set of policies and custom items to subsystems in the lower layer of WPF. It also introduces a new set of subsystems.

The main policy introduced by frameworkelement is about application layout. Frameworkelement is generated based on the basic layout protocols introduced by uielement, and the concept of "slots" is added, so that the layout maker can conveniently have a set of Attribute-oriented consistent layout semantics. Attributes such as horizontalalignment, verticalignment, minwidth, and margin make all components derived from frameworkelement have consistent behavior in the layout container.

Using frameworkelement, many functions in the WPF core layer can be more convenient for API exposure. For example, frameworkelement provides direct access to the animation through the beginstoryboard method. Storyboard provides a way to write scripts for multiple animations for a set of attributes.

The two most important elements introduced by frameworkelement are data binding and style.

Users who have used Windows Forms or ASP. NET to create an Application User Interface (UI) should be familiar with the Data Binding subsystem in WPF. In each of the above systems, you can express in a simple way that you want to bind one or more attributes of a given element to a piece of data. WPF fully supports attribute binding, transformation, and list binding.

One of the most noteworthy features of Data Binding in WPF is the introduction of data templates. Using a data template, you can specify the visualization method of a data segment in a declarative manner. You can change the problem direction and let the data determine the display content to be created without creating a custom user interface that can be bound to the data.

Styles are actually lightweight data bindings. With styles, You can bind a group of attributes of a shared definition to one or more instances of an element. You can apply a style to an element by explicitly referencing it (setting the style attribute) or implicitly associating the style with the CLR type of the element.

System. Windows. Controls. Control

The most important function of a widget is templated. If you think of a WPF combination system as a Retained Mode rendering system, the control can be templated to describe its presentation in a parameterized declarative manner. Controltemplate is actually a script used to create a group of child elements and bind it to the properties provided by the control.

Control provides a set of common attributes, such as foreground, background, and padding. template creators can use these common attributes to customize the display of controls. Controls provide a data model and an interactive model. The interactive model defines a set of commands (such as window "close") and binding to the input pen potential (such as clicking the Red Cross in the window ). The data model provides a set of attributes for customizing the interaction model or custom display (determined by the template ).

The data model (attribute), interactive model (command and event), and display model (Template) are divided so that you can completely customize the appearance and behavior of the control.

The most common control data model is the content model. If you view a control such as a button, you will see that it has an attribute named "content" of the object type. In Windows Forms and ASP. NET, this attribute is usually a string-however, this limits the type of content you can enter in the button. The content of a button can be a simple string, a complex data object, or an entire element tree. If it is a data object, you can use a data template to construct the display content.

Summary

WPF is designed to help you create a dynamic data-driven demo system. Each part of the system can be created through the property set of the driving behavior. Data Binding is a basic part of the system and is integrated in each layer.

A traditional application creates a display content and binds it to some data. In WPF, all aspects of control content and display content are generated by some type of data binding. By creating a composite control inside the button and binding it to the content attribute of the button, the text in the button is displayed.

You should be very familiar with developing WPF-based applications. In this example, setting properties, using objects, and binding data are very similar to using Windows Forms or ASP. NET. If you have a deeper understanding of the WPF architecture, you can create a wider array of applications that fundamentally regard data as the core driving force of your applications.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.