提高 WPF UI 渲染速度的方法 [英] Ways to improve WPF UI rendering speed

查看:180
本文介绍了提高 WPF UI 渲染速度的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 WPF 应用程序的屏幕包含大量原始控件,则其渲染会变得缓慢.在这种情况下,除了添加更少的控件和使用更强大的显卡之外,还有哪些推荐的方法可以提高 WPF 应用程序的响应能力?

In case a screen of a WPF application contains lots of primitive controls, its rendering becomes sluggish. What are the recommended ways to improve the responsiveness of a WPF application in such a case, apart from adding fewer controls and using more powerful videocard?

有没有办法以某种方式使用屏幕外缓冲或类似的东西?

Is there a way to somehow use offscreen buffering or something like that?

推荐答案

我们的团队面临渲染性能问题.在我们的例子中,我们有大约 400 个运输单元,我们应该用很多细节(文本标签、特殊标记、不同的几何图形等)渲染每个单元的图表.

Our team was faced with problems of rendering performance. In our case we have about 400 transport units and we should render chart of every unit with a lot of details (text labels, special marks, different geometries etc.).

在我们的最初实现中,我们将每个图表拆分为基元,并通过绑定组成整个单元的图表.这是非常悲伤的经历.UI 反应极其缓慢.

In first our implementations we splitted each chart into primitives and composed whole unit's chart via Binding. It was very sad expirience. UI reaction was extremely slow.

因此我们决定为每个单元创建一个 UI 元素,并使用 DrawingContext 渲染图表.虽然这在性能方面要好得多,但我们花了大约一个月的时间来改进渲染.

So we decided to create one UI element per each unit, and render chart with DrawingContext. Although this was much better in performance aspect, we spent about one month improving rendering.

一些建议:

  1. 缓存所有内容.画笔、颜色、几何图形、格式化文本、字形.(例如我们有两个类:RenderToolsTextCache.每个单元的渲染过程地址到两个类的共享实例.所以如果两个图表有相同的文本,它的准备只执行一次.)
  2. Freeze Freezable,如果您打算长期使用它.尤其是几何图形.复杂的未冻结几何图形执行 HitTest 非常慢.
  3. 选择渲染每个基元的最快方式.例如,文本渲染大约有 6 种方式,但最快的是 DrawingContext.DrawGlyphs.
  4. 使用分析器发现热点.例如,在我们的项目中,我们缓存了几何图形并按需渲染它们.似乎不可能有任何改进.但有一天我们想,如果我们一次性渲染几何图形并缓存准备好的视觉效果会怎样?在我们的案例中,这种方法是可以接受的.我们单位的图表只有几个状态.当图表的数据发生变化时,我们为每个状态重建 DrawingVisual 并将它们放入缓存中.
  1. Cache everything. Brushes, Colors, Geometries, Formatted Texts, Glyphs. (For example we have two classes: RenderTools and TextCache. Rendering process of each unit addresses to shared instance of both classes. So if two charts have the same text, its preparation is executed just once.)
  2. Freeze Freezable, if you are planning to use it for a long time. Especially geometries. Complex unfreezed geometries execute HitTest extremely slow.
  3. Choose the fastest ways of rendering of each primitive. For example, there is about 6 ways of text rendering, but the fastest is DrawingContext.DrawGlyphs.
  4. Use profiler to discover hot spots. For example, in our project we had geometries cache and rendered appropriate of them on demand. It seemed to be, that no improvements are possible. But one day we thought what if we will render geometries one time and cache ready visuals? In our case such approach happened acceptable. Our unit's chart has just several states. When data of chart is changed, we rebuild DrawingVisual for each state and put them into cache.

当然,这种方式需要一些投资,是枯燥乏味的工作,但效果很棒.

Of course, this way needs some investments, it's dull and boring work, but result is awesome.

顺便说一句:当我们打开 WPF 缓存选项(您可以在答案中找到链接)时,我们的应用程序挂了.

By the way: when we turned on WPF caching option (you could find link in answers), our app hung up.

这篇关于提高 WPF UI 渲染速度的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆