"Programming WPF" translation 9th Chapter 3. Custom Features

Source: Internet
Author: User
Tags inheritance

Once you have selected a base class, you will be designing an API for your control. Most WPF elements provide properties that expose most features, events, and commands as they gain extensive support from the framework, as well as ease of use of XAML. The WPF framework provides automatic support for routed event and commands, and its dependency property system provides data peninsula and animation support. Of course, you can also write a method--for a certain function, the method is the best way. (for example, the ListBox has a scrollIntoView method that ensures that a particular item is visible.) This is a convenient thing to do from the code. However, I prefer to use attributes, events, and commands in a reasonable place.

9.3.1 Property

. NET type system provides a standard way to define attributes for an object. It specifies a contract that provides a method accessor for get and set, but the implementation of these and the way in which the attribute values are stored are left to the developer. In WPF, elements typically use a dependency property system. NET provides a representative style property accessor, but these are simply a convenience for packaging dependent attributes (DP).

DP Systems add a lot of features-there's no standard. NET attribute is provided. For example, a DP inherits its value from the parent element. This differs from OO inheritance in that a DP inherits its characteristics from its base class (although the DP also supports the inheritance of oo meaning). Inheritance of property values is a more dynamic feature that allows properties to be set on a single cell and automatically propagate to all of its child elements. For example, all elements have a cursor property to control the mouse pointer. This attribute uses the inheritance of the value, meaning that once you see cursor on the element, all the child elements will automatically get the same cursor attribute value. (If you use Windows Forms, you will be familiar with this concept, where any element has the same characteristics.) )

The DP also automatically gets their values elsewhere. DP supports data binding and styling, which provide a mechanism for defining default values. The animation system also relies on DP, which uses a DP structure to instantly adjust the property values.

By implementing your element attributes, such as DP, you can not only get these features automatically, but the DP system also manages the storage of the values for you. You do not have to store property values for defining any of the field instances.

Storage Manager looks like a little thing, after all, how difficult is it to add a field to a class? Nevertheless, this feature provides surprising memory storage.

Simply inheriting from control, your element can support more than 40 attributes (plus any subordinate attributes) to change complexity, most of which seem to have a default value for most objects. If each element has its own set of fields to store these values, each element will occupy hundreds of bytes. A complex user interface may require thousands of bytes (even if the UI has a fairly simple structure, the visual tree can significantly increase the number of elements.) )

Most of the attributes of most elements either inherit from their parent classes or are set to their default values, and then use the elements to store these values in fields, which wastes hundreds of thousands of memory. A more advanced way of storing exposes the fact that most of the properties that are not set are valid. And as the memory is cheap, moving data out of the CPU is expensive. The CPU can convert the main memory faster than the data and execute the code more quickly. Only the memory cache can quite quickly keep up with the processor, and most modern processors typically have only hundreds of bytes of cache. Even high-end systems have only a few megabytes of cache. Saving hundreds of bytes can significantly improve performance.

With a DP system, we can make it process information more efficiently-by ordering only the property values that are set for display.

Finally, the DP system tracks the changes in values. This means that once any interested section wants to know when a property value changes, it can use the DP system registration notification. (data binding depends on the times.) We don't need to write any special code to make it happen. The DP System Manager stores our attribute values, so it knows when attributes change.

Any WPF custom element that you create will automatically support all DP, because FrameworkElement is indirectly derived from the DependencyObject base class. To define a new attribute on our custom element, we must create a new DependencyObject object in the element's static constructor. As a rule, we expose object attributes, sorted by a public static field in our class, as shown in example 9-1.

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.