Idle talk about wpf ii 6 (WPF performance optimization points)

Source: Internet
Author: User

While building a beautiful UI, we also need to focus on the performance of the application, especially for WPF. The following describes some useful performance optimizations In the MS documentation. For more information, see. This Post is not completely original and is summarized based on some documents.

1. When creating a logical tree, try to consider building the logic from the parent node to the child node. Because when a node of the logic tree changes (such as adding or deleting), its parent node and all child nodes will inspire Invalidation. We should avoid unnecessary Invalidation.

2. When we display a CLR Object List (such as List) in the List (such as ListBox), if we want to modify the List object, ListBox dynamically reflects this change. 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.

3. 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.

4. 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. So sometimes we can use the cache mechanism to accelerate the access speed when implementing DependencyProperty: See the following cache example:

Public static readonly DependencyProperty MagicStringProperty =
DependencyProperty. Register ("MagicString", typeof (string), typeof (MyButton), new PropertyMetadata (new PropertyInvalidatedCallback (callback), new GetValueOverride (MagicStringGetValueCallback )));

Private static void OnMagicStringPropertyInvalidated (DependencyObject d)
{
// Mark the cached data as invalid
(MyButton) d). _ magicStringValid = false;
}

Private static object MagicStringGetValueCallback (DependencyObject d)
{
// Call the cache accessor to obtain the value
Return (MyButton) d). MagicString;
}

// Private CLR accessors and local caches
Public string MagicString
{
Get
{
// When the current value is invalid, obtain the latest value and save it
If (! _ MagicStringValid)
{
_ MagicString = (string) GetValueBase (MagicStringProperty );
_ MagicStringValid = true;
}

Return _ magicString;
}
Set
{
SetValue (MagicStringProperty, value );
}
}

Private string _ magicString;
Private bool _ magicStringValid;

In addition, the registered DependencyProperty cannot be inherited by default. If you need to inherit the property, it will also reduce the efficiency of DependencyProperty value refreshing. When registering the DependencyProperty property, you should pass the DefaultValue to the parameters of the Register Method to set the default value, rather than in the constructor.

5. 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.

6. The explicit Run Command in TextBlock is higher than the code without the Run Command.

7. Using UIElement (such as TextBlock) in TextFlow requires a higher cost than using TextElement (such as Run.

8. 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.

9. When TextBlock uses HyperLinks, it is more efficient to combine multiple HyperLinks. Take a look at the two methods below, and the latter is highly efficient.

A,
<TextBlock Width = "600">
<Hyperlink TextDecorations = "None"> MSN Home </Hyperlink>
</TextBlock>
<TextBlock Width = "600">
<Hyperlink TextDecorations = "None"> My MSN </Hyperlink>
</TextBlock>

B,
<TextBlock Width = "600">
<Hyperlink TextDecorations = "None"> MSN Home </Hyperlink>
<Hyperlink TextDecorations = "None"> My MSN </Hyperlink>
</TextBlock>

10. It is related to the TextDecorations above. When displaying a hyperlink, try to show the underline only when IsMouseOver is True. The code that keeps displaying the underline is much higher.

11. 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.

12. If multiple elements use the same Brush, you should consider defining the Brush in the resource so that they can share a Brush instance.

13. 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.

14. When using a large 3D Surface in the system, disable it if you do not need the HitTest function of the Surface. The default HitTest takes a lot of CPU time for calculation. UIElement has the IsHitTestVisible attribute that can be used to disable the HitTest function.

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.