如何提高wpf效率 转载

WPF Performance Tips

Windows Presentation Foundation provides a very confortable way to develop rich user experiences. A drop shadow for example can added by inserting two simple lines of XML. But this simplicity can also mislead us to overuse them. This leads to performance issues. The following tipps may help you to avoid or fix them.

WPF 让我们很容易的开发富用户体验的程序。比如阴影效果可以用两行XML代码实现。但这可能会误导我们过度使用他们,而导致性能问题,下面有些Tips可以帮我们改善WPF程序性能。

Dispatch expensive calls either within the UI thread with a lower DispatcherPriority by callingDispatcher.BeginInvoke() or to a background thread by using a BackgroundWorker to keep the UI responsive.

当程序需要长时间进行后台操作的时候,使用Dispatcher.BeginInvoke()保持程序响应。

Fix binding errors because they consume a lot of time, trying to resolve the path error, including searching for attached properties. You can find them by looking for System.Windows.Data Error in the Visual Studio output log.

修复binding错误。binding上的错误会消耗很多时间,程序会试图解决path的错误,包括查找附加属性。你可以通过ststem.windows.data.error找到binding错误。

Reduce the number of visuals by removing unneeded elements, combining layout panels and simplifying templates. This keeps the memory footprint small and improves the rendering performance.

通过移出不必要的元素,合并layout panels,简化templates来减少可视化树的层次。这可以保证第内存使用,而改变渲染性能。

Prevent Software Rendering. The use of transparent windows by setting AllowsTransparency to true or using old BitmapEffects can cause WPF to render the UI in software on Windows XP, which is much slower.

避免使用软件渲染。设定Windows的AllowsTransparency为true或者使用老的BitmapEffects可以导致WPF使用软件渲染UI从而导致程序变慢。

Load resources when needed. Even thow it's the most comfortable way to merge all resources on application level it can also cost performance by loading all resources at startup. A better approach is to load only often used resources and load the other on view level.

将资源定义在使用的地方。虽然把所有资源放到应用程序级很方便,但这样会迫使程序在开始时加载所有资源。更好的办法是在用到的地方加载。

Virtualize lists and views by using a VirtualizingStackPanel as ItemsPanel for lists. This only creates the visible elements at load time. All other elements are lazy created when they get visible. Be aware that grouping or CanContextScrol="True" prevents virtualization!

使用VirtualizingStackPanel 虚拟化列表作为lists中的ItemsPanel和视图.VirtualizingStackPanel只会在lists中的item可见时加载。但注意要分组和设定CanContextScrol为True可以组织虚拟化。

Enable Container Recycling. Virtualization brings a lot of performance improvements, but the containers will be disposed and re created, this is the default. But you can gain more performance by recycle containers by setting VirtualizingStackPanel.VirtualizationMode="Recycling"

虚拟话可以带来很大的性能提升,但是默认情况下容器会被重新创建。我们可以通过设定VirtualizingStackPanel.VirtualizationMode="Recycling" 来得到更多的性能提升。

Freeze Freezables by calling Freeze() in code or PresentationOptions:Freeze="true" in XAML. This reduces memory consumption and improves performance, because the system don't need to monitor for changes.

通过在代码中调用Freeze()或者在Xmal中设定PresentationOptions:Freeze="true"来冻结可以冻结的控件。由于这样系统不必监听该控件的变化,所以可以带来性能的提升。

Disable Assembly localization if you don't need it. By using the [NeutralResourcesLanguageAttribute].This prevents an expensive lookup for satelite assemblies

不太理解……高手可以帮忙翻译。

Lower the framerate of animations by setting Storyboard.DesiredFrameRate to lower the CPU load. The default is 60 frames/second

降低动画的帧率。大多数动画不需要高帧率,而系统默认为60frames/sec,所以可以设定Storyboard.DesiredFrameRate 为更低值。

Use StreamGeometries instead of PathGeometries if possible to draw complex 2D geometries, because they are much more efficient and consume less memory.

尽可能使用StreamGeometries 代替PathGeometries ,因为它可以降低内存占用,更高效。

 

PresentationOptions:Freeze="True" 冻结不会进行编辑的资源。

在MSDN上看到的这个说明“对象行为”,上面说到,每一个SolidColorBrush占用的大小,当冻结的时候,需要212字节,未冻结的则需要972字节,即使单位很小,但是差距还是非常大的,通常我们写程序的时候,这类的对象都不会想到冻结它,当然,冻结的对象也意味着不能被修改,冻结等于只读。当SolidColorBrush当成资源对象的时候,这个对象几乎就是不会被修改的。所以,我们有必要在这种情况下将该对象冻结,已达到节省资源和优化程序的目的。克隆的对象,就相当于对当前对象的解冻行为。

posted @ 2012-12-17 10:00  ZN大叔  阅读(1505)  评论(0编辑  收藏  举报