避免不必要地使用断字
除非需要,否则不建议使用自动断字功能。
画笔的不透明度与元素的不透明度
WPF创建临时曲面。
BitmapScalingMode
WPF染引擎在处理图像时从质量优化算法切换到速度优化算法。
// Set the bitmap scaling mode for the image to render faster. RenderOptions.SetBitmapScalingMode(MyImage, BitmapScalingMode.LowQuality);
Image
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
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>对象,这意味着您的性能受到第二个对象的不必要的开销的影响。
容器回收
Recycling 。
延迟滚动
在延迟滚动中,仅在用户释放滚动块时才会更新内容。
ScrollViewer 其控件模板中设置的任何控件上进行设置。
有关如何启用这些功能的信息,请参阅先前章节。
| 控制 | 虚拟化 | 容器回收 | 延迟滚动 |
|---|---|---|---|
| ComboBox | 可启用 | 可启用 | 可启用 |
| ContextMenu | 可启用 | 可启用 | 可启用 |
| DocumentViewer | 不可用 | 不可用 | 可启用 |
| ListBox | 默认 | 可启用 | 可启用 |
| ListView | 默认 | 可启用 | 可启用 |
| TreeView | 可启用 | 可启用 | 可启用 |
| ToolBar | 不可用 | 不可用 | 可启用 |
备注