Use Controls with UI Virtualization When You Display Large Data
If you bind a combo box with large number of rows, it would make the rendering of the items in the combo box pretty slow. The slowness is caused by the time taken to compute the position of each item in such a situation. With WPF, you can defer this behavior. This is called as UI Virtualization and it simply refers to the item container generation only on its visibility. To achieve this, you need to set the
IsVirtualizing property to true for such controls. For example, Listbox is a control which often is bound with a large set of data and is a primary candidate for UI virtualization. Other examples include Combobox, ListView, and TreeView.
Use Deferred Scrolling to Enrich User Experience
Scrolling the datagrid or a listbox, it often slows down the entire application because of the continuous updates that are invoked forcibly due to the scrolling. This is the behavior by default. In such cases, we can use the “Deferred Scrolling” property of the controls to enrich the user experience. All you have to do is set the
IsDeferredScrollingEnabled attached property to true.
Use Font Cache Service to Improve the Start-up Time
The WPF applications can share the font data among them. This can be achieved through the Windows service named
PresentationFontCache Service. This is automatically started with windows. You can find this service under the Services panel and ensure that is started.
Use Unloaded Event to Unload Unnecessary Animations
Animations definitely take a toll on the resource utilization, and would also increase if they are not disposed of in the right manner. You should dispose of them when you consider them useless. Failing to do so, will consume precious resources until the Garbage Collector kicks in. For example to remove a story board, use the
Remove method of the Story board in the
Use Container Recycling to Increase Performance
You can increase the performance by recycling the containers that perform the Virtualization. The following code snippet sets the ViruatlizationMode to “Recycling” which allows you to gain more performance. This forces the container objects to be reused when the user scrolls and reaches another item.
Predict the Graphics Capability and Provide Features
RenderCapability.Tier property to determine if the machine supports hardware acceleration, partial hardware acceleration, or no acceleration. After you determine this, you can then selectively choose those capabilities that work best on the user’s hardware.
Lower The Bitmapscalingmode to Render Images Faster
You can lower the consumption of resources on a machine when you have certain animations being processed by your WPF application. To do this, you need to use the
BitmapScalingMode property of the
RenderOptions object. You would need to use the “LowQuality” option from the
BitMapScalingMode enum to ensure that the image is processed using the speed algorithm instead of the default high-quality image re-sampling algorithm.
Use The Right Elements in the Right Places
We need to use the right elements in the right places. Avoid
UIElements as child or nested controls when you build the tree. For example, in the Flow Document, we often use the
TextBlock element inside the
<FlowDocument> <Paragraph> <TextBlock>some text</TextBlock> </Paragraph> </FlowDocument>
Instead of doing above, we can rewrite the XAML content as shown below. The
Run element is not a
UIElement and involves lesser overhead while rendering.
< FlowDocument> <Paragraph> <Run>some text</Run> </Paragraph>< /FlowDocument>
A similar example is the usage of Content property of the Label Control. If this content is updated more than once in its lifetime and is a string, this databinding procedure can hinder the application’s performance. Since the Content is a string, it will be discarded and recreated during the databinding. Use a
TextBlock in such cases and data bind to its
Text property. The unnecessary elements in the visual tree also contribute to the slowness of WPF applications. You should ideally combine the layout and optimize the default control templates.
Increase Usage of Static Resources
Static resources are pre-defined resources that can be hooked to XAML properties. It is similar to Compile-time tie up’s and does not have a performance impact. The dynamic resources on the other hand involve a run-time seek and also construction of such objects which lead to performance impacts. Doing it this way also enables you to share common resources like brushes.
Avoid using ScrollBarVisibility=Auto
Whenever possible, avoid using the ScrollBarVisibility.Auto value for the HorizontalScrollBarVisibility and VerticalScrollBarVisibility properties. These properties are defined for RichTextBox, ScrollViewer, and TextBox objects, and as an attached property for the ListBox object. Instead, set ScrollBarVisibility to Disabled, Hidden, or Visible.
The Auto value is intended for cases when space is limited and scrollbars should only be displayed when necessary. For example, it may be useful to use this ScrollBarVisibility value with a ListBox of 30 items as opposed to a TextBox with hundreds of lines of text.
Implement a Splash Screen
In cases where there is a significant, unavoidable delay between starting an application and displaying the first UI, optimize the perceived startup time by using a splash screen. This approach displays an image almost immediately after the user starts the application. When the application is ready to display its first UI, the splash screen fades. Starting in the .NET Framework 3.5 SP1, you can use the SplashScreen class to implement a splash screen. You can also implement your own splash screen by using native Win32 graphics. Display your implementation before the Run method is called.