如何加快WPF项目?

时间:2021-10-01 02:43:41

I love programming with and for Windows Presentation Framework. Mostly I write browser-like apps using WPF and XAML.

我喜欢用Windows Presentation Framework进行编程。我主要使用WPF和XAML编写类似浏览器的应用程序。

But what really annoys me is the slowness of 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.

但真正让我恼火的是WPF的缓慢。一个简单的页面只有几个控件的加载速度足够快,但是一旦页面变得非常复杂,比如包含大量的数据输入字段、一个或两个选项卡控件,就会变得很痛苦。

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?
  2. 如何加速WPF?
  3. How do you profile bottlenecks?
  4. 如何评估瓶颈?
  5. How do you deal with the slowness?
  6. 你如何处理慢度?

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:

其他一些相关的问题:

8 个解决方案

#1


36  

  1. How do you speed up WPF?

    Often after using one of the following profiling tools it is obvious what is causing my bottlenecks.

    如何加速WPF?通常在使用以下分析工具之一之后,很明显是什么导致了我的瓶颈。

    • 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.

    你如何应对这种缓慢?分析和咨询。

#2


6  

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中做了2个增强:Deffered滚动和UI元素回收!!!

#3


5  

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应用程序性能调优和分析

#4


2  

WPF is meant for computers with modern graphics cards. Do your clients have modern graphics cards capable of running 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是为拥有现代显卡的计算机而设计的。你的客户有现代的显卡能够运行Aero吗?如果您的客户端有较老的显卡,那么WPF将退回到软件渲染,与硬件加速显卡相比,软件渲染的运行速度非常慢。

You also might want to profile your application to make sure that it is actually WPF that is the slow part. It's possible that there is something else that is actually the bottleneck.

您可能还需要对应用程序进行概要分析,以确保实际上WPF是最慢的部分。可能还有其他的瓶颈。

#5


1  

avoiding animations also helps a lot sometimes. if you have to use animations, decrease the framerate, this will improve "Feeled" performance

避免动画有时也有很大的帮助。如果你必须使用动画,减少帧数,这将提高“感觉”性能。

#6


1  

Remove alpha transparency/ bitmap effects.

移除alpha透明度/位图效果。

#7


0  

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. The solution is to simplify it.

我只在使用listview或具有一定复杂性的网格时才注意到性能缓慢。解决办法是简化它。

Other than that I only noticed a slow performance when loading the app for the first time.

除此之外,我只注意到在第一次加载应用程序时表现缓慢。

HTH

HTH

#8


0  

I find it helpful to side-step the XAML, and write the entire UI in C#. 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,用c#编写整个UI非常有用。这使我能够精确地控制控件何时被创建和加载。它也帮助我理解XAML在“幕后”做什么。

#1


36  

  1. How do you speed up WPF?

    Often after using one of the following profiling tools it is obvious what is causing my bottlenecks.

    如何加速WPF?通常在使用以下分析工具之一之后,很明显是什么导致了我的瓶颈。

    • 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.

    你如何应对这种缓慢?分析和咨询。

#2


6  

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中做了2个增强:Deffered滚动和UI元素回收!!!

#3


5  

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应用程序性能调优和分析

#4


2  

WPF is meant for computers with modern graphics cards. Do your clients have modern graphics cards capable of running 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是为拥有现代显卡的计算机而设计的。你的客户有现代的显卡能够运行Aero吗?如果您的客户端有较老的显卡,那么WPF将退回到软件渲染,与硬件加速显卡相比,软件渲染的运行速度非常慢。

You also might want to profile your application to make sure that it is actually WPF that is the slow part. It's possible that there is something else that is actually the bottleneck.

您可能还需要对应用程序进行概要分析,以确保实际上WPF是最慢的部分。可能还有其他的瓶颈。

#5


1  

avoiding animations also helps a lot sometimes. if you have to use animations, decrease the framerate, this will improve "Feeled" performance

避免动画有时也有很大的帮助。如果你必须使用动画,减少帧数,这将提高“感觉”性能。

#6


1  

Remove alpha transparency/ bitmap effects.

移除alpha透明度/位图效果。

#7


0  

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. The solution is to simplify it.

我只在使用listview或具有一定复杂性的网格时才注意到性能缓慢。解决办法是简化它。

Other than that I only noticed a slow performance when loading the app for the first time.

除此之外,我只注意到在第一次加载应用程序时表现缓慢。

HTH

HTH

#8


0  

I find it helpful to side-step the XAML, and write the entire UI in C#. 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,用c#编写整个UI非常有用。这使我能够精确地控制控件何时被创建和加载。它也帮助我理解XAML在“幕后”做什么。