WPF 和 WinForms 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1337345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:55:19  来源:igfitidea点击:

What is the difference between WPF and WinForms?

wpfwinforms

提问by l--''''''---------''''''''''''

I am programming simple Windows apps. I don't need DB support. Why would I use WPF instead of WinForms?

我正在编写简单的 Windows 应用程序。我不需要数据库支持。为什么我要使用 WPF 而不是 WinForms?

采纳答案by Benjol

  • To learn.
  • To have greater (i.e. any) control over the appearance of your program
  • To benefit from easier data binding, triggers, styles
  • 学习。
  • 对程序的外观有更大的(即任何)控制权
  • 受益于更简单的数据绑定、触发器、样式

(I don't see what DB support has got to do with it)

(我不明白数据库支持与它有什么关系)

回答by ChrisF

One obvious answer is that WPF offers a richer user experience than WinForms, allowing for animations even 3D in the user interface for example.

一个显而易见的答案是,WPF 提供了比 WinForms 更丰富的用户体验,例如,允许在用户界面中使用甚至 3D 的动画。

From a development perspective it goes a long way to enforce the separation of the User Interface (in the XAML) from the business logic (in VB.NET or C#) which is always a good thing.

从开发的角度来看,强制将用户界面(在 XAML 中)与业务逻辑(在 VB.NET 或 C# 中)分离总是一件好事。

A Google search for "WPF vs WinForms" brings up lots of pages that discuss this issue. I won't repeat all their findings here, but this pageraises some interesting points:

在 Google 上搜索“WPF vs WinForms”会出现很多讨论这个问题的页面。我不会在这里重复他们的所有发现,但这个页面提出了一些有趣的观点:

  1. Databinding in WPF is superior to what Windows Forms offers.
  2. UI and C# business logic can be cleanly separated in WPF
  3. Storyboard
  4. Data/control templates – a much cleaner way than anything Windows Forms can offer.
  5. Styles – cool and simple. Its so easy to style all your buttons in an application to have the same look and feel.
  6. Even if the VS designer breaks, its easy to code XAML.
  7. UI virtualization – I've got grids with 100K rows, ticking off a moving market. Performance would be dreadful if it wasn't for UI visualization which come for free.
  8. 3D support.
  9. Nothing scientific but, UI development feels quicker in WPF – maybe its just because a WPF application looks cooler at the end of an iteration, or maybe its because development really is quicker.
  10. I can add a User Experience engineer to my team, and with no C# knowledge he can work magic in Expression Blend and give the front-office trading application a makeover that is guaranteed to win over the business users.
  1. WPF 中的数据绑定优于 Windows 窗体提供的数据绑定。
  2. UI 和 C# 业务逻辑可以在 WPF 中完全分离
  3. 故事板
  4. 数据/控件模板 - 比 Windows 窗体所能提供的任何东西都更简洁的方式。
  5. 风格——酷而简单。在应用程序中设置所有按钮的样式以具有相同的外观和感觉非常容易。
  6. 即使 VS 设计器坏了,编写 XAML 代码也很容易。
  7. UI 虚拟化——我有 10 万行的网格,这是一个移动的市场。如果不是免费提供的 UI 可视化,性能会很糟糕。
  8. 3D 支持。
  9. 没有什么科学依据,但是在 WPF 中 UI 开发感觉更快——可能只是因为 WPF 应用程序在迭代结束时看起来更酷,或者可能是因为开发确实更快。
  10. 我可以在我的团队中添加一名用户体验工程师,并且在没有 C# 知识的情况下,他可以在 Expression Blend 中发挥神奇的作用,并对前台交易应用程序进行改造,从而保证赢得业务用户的青睐。

回答by jrista

WPF is the current platform for developing windows desktop applications. It is a modern, advanced, hardware accelerated framework for developing applications that maintain separation of concerns. It supports advanced rendering of 2D vector and 3D graphics, providing an immense range of capabilities for building rich, interactive, and quality user interfaces.

WPF 是当前用于开发 Windows 桌面应用程序的平台。它是一个现代的、先进的、硬件加速的框架,用于开发保持关注点分离的应用程序。它支持 2D 矢量和 3D 图形的高级渲染,为构建丰富、交互式和高质量的用户界面提供了广泛的功能。

Winforms, on the other hand, provides a basic platform for developing "classic" windows apps with your standard look and feel using standard controls. It does not offer the rich, hardware accelerated, vector 2D and 3D capabilities that WPF offers. Winforms applications tend to have much greater coupling between application behavior and UI (view), which presents a less flexible platform upon which to develop applications.

另一方面,Winforms 提供了一个基本平台,用于使用标准控件开发具有标准外观和感觉的“经典”Windows 应用程序。它不提供 WPF 提供的丰富的、硬件加速的矢量 2D 和 3D 功能。Winforms 应用程序往往在应用程序行为和 UI(视图)之间有更大的耦合,这为开发应用程序提供了一个不太灵活的平台。

As for which one you choose, it entirely depends on your needs. If you need vector graphics, 3D rendering, the ability to create rich, interactive, animated, modern user interfaces, and/or wish to maintain separation of concerns, WPF is definitely the right choice. If you need none of that, and just need to create a simple UI that solves a simple problem, winforms will meet your needs just fine.

至于选择哪一种,完全看你的需求了。如果您需要矢量图形、3D 渲染、创建丰富、交互式、动画、现代用户界面的能力,和/或希望保持关注点分离,WPF 绝对是正确的选择。如果您不需要这些,只需要创建一个简单的 UI 来解决一个简单的问题,那么 winforms 将很好地满足您的需求。

回答by Sai Saran

The single most important difference between WinFormsand WPFis the fact that while WinForms is simply a layer on top of the standard Windows controls (e.g. a TextBox), WPF is built from scratch and doesn't rely on standard Windows controls in almost all situations. This might seem like a subtle difference, but it really isn't, which you will definitely notice if you have ever worked with a framework that depends on Win32/WinAPI.

WinForms和之间最重要的区别在于WPF,虽然 WinForms 只是标准 Windows 控件(例如 TextBox)之上的一个层,但 WPF 是从头开始构建的,几乎在所有情况下都不依赖于标准 Windows 控件。这似乎是一个微妙的区别,但实际上并非如此,如果您曾经使用过依赖于 Win32/WinAPI 的框架,您肯定会注意到这一点。

A great example of this is a button with an image and text on it. This is not a standard Windows control, so WinForms doesn't offer you this possibility out of the box. Instead you will have to draw the image yourself, implement your own button that supports images or use a 3rd party control. With WPF, a button can contain anything because it's essentially a border with content and various states (e.g. untouched, hovered, pressed). The WPF button is "look-less", as are most other WPF controls, which means that it can contain a range of other controls inside of it. You want a button with an image and some text? Just put an Image and a TextBlock control inside of the button and you're done! You simply don't get this kind of flexibility out of the standard WinForms controls, which is why there's a big market for rather simple implementations of controls like buttons with images and so on.

一个很好的例子是一个带有图像和文本的按钮。这不是标准的 Windows 控件,因此 WinForms 不提供这种开箱即用的可能性。相反,您必须自己绘制图像,实现自己的支持图像的按钮或使用第 3 方控件。使用 WPF,按钮可以包含任何内容,因为它本质上是包含内容和各种状态(例如未触摸、悬停、按下)的边框。WPF 按钮与大多数其他 WPF 控件一样“无外观”,这意味着它可以在其中包含一系列其他控件。你想要一个带有图像和一些文字的按钮吗?只需在按钮内放置一个 Image 和一个 TextBlock 控件,就完成了!您根本无法从标准的 WinForms 控件中获得这种灵活性,这就是为什么“

The drawback to this flexibility is that sometimes you will have to work harder to achieve something that was very easy with WinForms, because it was created for just the scenario you need it for. At least that's how it feels in the beginning, where you find yourself creating templates to make a ListView with an image and some nicely aligned text, something that the WinForms ListViewItem does in a single line of code.

这种灵活性的缺点是有时您必须更加努力才能实现使用 WinForms 非常容易的东西,因为它只是为您需要的场景而创建的。至少一开始的感觉是这样的,在那里您发现自己正在创建模板来制作带有图像和一些对齐良好的文本的 ListView,这是 WinForms ListViewItem 在一行代码中完成的。

This was just one difference, but as you work with WPF, you will realize that it is in fact the underlying reason for many of the other differences - WPF is simply just doing things in its own way, for better and for worse. You're no longer constrained to doing things the Windows way, but to get this kind of flexibility, you pay with a little more work when you're really just looking to do things the Windows way.

这只是一个差异,但是当您使用 WPF 时,您会意识到它实际上是许多其他差异的根本原因 - WPF 只是以自己的方式做事,无论好坏。您不再受限于以 Windows 方式做事,但要获得这种灵活性,当您真正只想以 Windows 方式做事时,您需要付出更多的努力。

回答by Chris O

WPF can utilize hardware acceleration to some degree, but that is expected to improve over time.

WPF 可以在一定程度上利用硬件加速,但预计会随着时间的推移而改进。

Also, because of XAML, you have more options for "doing stuff", declarative vs. programmatic, or a mixture of both.

此外,由于 XAML,您可以有更多选项用于“做事”、声明式与编程式或两者的混合。

Microsoft no longer does active development on winforms, they are strongly pushing WPF, and for good reason.

微软不再在 winforms 上进行积极的开发,他们正在大力推动 WPF,这是有充分理由的。

WPF allows for much easier "resolution agnostic" designing. To achieve that in winforms, it is a lotmore work.

WPF 允许更容易的“分辨率不可知”设计。要在 winforms 中实现这一点,还有很多工作要做。

The MVVM pattern was already mentioned in one of the comments, this allows one to do real unittesting vs. GUI-based testing on your code, that is a bigwin, in my experience.

MVVM 模式已在其中一条评论中提及,这允许对您的代码进行真正的单元测试与基于 GUI 的测试,根据我的经验,这是一个巨大的胜利。

回答by CaptainDeveloper

A

一种

Anywhere Execution due to XAML.

Can be used as WinApp, WebApp, Mobile.

Whereas WindowsForm Internal UI representation is in C#.

由于 XAML 的任何地方执行。

可用作 WinApp、WebApp、Mobile。

而 WindowsForm 内部 UI 表示是在 C# 中。

B

Binding -->Simple object to object data transfer.

绑定 --> 简单对象到对象数据传输。

C

C

Common look and feel(Styles) -->can define look and feel styles commonly and use it for bunch of controls.

通用外观(样式)--> 可以定义通用外观样式并将其用于一堆控件。

D

D

Directive Programming -->Binding objects in XAML.

指令编程 --> 在 XAML 中绑定对象。

E

Expression blend and Animation-->WPF uses DirectX, and DirectX can be used for animation.

Expression blend and Animation-->WPF 使用 DirectX,DirectX 可以用于动画。

F

F

Faster Execution(Hardware Rendering)

WPF internally uses DirectX (Hardware rendering) while Winform internally uses GDI (mostly uses Software rendering).

There are two ways by which a computer renders display on monitor.

1) (CPU) Software rendering -->In case of CPU rendering ,the CPU drives the whole logic of rendering display on monitor.CPU also does other operations like running applications,performing memory management,running OS. So on top of it its extra load to display things on monitor.

2)(GPU) Hardware rendering -->Its Specialized kinda processor, specifically meant for rendering and display faster on monitor.

Now in WPF, this rendering is further optimised in

更快的执行(硬件渲染)

WPF 内部使用 DirectX(硬件渲染),而 Winform 内部使用 GDI(主要使用软件渲染)。

计算机在显示器上呈现显示的方式有两种。

1) (CPU) 软件渲染 --> 在 CPU 渲染的情况下,CPU 驱动渲染显示在监视器上的整个逻辑。CPU 还执行其他操作,例如运行应用程序、执行内存管理、运行操作系统。因此,最重要的是它的额外负载在监视器上显示内容。

2)(GPU)硬件渲染-->它的专用处理器,专门用于在显示器上更快地渲染和显示。

现在在 WPF 中,此渲染在

>Tier 0 mode (Software rendering) uses DirectX7 internally,

>Tier 1 mode (Partial Hardware rendering) uses DirectX7 to DirectX9 internally,

>Tier 2 mode (Hardware rendering) uses DirectX9 internally. 

G

G

Graphic Independence (DIP) -->Means resolution independence

Resolution --> Total number of pixels that fits into screen/monitor

Pixel --> Simple dot on screen.

Windows form uses pixels as a measurement unit, so when pixel changes then win forms has to adjust itself that means we have to write logic for it.

But WPF does not use pixels as a measurement unit but uses DIP(Device independent pixels)

1 DIP = 1/96th of the inch.

图形独立性 (DIP) --> 表示分辨率独立性

分辨率 --> 适合屏幕/显示器的像素总数

像素 --> 屏幕上的简单点。

Windows 窗体使用像素作为度量单位,因此当像素发生变化时,win 窗体必须自行调整,这意味着我们必须为其编写逻辑。

但是 WPF 不使用像素作为度量单位而是使用 DIP(设备独立像素)

1 DIP = 1/96 英寸。

At last Testing --> Better unit testing with use of MVVM pattern.

最后测试 --> 使用 MVVM 模式进行更好的单元测试。

回答by Budda

Nobody mentioned about better testing capabilities of the WPF applications (if they are written in the correct way, for example based on the MVVM pattern).

没有人提到 WPF 应用程序更好的测试功能(如果它们以正确的方式编写,例如基于 MVVM 模式)。

回答by Meta-Knight

If you want to have a rich user interface like the image posted in your previous question, I'd recommend going with WPF. Aside from making it easier to create a nice-looking application, it's also the technology Microsoft will push in the future. There's almost no new development for Winforms.

如果您想拥有像上一个问题中发布的图像那样的丰富用户界面,我建议您使用 WPF。除了更容易创建漂亮的应用程序之外,这也是微软未来将推动的技术。Winforms 几乎没有新的开发。

回答by Aakash Singh

Advantages and disadvantages of WPF over winforms :

WPF 相对于 winforms 的优缺点:

  1. The ability to make very rich UIs relatively easily.
  2. Easier animation and special effects
  3. Inherent scalability
  4. Powerfull styling and skinning structure
  5. Easy to create an own Look and Feel
  6. Does support Windows Forms
  7. The future technology for developing Vista Applications
  8. The ability to reuse existing code
  9. Highly advanced databinding possible
  10. Declarative vs procedural code
  11. Requires .NET Framework 3.0
  12. Compared to Windows Forms, still in development fase
  13. Requires Dx9 compatible vidcard for advanced graphics
  1. 能够相对容易地制作非常丰富的 UI。
  2. 更简单的动画和特效
  3. 固有的可扩展性
  4. 强大的造型和蒙皮结构
  5. 轻松创建自己的外观和感觉
  6. 是否支持 Windows 窗体
  7. 用于开发 Vista 应用程序的未来技术
  8. 重用现有代码的能力
  9. 高度先进的数据绑定成为可能
  10. 声明性与程序性代码
  11. 需要 .NET 框架 3.0
  12. 与 Windows Forms 相比,仍处于开发阶段
  13. 需要兼容 Dx9 的显卡才能获得高级图形

However, there are drawbacks to WPF, where WinForms comes out on top:

但是,WPF 也有缺点,其中 WinForms 名列前茅:

  1. WPF's in-box control suite is far more limited than that of WinForms.
  2. There's greater support in the 3rd-party control space for WinForms. (That's changing, but for now by advantage of time, WinForms has greater support in the community.) 3.WPF will not run on windows 2000 or lower.
  3. Extensive documentation to be found on the internet FOR WIN Forms
  4. Plenty of examples FOR WIN Forms
  5. Design your own look and feel in a application is a lot of work. 7.C# is slower to run. This is somewhat taken care of when using WPF, although currently the launching of WPF application is still a bit slow. However, after the program is launched, the animation effects are all very smooth.
  1. WPF 的内置控件套件远比 WinForms 的有限。
  2. 对 WinForms 的第 3 方控制空间有更大的支持。(这种情况正在改变,但现在利用时间优势,WinForms 在社区中获得了更大的支持。) 3.WPF 将无法在 windows 2000 或更低版本上运行。
  3. 可在 Internet 上找到的大量文档 FOR WIN Forms
  4. WIN 表格的大量示例
  5. 在应用程序中设计您自己的外观和感觉需要大量工作。7.C#运行速度较慢。这在使用 WPF 时会有所照顾,尽管目前 WPF 应用程序的启动仍然有点慢。但是,程序启动后,动画效果都非常流畅。