WPF in the Bible note three

Source: Internet
Author: User

1 field: A variable that is encapsulated in a class.
Method: A function encapsulated in a class.
Members: Fields and methods in a class can be divided into static and non-static members.
Static fields have only one copy in memory.
A non-static field is a copy of each instance.
Method whether static or not, there is only one copy in memory.
2. CLR Properties
The practice of exposing field data directly to the outside world is insecure, and we want the object to be able to determine if the value of the field being written is correct. The set and get methods are provided to read the private field, which is the secure wrapper for the private field. The. Net framework combines the Get/set method into a property, also known as a CLR property.
The compilation result of the property is actually two methods (Get/set), with only one copy in memory and no additional memory burden.
3. Dependency properties
One can have no value of its own and be able to derive a property from the data source by binding it.
4. Dependent objects (Dependency object)
objects are created without the space used to store the data, leaving only the ability to obtain the default value when data is needed, to borrow other object data, or to allocate space in real time, which is achieved by relying on the Dependency property.
All UI controls in WPF are dependent objects.
5. A dependency property wrapper (wrapper) is a CLR property. The wrapper exposes the dependency property to the outside world in the form of instance properties, so that a dependency property becomes the path to the data source.
6. Customize a dependency object and declare a dependency property.

1  Public classStudent:dependencyobject2 {3      PublicStudent ()4     {5             6     }7         8      Public Static ReadOnlyDependencyProperty nameproperty=9Dependencyproperty.register ("Name",typeof(string),typeof(Student));Ten}

7, directly use the above dependency property. Read and write the value of a dependency property through the GetValue and SetValue methods of the dependent object.

1 Student stu=new  Student (); 2 Stu. SetValue (Student.nameproperty,this. TextBox1.Text); 3 this. textbox2.text= (string) stu. GetValue (Student.nameproperty);

8. Direct use of the GetValue and SetValue methods is slightly inconvenient, so in most cases we will add a CLR attribute wrapper for the dependency property. With this wrapper, the dependent object has a binding Path for exposing the data.

 1  public  string   Name  2  3  get  {return  (string   4  { SetValue (Nameproperty,value);}  5 } 

The dependency property comes with the norifypropertychanged feature.
9, declare the dependency property of the snippet: input PROPDP, press two times in a row tab key, the standard dependency properties of the code snippet appears.
10. The process of creating and registering a dependency property:
Create a dependent DependencyProperty instance that generates hashcode with its CLR property name and host type name, Finally, the hashcode and DependencyProperty instances are stored as key-value to the global hashcode named Propertyfromname. Finally, the resulting DependencyProperty instance is returned as a return value.
Each DependencyProperty instance has a unique globalindex int type attribute, which is the hash value of the DependencyProperty instance, which can be retrieved directly by this value.
11. How dependency Properties Store values
Each DependencyObject instance comes with an array of effectivevalueentry types, The value of the Propertyindex property for each effectivevalueentry is the globalindex of the DependencyProperty instance.
When the value of a dependency property is to be read, the algorithm retrieves the value from the effectivevalueentry[] array, and if there is no value, the default value of the dependency property is returned.
The dependency Property object that is decorated by the static readonly keyword is used to retrieve the true property value instead of the stored value.
12. Additional Properties
Snippet:propa
Declare an additional property:

1     classSchool:dependencyobject2     {3 4          Public Static intgetgrade (DependencyObject obj)5         {6             return(int) obj. GetValue (gradeproperty);7         }8 9          Public Static voidSetgrade (DependencyObject obj,intvalue)Ten         { One obj. SetValue (gradeproperty, value); A         } -  -         //Using a DependencyProperty as the backing store for Grade. This enables animation, styling, binding, etc ... the          Public Static ReadOnlyDependencyProperty Gradeproperty = -Dependencyproperty.registerattached ("Grade",typeof(int),typeof(Ownerclass),NewPropertyMetadata (0)); -  -}

Using attached properties:

1 New Student (); 2 6 ); 3 int grade = School.getgrade (stu);

The essence of an attached property is a dependency property, or you can use a binding to rely on data on other objects.
13. Routed event (RoutedEvent)
Like dependency properties, each routed event has its own CLR event.
Ways to listen for and handle routed events:
C#:

this. Grid. AddHandler (button.clickevent,new Routedeventhandler (this. buttonclicked));

Xaml:

<x:name= "GridRoot"  button.click= "buttonclicked">  </Grid>

14, the RoutedEventArgs two attributes
The source of the message on the Source:logicaltree
The source of the message on the Originalsource:visualtree
15. Additional Events
The host of the attached event does not have an interface element for the visual entity. For example, design a student class that fires a routed event when the property value of the student instance changes. In order to send routed events, you can borrow the RaiseEvent () method of a UI element such as a button.
To define additional events:

1     classStudent2     {3          Public Static ReadOnlyRoutedEvent namechangedevent =eventmanager.registerroutedevent4("namechanged", Routingstrategy.bubble,typeof(Routedeventhandler),typeof(Student));5 6          Public Static voidAddnamechangedhandler (DependencyObject D, Routedeventhandler h)7         {8UIElement E = d asUIElement;9             if(E! =NULL)Ten             { One E.addhandler (Student.namechangedevent, h); A             } -         } -  the          Public Static voidRemovenamechangedhandler (DependencyObject D, Routedeventhandler h) -         { -UIElement E = d asUIElement; -             if(E! =NULL) +             { - E.removehandler (Student.namechangedevent, h); +             } A         } at          Public intId {Get;Set; } -          Public stringName {Get;Set; } -}

16. The routing of the attached event the first station is the element that fires it (as in the example above)
17. In fact, additional events are generally defined in helper classes such as binding, Mouse, keyboard, and so on.
18. Routing Commands (RoutedCommand)
Use commands to avoid writing your own code to determine if a button is available and to add shortcut keys.
The command target sends routed events CanExecute and executed, and so on, and these routed events are passed up along the UI element tree and captured by commandbinding.

WPF in the Bible note three

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.