Summary of WPF performance optimization experience

Source: Internet
Author: User

WPF performance optimization 1. Rendering tier

1. Based on Different hardware configurations, WPF uses different rendering tier for rendering. Pay special attention to the following situations, because in these cases, hardware acceleration is not allowed even in rendering Tier 2. (Incomplete, please refer to the SDK for other items)

WPF performance optimization II. layout and design

1. Try to use canvas and other simple layout elements as much as possible, and use less complex elements such as grid or stackpanel. The more complicated the performance, the higher the overhead.

2. When creating a logical or visual tree, follow the top-down principle.

WPF performance optimization III. Images

1. When performing image animation processing (such as resizing), you can use this statement renderoptions. setbitmapscalingmode (myimage, bitmapscalingmode. lowquality) to improve performance.

2. cachinghint can be used when tilebrush is used.

WPF performance optimization 4. Object Behavior

1. Access to CLR objects and CLR properties is more efficient than access to dependencyobject/dependencyproperty. Note that this refers to access. Do not confuse it with the previous binding. However, registering a property as dependencyproperty has many advantages: inheritance, data binding, and style.

WPF performance optimization 5. application resources

1. In a custom control, do not define Resources in the control's resourcedictionary, but at the window or application level. This is because every instance retains a copy of the resource in the control.

2. Try to use static resources, but do not use them blindly.

WPF performance optimization VI. Text

1. Use textblock or label when there are few words, and use flowdocument for a long time.

2. When using the elements textflow and textblock, if you do not need some features of textflow, you should consider using textblock because it is more efficient.

3. The cost of using uielement (such as textblock) in textflow is higher than that of using textelement (such as run). Avoid using textblock in flowdocument and use run instead.

4. The explicit Run Command in textblock is higher than the code without the Run Command.
5. binding the contentproperty of the label element and a string is less efficient than binding the text property of the string and textblock. Because the label discards the original string when updating the string and re-displays all the content. If the string does not need to be updated, it does not matter if you use label.

6. When textblock uses hyperlinks, it is more efficient to combine multiple hyperlinks.

7. When displaying a hyperlink, try to show the underline only when ismouseover is set to true. The code that keeps displaying the underline is much higher.

8. Try not to use unnecessary string connections.

WPF performance optimization 7. Data Binding

1. during data binding, if the bound data source is a CLR object and the property is also a CLR attribute, the Mechanism Implemented by the object CLR object during binding is different, the binding efficiency is also different.

A. The data source is a CLR object and the property is also a CLR property. The object implements the notification function in typedescriptor/propertychanged mode. The binding engine uses typedescriptor to reflect the source object. The efficiency is the lowest.

B. The data source is a CLR object and the property is also a CLR property. The object uses inotifypropertychanged to implement the notification function. The binding engine directly reflects the source object. The efficiency is slightly improved.

C. The data source is a dependencyobject and the property is a dependencyproperty. In this case, reflection is not required and you can bind it directly. The highest efficiency.

2. When a CLR object is large, for example, if there are 1000 attributes, try to break this object into many very small CLR objects. For example, it is divided into 1000 CLR objects with only one attribute.

3. When a CLR Object List (such as list) is displayed in the list (such as ListBox), ListBox dynamically reflects this change after the list object is modified. In this case, we should bind the dynamic observablecollection object. Instead of updating itemsource directly. The difference between the two lies in that directly updating itemsource will cause WPF to discard all the existing data in ListBox and then reload all the data from list. The use of observablecollection can avoid such a process of deleting all the items before reloading, which is more efficient.

4. Try to bind ilist instead of ienumerable to itemscontrol.

WPF performance optimization 8. Other performance recommendations

1. If you need to modify the opacity attribute of an element, modify the attribute of a brush and then use this brush to fill the element. Because directly modifying the opacity of an element forces the system to create a temporary Surface

2. When using navigationwindow, try to update the client area by object instead of Uri.

3. Try not to use scrollbarvisibility = auto

 

WPF performance optimization 1. Rendering tier

1. Based on Different hardware configurations, WPF uses different rendering tier for rendering. Pay special attention to the following situations, because in these cases, hardware acceleration is not allowed even in rendering Tier 2. (Incomplete, please refer to the SDK for other items)

WPF performance optimization II. layout and design

1. Try to use canvas and other simple layout elements as much as possible, and use less complex elements such as grid or stackpanel. The more complicated the performance, the higher the overhead.

2. When creating a logical or visual tree, follow the top-down principle.

WPF performance optimization III. Images

1. When performing image animation processing (such as resizing), you can use this statement renderoptions. setbitmapscalingmode (myimage, bitmapscalingmode. lowquality) to improve performance.

2. cachinghint can be used when tilebrush is used.

WPF performance optimization 4. Object Behavior

1. Access to CLR objects and CLR properties is more efficient than access to dependencyobject/dependencyproperty. Note that this refers to access. Do not confuse it with the previous binding. However, registering a property as dependencyproperty has many advantages: inheritance, data binding, and style.

WPF performance optimization 5. application resources

1. In a custom control, do not define Resources in the control's resourcedictionary, but at the window or application level. This is because every instance retains a copy of the resource in the control.

2. Try to use static resources, but do not use them blindly.

WPF performance optimization VI. Text

1. Use textblock or label when there are few words, and use flowdocument for a long time.

2. When using the elements textflow and textblock, if you do not need some features of textflow, you should consider using textblock because it is more efficient.

3. The cost of using uielement (such as textblock) in textflow is higher than that of using textelement (such as run). Avoid using textblock in flowdocument and use run instead.

4. The explicit Run Command in textblock is higher than the code without the Run Command.
5. binding the contentproperty of the label element and a string is less efficient than binding the text property of the string and textblock. Because the label discards the original string when updating the string and re-displays all the content. If the string does not need to be updated, it does not matter if you use label.

6. When textblock uses hyperlinks, it is more efficient to combine multiple hyperlinks.

7. When displaying a hyperlink, try to show the underline only when ismouseover is set to true. The code that keeps displaying the underline is much higher.

8. Try not to use unnecessary string connections.

WPF performance optimization 7. Data Binding

1. during data binding, if the bound data source is a CLR object and the property is also a CLR attribute, the Mechanism Implemented by the object CLR object during binding is different, the binding efficiency is also different.

A. The data source is a CLR object and the property is also a CLR property. The object implements the notification function in typedescriptor/propertychanged mode. The binding engine uses typedescriptor to reflect the source object. The efficiency is the lowest.

B. The data source is a CLR object and the property is also a CLR property. The object uses inotifypropertychanged to implement the notification function. The binding engine directly reflects the source object. The efficiency is slightly improved.

C. The data source is a dependencyobject and the property is a dependencyproperty. In this case, reflection is not required and you can bind it directly. The highest efficiency.

2. When a CLR object is large, for example, if there are 1000 attributes, try to break this object into many very small CLR objects. For example, it is divided into 1000 CLR objects with only one attribute.

3. When a CLR Object List (such as list) is displayed in the list (such as ListBox), ListBox dynamically reflects this change after the list object is modified. In this case, we should bind the dynamic observablecollection object. Instead of updating itemsource directly. The difference between the two lies in that directly updating itemsource will cause WPF to discard all the existing data in ListBox and then reload all the data from list. The use of observablecollection can avoid such a process of deleting all the items before reloading, which is more efficient.

4. Try to bind ilist instead of ienumerable to itemscontrol.

WPF performance optimization 8. Other performance recommendations

1. If you need to modify the opacity attribute of an element, modify the attribute of a brush and then use this brush to fill the element. Because directly modifying the opacity of an element forces the system to create a temporary Surface

2. When using navigationwindow, try to update the client area by object instead of Uri.

3. Try not to use scrollbarvisibility = auto

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.