简体   繁体   English

WPF 和 WinForms 有什么区别?

[英]What is the difference between WPF and WinForms?

I am programming simple Windows apps.我正在编写简单的 Windows 应用程序。 I don't need DB support.我不需要数据库支持。 Why would I use WPF instead of WinForms?为什么我要使用 WPF 而不是 WinForms?

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.在 Google 上搜索“WPF vs WinForms”会出现很多讨论这个问题的页面。 I won't repeat all their findings here, but this page raises some interesting points:我不会在这里重复他们的所有发现,但这个页面提出了一些有趣的观点:

  1. Databinding in WPF is superior to what Windows Forms offers. WPF 中的数据绑定优于 Windows 窗体提供的数据绑定。
  2. UI and C# business logic can be cleanly separated in WPF UI 和 C# 业务逻辑可以在 WPF 中完全分离
  3. Storyboard故事板
  4. Data/control templates – a much cleaner way than anything Windows Forms can offer.数据/控件模板 - 比 Windows 窗体所能提供的任何东西都更简洁的方式。
  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.即使 VS 设计器坏了,编写 XAML 代码也很容易。
  7. UI virtualization – I've got grids with 100K rows, ticking off a moving market. UI 虚拟化——我有 10 万行的网格,这是一个移动的市场。 Performance would be dreadful if it wasn't for UI visualization which come for free.如果不是免费提供的 UI 可视化,性能会很糟糕。
  8. 3D support. 3D 支持。
  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.没有什么科学依据,但是在 WPF 中 UI 开发感觉更快——可能只是因为 WPF 应用程序在迭代结束时看起来更酷,或者可能是因为开发确实更快。
  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.我可以在我的团队中添加一名用户体验工程师,并且在没有 C# 知识的情况下,他可以在 Expression Blend 中发挥神奇的作用,并对前台交易应用程序进行改造,从而保证赢得业务用户的青睐。

WPF is the current platform for developing Windows desktop applications. WPF 是当前用于开发 Windows 桌面应用程序的平台。 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.它支持 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.另一方面,WinForms 提供了一个基本平台,用于使用标准控件开发具有标准外观和感觉的“经典”Windows 应用程序。 It does not offer the rich, hardware accelerated, vector 2D and 3D capabilities that WPF offers.它不提供 WPF 提供的丰富的、硬件加速的矢量 2D 和 3D 功能。 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 应用程序往往在应用程序行为和 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.如果您需要矢量图形、3D 渲染、创建丰富、交互式、动画、现代用户界面的能力,和/或希望保持关注点分离,WPF 绝对是正确的选择。 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.如果您不需要这些,而只需要创建一个简单的 UI 来解决一个简单的问题,WinForms 将很好地满足您的需求。

  • To learn.学习。
  • To have greater (ie 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) (我不明白数据库支持与它有什么关系)

The single most important difference between WinForms and WPF is the fact that while WinForms is simply a layer on top of the standard Windows controls (eg a TextBox), WPF is built from scratch and doesn't rely on standard Windows controls in almost all situations. WinFormsWPF之间最重要的区别在于,尽管WinForms只是标准Windows控件(例如TextBox)之上的一层,但WPF是从头开始构建的,几乎在所有情况下都不依赖于标准的Windows控件。 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. 这可能看起来像是一个微妙的区别,但实际上并非如此,如果你曾经使用过依赖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. 这不是标准的Windows控件,因此WinForms不提供开箱即用的这种可能性。 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 (eg untouched, hovered, pressed). 使用WPF,按钮可以包含任何内容,因为它基本上是内容和各种状态的边界(例如,未触摸,悬停,按下)。 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. 与大多数其他WPF控件一样,WPF按钮是“无外观”,这意味着它可以包含一系列其他控件。 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! 只需将一个Image和一个TextBlock控件放在按钮内,就可以了! 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. 你只是没有从标准的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. 这种灵活性的缺点在于,有时您必须更加努力地使用WinForms来实现非常简单的操作,因为它是为您需要的场景创建的。 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. 至少在开始时就是这样,你发现自己创建模板来创建一个带有图像的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. 这只是一个区别,但是当你使用WPF时,你会发现它实际上是许多其他差异的根本原因 - WPF只是以自己的方式做事,无论好坏。 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. 你不再局限于以Windows方式做事,但为了获得这种灵活性,当你真正只是想用Windows做事时,你会付出更多的工作。

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. WPF 允许更容易的“分辨率不可知”设计。 To achieve that in WinForms, it is a lot more work.要在 WinForms 中实现这一点,还有很多工作要做。

The MVVM pattern was already mentioned in one of the comments, this allows one to do real unit testing vs. GUI-based testing on your code, that is a big win, in my experience. MVVM 模式已在其中一条评论中提及,这允许对代码进行真正的单元测试与基于 GUI 的测试,根据我的经验,这是一个巨大的胜利。

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

A一种

Anywhere Execution due to XAML.由于 XAML 的任何地方执行。

Can be used as WinApp, WebApp, Mobile.可用作 WinApp、WebApp、Mobile。

Whereas WindowsForm Internal UI representation is in C#.而 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). WPF 内部使用 DirectX(硬件渲染),而 Winform 内部使用 GDI(主要使用软件渲染)。

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. 1) (CPU) 软件渲染 --> 在 CPU 渲染的情况下,CPU 驱动渲染显示在监视器上的整个逻辑。CPU 还执行其他操作,例如运行应用程序、执行内存管理、运行操作系统。 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. 2)(GPU)硬件渲染-->它的专用处理器,专门用于在显示器上更快地渲染和显示。

Now in WPF, this rendering is further optimised in现在在 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图形独立性 (DIP) --> 表示分辨率独立性

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

But WPF does not use pixels as a measurement unit but uses DIP(Device independent pixels)但是 WPF 不使用像素作为度量单位而是使用 DIP(设备独立像素)

1 DIP = 1/96th of the inch. 1 DIP = 1/96 英寸。

At last Testing --> Better unit testing with use of MVVM pattern.最后测试 --> 使用 MVVM 模式进行更好的单元测试。

If you want to have a rich user interface like the image posted in your previous question , I'd recommend going with WPF.如果您想要像上一个问题中发布的图像那样拥有丰富的用户界面,我建议您使用 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. Winforms 几乎没有新的开发。

Advantages and disadvantages of WPF over winforms : WPF优于winforms的优点和缺点:

  1. The ability to make very rich UIs relatively easily. 能够相对轻松地制作非常丰富的UI。
  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 是否支持Windows窗体
  7. The future technology for developing Vista Applications 未来开发Vista应用程序的技术
  8. The ability to reuse existing code 重用现有代码的能力
  9. Highly advanced databinding possible 可实现高度先进的数据绑定
  10. Declarative vs procedural code 声明性与程序性代码
  11. Requires .NET Framework 3.0 需要.NET Framework 3.0
  12. Compared to Windows Forms, still in development fase 与Windows Forms相比,仍处于开发阶段
  13. Requires Dx9 compatible vidcard for advanced graphics 需要兼容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. WPF的内置控制套件比WinForms的限制更有限。
  2. There's greater support in the 3rd-party control space for WinForms. 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. (这种情况正在发生变化,但是现在凭借时间优势,WinForms在社区中得到了更多的支持。)3.WPF将无法在Windows 2000或更低版本上运行。
  3. Extensive documentation to be found on the internet FOR WIN Forms 可以在互联网上找到广泛的文档FOR WIN Forms
  4. Plenty of examples FOR WIN Forms WIN表格的大量例子
  5. Design your own look and feel in a application is a lot of work. 在应用程序中设计自己的外观和感觉是很多工作。 7.C# is slower to run. 7.C#运行较慢。 This is somewhat taken care of when using WPF, although currently the launching of WPF application is still a bit slow. 虽然目前WPF应用程序的启动仍然有点慢,但在使用WPF时需要注意这一点。 However, after the program is launched, the animation effects are all very smooth. 但是,在程序启动后,动画效果都非常流畅。
  • The single most important difference between WinForms and WPF is the fact that while WinForms is simply a layer on top of the standard Windows controls (eg a TextBox), WPF is built from scratch and doesn't rely on standard Windows controls in almost all situations. WinForms 和 WPF 之间唯一最重要的区别是,虽然 WinForms 只是标准 Windows 控件(例如 TextBox)之上的一个层,但 WPF 是从头开始构建的,几乎在所有情况下都不依赖于标准 Windows 控件.

  • 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.这不是标准的 Windows 控件,因此 WinForms 不提供这种开箱即用的可能性。 Instead, you will have to draw the image yourself, implement your own button that supports images or use a 3rd party control.相反,您必须自己绘制图像,实现自己的支持图像的按钮或使用第 3 方控件。 With WPF, a button can contain anything because it's essentially a border with content and various states (eg untouched, hovered, pressed).使用 WPF,按钮可以包含任何内容,因为它本质上是包含内容和各种状态(例如未触摸、悬停、按下)的边框。

check this article it will help you: https://www.wpf-tutorial.com/about-wpf/wpf-vs-winforms/#:~:text=The%20single%20most%20important%20difference,controls%20in%20almost%20all%20situations .检查这篇文章它会帮助你: https : //www.wpf-tutorial.com/about-wpf/wpf-vs-winforms/# :~: text=The%20single%20most%20important%20difference,controls%20in% 20almost%20all%20situations

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

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