Optimizing Performance: Other Recommendations

This topic provides performance recommendations in addition to the ones covered by the topics in the Optimizing WPF Application Performance section.

This topic contains the following sections:

Opacity on Brushes Versus Opacity on Elements

When you use a Brush to set the Fill or Stroke of an element, it is better to set the Brush.Opacity value rather than the setting the element's Opacity property. Modifying an element's Opacity property can cause WPF to create a temporary surface.

The NavigationWindow object derives from Window and extends it with content navigation support, primarily by aggregating NavigationService and the journal. You can update the client area of NavigationWindow by specifying either a uniform resource identifier (URI) or an object. The following sample shows both methods:

private void buttonGoToUri(object sender, RoutedEventArgs args)
    navWindow.Source = new Uri("NewPage.xaml", UriKind.RelativeOrAbsolute);

private void buttonGoNewObject(object sender, RoutedEventArgs args)
    NewPage nextPage = new NewPage();
    navWindow.Content = nextPage;
Private Sub buttonGoToUri(ByVal sender As Object, ByVal args As RoutedEventArgs)
    navWindow.Source = New Uri("NewPage.xaml", UriKind.RelativeOrAbsolute)
End Sub

Private Sub buttonGoNewObject(ByVal sender As Object, ByVal args As RoutedEventArgs)
    Dim nextPage As New NewPage()
    navWindow.Content = nextPage
End Sub

Each NavigationWindow object has a journal that records the user's navigation history in that window. One of the purposes of the journal is to allow users to retrace their steps.

When you navigate using a uniform resource identifier (URI), the journal stores only the uniform resource identifier (URI) reference. This means that each time you revisit the page, it is dynamically reconstructed, which may be time consuming depending on the complexity of the page. In this case, the journal storage cost is low, but the time to reconstitute the page is potentially high.

When you navigate using an object, the journal stores the entire visual tree of the object. This means that each time you revisit the page, it renders immediately without having to be reconstructed. In this case, the journal storage cost is high, but the time to reconstitute the page is low.

When you use the NavigationWindow object, you will need to keep in mind how the journaling support impacts your application's performance. For more information, see Navigation Overview.

Hit Testing on Large 3D Surfaces

Hit testing on large 3D surfaces is a very performance intensive operation in terms of CPU consumption. This is especially true when the 3D surface is animating. If you do not require hit testing on these surfaces, then disable hit testing. Objects that are derived from UIElement can disable hit testing by setting the IsHitTestVisible property to false.

CompositionTarget.Rendering Event

The CompositionTarget.Rendering event causes WPF to continuously animate. If you use this event, detach it at every opportunity.

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.

Configure Font Cache Service to Reduce Start-up Time

The WPF Font Cache service shares font data between WPF applications. The first WPF application you run starts this service if the service is not already running. If you are using Windows Vista, you can set the "Windows Presentation Foundation (WPF) Font Cache" service from "Manual" (the default) to "Automatic (Delayed Start)" to reduce the initial start-up time of WPF applications.

See also