简体   繁体   English

如何加速WPF程序?

[英]How to speed up WPF programs?

I love programming with and for Windows Presentation Framework. 我喜欢使用Windows Presentation Framework进行编程。 Mostly I write browser-like apps using WPF and XAML. 大多数情况下,我使用WPF和XAML编写类似浏览器的应用程序。

But what really annoys me is the slowness of WPF. 但真正令我恼火的是WPF的缓慢。 A simple page with only a few controls loads fast enough, but as soon as a page is a teeny weeny bit more complex, like containing a lot of data entry fields, one or two tab controls, and stuff, it gets painful. 一个只有少量控件的简单页面加载速度足够快,但是一旦页面变得更加复杂,就像包含大量数据输入字段,一个或两个选项卡控件和东西一样,它会变得很痛苦。

Loading of such a page can take more than one second. 加载此类页面可能需要一秒钟以上。 Seconds, indeed, especially on not so fast computers (read: the customers computers) it can take ages. 实际上,特别是在没有那么快的计算机(阅读:客户计算机)上它可能需要很长时间。

Same with changing values on the page. 与更改页面上的值相同。 Everything about the WPF UI is somehow sluggy. 关于WPF UI的一切都是某种程度的笨拙。

This is so mean! 这太平均了! They give me this beautiful framework, but make it so excruciatingly slow so I'll have to apologize to our customers all the time! 他们给了我这个漂亮的框架,但是让它变得非常缓慢,所以我必须一直向我们的客户道歉!

My Question: 我的问题:

  1. How do you speed up WPF? 你如何加速WPF?
  2. How do you profile bottlenecks? 你如何描述瓶颈?
  3. How do you deal with the slowness? 你如何应对缓慢?

Since this seems to be an universal problem with WPF, I'm looking for general advice, useful for many situations and problems. 由于这似乎是WPF的普遍问题,我正在寻找一般建议,对许多情况和问题都有用。

Some other related questions: 其他一些相关问题:

  1. How do you speed up WPF? 你如何加速WPF?

    Often after using one of the following profiling tools it is obvious what is causing my bottlenecks. 通常在使用以下分析工具之一后,很明显是什么导致了我的瓶颈。

    • If memory is the issue then I virtualize my data. 如果内存是问题,那么我虚拟化我的数据。
    • If render time is the issue then I virtualize the controls or simplify control templates where possible. 如果渲染时间是问题,那么我会尽可能虚拟化控件或简化控件模板。
    • If processing time is the issue I try to improve my algorithm or move that work to a background thread and show a throbber in my ui while the work is going. 如果处理时间是问题我尝试改进我的算法或将该工作移动到后台线程并在工作进行时在我的ui中显示一个throbber。

  2. How do you profile bottlenecks? 你如何描述瓶颈?

  3. How do you deal with the slowness? 你如何应对缓慢?

    Profiling and counseling. 分析和咨询。

Install SP1... Loads of very cool performance increases for WPF!!! 安装SP1 ... WPF的非常酷的性能增加!

Read more here 在这里阅读更多

Here is a example of 2 enhanchements made in SP1: Deffered scrolling & UI Element recyceling !!! 以下是在SP1中进行的两个enhanchements的示例:Deffered scrolling&UI Element recyceling !!!

I can not add comments, that's why I post a new answer to this: I've found this video from the pdc09 that gives some ideas about how to profile wpf apps and because it helped me lot, I want to share the link: 我无法添加评论,这就是为什么我发布了一个新的答案:我发现这个来自pdc09的视频提供了一些关于如何配置wpf应用程序的想法,因为它帮了我很多,我想分享链接:

Advanced WPF Application Performance Tuning and Analysis 高级WPF应用程序性能调优和分析

WPF is meant for computers with modern graphics cards. WPF适用于带有现代图形卡的计算机。 Do your clients have modern graphics cards capable of running Aero? 您的客户是否拥有能够运行Aero的现代显卡? If your clients have older graphics cards, WPF will fall back to software rendering which runs extremely slow in comparison to hardware accelerated graphics. 如果您的客户使用较旧的图形卡,WPF将回退到与硬件加速图形相比运行速度极慢的软件渲染。

You also might want to profile your application to make sure that it is actually WPF that is the slow part. 您也可能想要分析您的应用程序,以确保它实际上是WPF,这是一个缓慢的部分。 It's possible that there is something else that is actually the bottleneck. 可能还有其他东西实际上是瓶颈。

avoiding animations also helps a lot sometimes. 避免动画有时也会有很大帮助。 if you have to use animations, decrease the framerate, this will improve "Feeled" performance 如果你必须使用动画,降低帧速率,这将改善“感觉”的性能

删除Alpha透明度/位图效果。

I find it helpful to side-step the XAML, and write the entire UI in C#. 我发现侧向步骤XAML很有帮助,并在C#中编写整个UI。 This lets me precisely control when the controls are created and loaded. 这使我可以精确控制何时创建和加载控件。 It also helps me understand what XAML is doing "under the covers". 它还有助于我理解XAML在“幕后”所做的事情。

can you give more details? 你能提供更多细节吗?

I only noticed a slow performance when I use something like a listview or a grid that has some complexity. 当我使用像listview或具有一些复杂性的网格之类的东西时,我只注意到性能缓慢。 The solution is to simplify it. 解决方案是简化它。

Other than that I only noticed a slow performance when loading the app for the first time. 除此之外,我第一次加载应用时只注意到性能下降。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM