避免不必要地使用断字
通过将该对象的 IsHyphenationEnabled 属性设置为 true
,可以启用此功能。 但是,启用此功能会导致 WPF 启动组件对象模型(COM)互操作性,这会影响应用程序性能。 除非需要,否则不建议使用自动断字功能。
画笔的不透明度与元素的不透明度
Brush使用 设置 元素Fill的Stroke或 时,最好设置Brush.Opacity值,而不是设置元素的属性。 Opacity 修改元素的属性Opacity可能会导致WPF创建临时曲面。
BitmapScalingMode
为任何位图的比例设置动画时,默认的高质量图像重采样算法有时会消耗足够的系统资源,从而导致帧速率下降,从而有效地导致动画断断续续。 通过将BitmapScalingModeRenderOptions对象的属性设置为LowQuality,可以在缩放位图时创建更平滑的动画。 LowQuality模式告诉渲WPF染引擎在处理图像时从质量优化算法切换到速度优化算法。
// Set the bitmap scaling mode for the image to render faster. RenderOptions.SetBitmapScalingMode(MyImage, BitmapScalingMode.LowQuality);
Image
如果应用程序要求显示缩略图,请考虑创建一个缩略版图像。 默认情况下,WPF 会加载图像并将其解码到最大尺寸。 如果仅需要缩略版本图像,WPF 会先将图像解码为完整尺寸,然后将其缩放为缩略图大小,这个过程是多余的。 若要避免此多余的过程,可请求 WPF 将图像解码到缩略图大小,或者请求 WPF 加载缩略图大小的图像。
// Create Image Element Image myImage = new Image(); myImage.Width = 200; // Create source BitmapImage myBitmapImage = new BitmapImage(); // BitmapImage.UriSource must be in a BeginInit/EndInit block myBitmapImage.BeginInit(); myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg"); // To save significant application memory, set the DecodePixelWidth or // DecodePixelHeight of the BitmapImage value of the image source to the desired // height or width of the rendered image. If you don't do this, the application will // cache the image as though it were rendered as its normal size rather then just // the size that is displayed. // Note: In order to preserve aspect ratio, set DecodePixelWidth // or DecodePixelHeight but not both. myBitmapImage.DecodePixelWidth = 200; myBitmapImage.EndInit(); //set image source myImage.Source = myBitmapImage;
StreamGeometry
StreamGeometry 是轻量替代PathGeometry对于创建几何形状。 使用StreamGeometry何时需要描述复杂几何,但不是希望支持数据绑定、 动画或修改的开销。与使用许多单个PathGeometry对象相比,性能更好。
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; namespace SDKSample { // Use StreamGeometry with StreamGeometryContext to define a triangle shape. public partial class StreamGeometryTriangleExample : Page { public StreamGeometryTriangleExample() { // Create a path to draw a geometry with. Path myPath = new Path(); myPath.Stroke = Brushes.Black; myPath.StrokeThickness = 1; // Create a StreamGeometry to use to specify myPath. StreamGeometry geometry = new StreamGeometry(); geometry.FillRule = FillRule.EvenOdd; // Open a StreamGeometryContext that can be used to describe this StreamGeometry // object's contents. using (StreamGeometryContext ctx = geometry.Open()) { // Begin the triangle at the point specified. Notice that the shape is set to // be closed so only two lines need to be specified below to make the triangle. ctx.BeginFigure(new Point(10, 100), true /* is filled */, true /* is closed */); // Draw a line to the next specified point. ctx.LineTo(new Point(100, 100), true /* is stroked */, false /* is smooth join */); // Draw another line to the next specified point. ctx.LineTo(new Point(100, 50), true /* is stroked */, false /* is smooth join */); } // Freeze the geometry (make it unmodifiable) // for additional performance benefits. geometry.Freeze(); // Specify the shape (triangle) of the Path using the StreamGeometry. myPath.Data = geometry; // Add path shape to the UI. StackPanel mainPanel = new StackPanel(); mainPanel.Children.Add(myPath); this.Content = mainPanel; } } }
将 IList 绑定到 ItemsControl 而非 IEnumerable
如果在将 对象绑定 为IList<T>对象或IEnumerableItemsControl对象之间可以选择,请选择IList<T>该对象。 绑定IEnumerable到ItemsControl强制WPF以创建包装IList<T>对象,这意味着您的性能受到第二个对象的不必要的开销的影响。
容器回收
在从继承的控件的 .NET Framework 3.5 SP1 中添加的 UI 虚拟化优化 ItemsControl 是容器回收, 这还可以提高滚动性能。 当 ItemsControl 填充使用 UI 虚拟化的时,它会为滚动到视图中的每个项创建一个项容器,并为每个滚动到视图之外的项销毁项容器。 容器回收使控件可以为不同数据项重复使用现有项容器,以便在用户滚动时不会不断创建和销毁项容器 ItemsControl 。 可以通过将附加属性设置为来选择启用项目回收 VirtualizationMode Recycling 。
延迟滚动
默认情况下,用户拖动滚动条上的滚动块时,内容视图会不断更新。 如果控件中滚动较慢,请考虑使用延迟滚动。 在延迟滚动中,仅在用户释放滚动块时才会更新内容。
若要实现延迟滚动,请将 IsDeferredScrollingEnabled 属性设置为 true
。 IsDeferredScrollingEnabled是附加属性,可在 ScrollViewer ScrollViewer 其控件模板中设置的任何控件上进行设置。
下表列出了显示数据的常见控件及其性能功能支持。 有关如何启用这些功能的信息,请参阅先前章节。
控制 | 虚拟化 | 容器回收 | 延迟滚动 |
---|---|---|---|
ComboBox | 可启用 | 可启用 | 可启用 |
ContextMenu | 可启用 | 可启用 | 可启用 |
DocumentViewer | 不可用 | 不可用 | 可启用 |
ListBox | 默认 | 可启用 | 可启用 |
ListView | 默认 | 可启用 | 可启用 |
TreeView | 可启用 | 可启用 | 可启用 |
ToolBar | 不可用 | 不可用 | 可启用 |
备注