WPF Learning (III)-dependency attributes and wpf learning dependency attributes
When I was learning about WPF, I was reading a book called "go deep into WPF. The dependency and additional attributes are being discussed on the entire 20 pages. I still don't understand them after reading them several times. It's really depressing.
In the previous example of binding to WPF, dependency attributes are used.
// As the bound target class, the class must be derived from DependencyObject // This defined class can meet the type requirements of the first parameter of the SetBinding method // an additional dependency attribute must be defined, used to meet the second parameter requirements of the SetBinding method // use the DependencyObject derived Methods GetValue and SetValue to control the storage of attributes and take public class UIClass: System. windows. dependencyObject {public string MyText {get {return (string) GetValue (MyTextProperty);} set {SetValue (MyTextProperty, value) ;}} public static readonly System. windows. dependencyProperty MyTextProperty = System. windows. dependencyProperty. register ("MyText", typeof (string), typeof (UIClass ));}Example of dependency attributes
In this example, the dependency attribute must be used to synchronize the attribute values of the presentation layer to the data layer. The second parameter of the SetBinding method must be of the dependent attribute type.
In the previous article, we used Winform to implement data synchronization, but the synchronization link creation process was insufficient. However, WPF solves this problem by using dependency attributes.
So how does the dependency attribute achieve this magic? Previously, I said that Winform implementation is very difficult, but it is not equal to implementation. This proposition is as follows:
Known: How does one obtain the attribute value of an instance and its attribute name string? Note that this requirement is not implemented in the coding stage, but in the running stage.
It should be such a method: GetObjectPropertyValue (object o, string propertyName) // return the attribute value based on the name of the input object and a property.
Reflection is not allowed !!! I cannot find a basic method to implement such a simple requirement ...... This reminds me that when I first came into contact with programming, I always used an alternative method to implement some standard functions. The solution is to retrieve a value by name, and create a name + value table to save/retrieve the attribute value by name. Well, you need to refresh the sense of presence for some instances. Add one to the table to describe the instance.
Public class PropertyContainer {static List <InstanceNameValue> lstINV = new List <InstanceNameValue> (); public void Register (int instance, string propertyName) {lstINV. add (new InstanceNameValue () {InstanceHashCode = instance, PropertyName = propertyName});} public object GetValue (object o, string propertyName) {foreach (InstanceNameValue item in lstINV) {if (o. getHashCode () = item. instanceHashCode & propertyName = item. propertyName) {return item. value ;}} return null ;}// instance, attribute name, Value class InstanceNameValue {public int InstanceHashCode; public string PropertyName; public object Value ;}Property container
Public class CheatClass {public string CheatProperty {get {return (string) PropertyContainer. GetValue (this, "WhatEverName ");}}}Hypocritical classes and false attributes
Call the PropertyContainer. GetValue method to pass in the instance and attribute name, and read and write the attribute value corresponding to this instance. It seems that this method can be used. In fact, this is a lie. I have never really read/write the attribute value of this instance (if it is actually instantiated by the class and attribute in the traditional sense ), I have been using a replacement to save the value I want to save in the attribute field. Even the class where the instance is located does not have this field at all.
When you understand this, you are very angry. After so many years of object-oriented processing, the attributes of classes no longer need to be stored by defining fields. They are clustered into a pile at runtime and put in a pot. The object is no longer the object. It can retain only the 12 bytes and give a sense of presence. Are there any relationships between classes? Are there any relationships between objects ??!!! What architecture is designed and what data structure is involved. Define an empty class, run it, and add attributes as needed.
A child once designed the data table structure in this way: The table name, field name, record number, and field value. He also showed off his own design of a common table structure. I slapped my hand and learned the data structure from my team! Today, Microsoft is drawing back a slap in the face. net is also used for such a capricious structure !!!
Angry, today is the end ......