First off, let me say that this list isn't mine. I saw it on a presentation and wanted to make sure I had it to reference later. Enjoy!
- Reduce unnecessary invocations of the layout pass -- update a Transform rather than replacing it
- Use the most efficient Panel where possible -- don't use Grid if you need is Canvas
- Use Drawing which are cheaper than Shape objects
- Use StreamGeometry, which is a light-weight alternative to PathGeometry for creating geometric shapes
- Use DrawingVisual to render shapes, images, or text
- If you need thumbnail images, create a reduced-sized version of the image
Note: By default, WPF loads your image and decodes it to its full size - Decode the image to desired size and not to the default size
- Combine images into a single image, such as a film strip composed of multiple images
- Set BitmapScalingMode to LowQuality for smother animating when scaling bitmap to instruct WPF to use speed-optimized algorithm
- Set CachingHint when possible, to conserve memory and increase perf
- Brush selection is critical to good performance; don’t use a VisualBrush or DrawingBrush when an ImageBrush or SolidColorBrush would be sufficient
- To draw complex, static vector content that may need to be repeatedly re-rendered, consider rendering it into a RenderTargetBitmap first and drawing it as an image instead
Note: Rendering images is usually faster than rendering vector content - Remember to clear event handlers to prevent object from remaining alive (see this post on finding memory leaks)
- Use Freezable objects when possible to reduce working set and improve perf
- Remember to virtualize
- Share resources -- if you use custom controls, define control resources at the application or window level or in the default theme for the custom controls
- Share a brush w/o copying by defining it as a resource
- Use static vs. dynamic resources
- Don’t use FlowDocument element when all you need is a Label or TextBlock
- Avoid binding to Label.Content property, use TextBlock instead if source is frequently changes.
Note: String is immutable, so Label object’s ContentPresenter must regenerate new content - Don't convert CLR object to XML if the only purpose is binding
Note: Binding to XML content is slower than CLR objects - Always prefer binding to an ObservableCollection<T> vs. List<T>
- When a brush is used to set the Fill or Stroke of an element, use the brush's Opacity value rather than element's Opacity property
- Disable hit testing (IsHitTestVisible = false) when possible
Note: Hit test on large 3D surfaces is expensive
If you're hungry for more, feel free to dig thru the Optimizing WPF Application Performance section on MSDN.
No comments:
Post a Comment