.net 创建新 GUI 时,WPF 是不是 Windows 窗体的首选?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/57909/
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-03 09:48:20  来源:igfitidea点击:

When creating a new GUI, is WPF the preferred choice over Windows Forms?

.netwpfwinforms.net-3.5.net-3.0

提问by Anheledir

Most restrictions and tricks with windows forms are common to most programmers. But since .NET 3.0 there is also WPF available, the Windows Presentation Foundation. It is said that you can make "sexy applications" more easy with it and with .NET 3.5 SP1 it got a good speed boost on execution.

Windows 窗体的大多数限制和技巧对大多数程序员来说都是常见的。但从 .NET 3.0 开始,还有 WPF 可用,即 Windows Presentation Foundation。据说您可以使用它更轻松地制作“性感应用程序”,并且使用 .NET 3.5 SP1 可以很好地提高执行速度。

But on the other side a lot of things are working different with WPF. I will not say it is more difficult but you have to learn "everything" from scratch.

但另一方面,WPF 有很多不同之处。我不会说它更难,但你必须从头开始学习“一切”。

My question: Is it worth to spend this extra time when you have to create a new GUI and there is no time pressure for the project?

我的问题:当您必须创建一个新的 GUI 并且项目没有时间压力时,是否值得花费这些额外的时间?

回答by Mark

After three months of trying to hammer out a line-of-business(LOB) application on WPF, I reached a point of considering turning back to Windows Forms for my project, and in researching other people's opinions, came across this thread...

在尝试在 WPF 上敲定业务线(LOB) 应用程序三个月后,我考虑为我的项目重新使用 Windows 窗体,并且在研究其他人的意见时,遇到了这个线程......

Yes, WPF is a brilliant technology and it has benefits that span far beyond mere eye-candy... the templating and binding capabilities are great examples. The whole object model offers more flexibility and broader possibilities. That doesn't, however, make it the defacto platform for future LOB applications.

是的,WPF 是一项出色的技术,它的优势远远超出了单纯的视觉效果……模板和绑定功能就是很好的例子。整个对象模型提供了更多的灵活性和更广泛的可能性。然而,这并不能使它成为未来 LOB 应用程序的实际平台。

The "problems" which WPF solves in terms of separating GUI from business logic aren't problems which can't be readily solved in Windows Forms by simply starting with the right architecture and mind-set. Even the object-path binding capabilities of WPF can be reproduced in Windows Forms with some very simple helper classes. The data template capabilities of WPF are very nice, but again they're nothing that you can't simulate in Windows Forms on those rare occasions when you absolutely don't know exactly what objects you're going to represent on any given part of the screen.

WPF 在将 GUI 与业务逻辑分离方面解决的“问题”并不是通过简单地从正确的体系结构和思维方式开始在 Windows 窗体中无法轻易解决的问题。甚至 WPF 的对象路径绑定功能也可以通过一些非常简单的帮助程序类在 Windows 窗体中重现。WPF 的数据模板功能非常好,但在那些极少数情况下,当您完全不知道将在任何给定部分表示什么对象时,它们也不是您无法在 Windows 窗体中模拟的。屏幕。

Where Windows Forms races ahead is in terms of maturity. You can't swing a dead cat on Google without hitting some blog where someone has solved a Windows Forms problem for you. WPF, on the other hand, has comparatively less learning resources available, fewer custom controls available, and hasn't had as many of its teething problems solved.

Windows Forms 领先的地方在于成熟度。如果不访问某个有人为您解决了 Windows 窗体问题的博客,您就无法在 Google 上摇摆不定。另一方面,WPF 的可用学习资源相对较少,可用的自定义控件较少,而且还没有解决很多初期问题。

At the peak of making a WPF vs Windows Forms decision has got to be the maturity of the development environment. Windows Forms editors are slick, responsive and intuitive. Feedback about errors gets to you instantly, the solutions are usually obvious, and the compile->debug->edit cycle in Windows Forms is very quick.

在做出 WPF 与 Windows 窗体决策的高峰期必须是开发环境的成熟度。Windows 窗体编辑器灵活、响应迅速且直观。有关错误的反馈会立即发送给您,解决方案通常很明显,而且 Windows 窗体中的编译->调试->编辑周期非常快。

WPF applications, on the other hand, have comparatively pathetic design time support, with the design view all-too ready to chicken out at the first encounter of an error, often requiring a project build after the fix before the designer is willing to kick in again. Drag'n'drop of components from the toolbox might as well not be supported, given the vast range of circumstances under which it either doesn't work at all, or yields completely unintuitive results. Despite the promise of the WpfToolkit, there still isn't a usable DataGrid for WPF that yields any kind of resonable performance or design time friendliness.

另一方面,WPF 应用程序具有相对可悲的设计时间支持,设计视图在第一次遇到错误时准备好退缩,通常需要在设计人员愿意介入之前在修复后构建项目再次。考虑到在很多情况下它要么根本不起作用,要么产生完全不直观的结果,也可能不支持从工具箱中拖放组件。尽管 WpfToolkit 做出了承诺,但仍然没有可用的 WPF DataGrid 产生任何类型的合理性能或设计时友好性。

Debugging WPF applications is a bit like the oldASP.NET debugging paradigm... hit F5-> wait -> launch -> error -> stop -> fix -> hit F5-> wait -> launch -> error -> groan -> stop -> fix -> hit F5.... All XAML which your program is running is locked, and tracking down XAML specific problems is often tedious.

调试 WPF 应用程序有点像旧的ASP.NET 调试范式...命中F5-> 等待 -> 启动 -> 错误 -> 停止 -> 修复 -> 命中F5-> 等待 -> 启动 -> 错误 -> 呻吟 ->停止 -> 修复 -> 命中F5.... 您的程序正在运行的所有 XAML 都被锁定,并且追踪特定于 XAML 的问题通常很乏味。

The bottom line, simply put, is that the development tools for Windows Forms are going to have you banging out front-ends in a fraction of the time of a WPF application... especiallyif you're creating master-detail grids or spreadsheet like interfaces, which most LOB have. With Windows Forms, you start with 90% of the work already done for you.

简而言之,最重要的是,Windows 窗体的开发工具将让您在 WPF 应用程序的一小部分时间内完成前端......尤其是如果您正在创建主从网格或电子表格像大多数 LOB 所具有的接口。使用 Windows 窗体,您可以从已经为您完成的 90% 的工作开始。

I'm a huge fan of the WPF architecture. I just wish the design-time tool-set didn't feel like a pre-alpha debug-build.

我是 WPF 架构的忠实粉丝。我只是希望设计时工具集不像是 pre-alpha 调试版本。



Edit: This answer was posted about .NET 3.5 + Visual Studio 2008, but .NET 4.0 with Visual Studio 2010 ships with a WPF data grid. While many improvements have been made to the new WPF development experience, my answer here remains unchanged, and I'd like to add the following suggestion:

编辑:此答案是关于 .NET 3.5 + Visual Studio 2008 发布的,但带有 Visual Studio 2010 的 .NET 4.0 附带 WPF 数据网格。虽然对新的 WPF 开发体验进行了许多改进,但我在这里的答案保持不变,我想添加以下建议:

If you're in a rush to do RADdevelopment, go with Windows Forms. If you're looking to produce a well architected, maintainable, scalable, resource firendly, multi-user Line-Of-Business application, consider ASP.NET MVC + HTML 5 + jQuery... My projects with these technologies have resulted in better outcomes, sooner, for my customers. MVC offers all of the same templating that WPF does, and jQuery enables animations and complex interactions. More importantly, an ASP.NET MVC + jQuery solution doesn't require your end users to have modern desktops with decent graphics hardware.

如果您急于进行RAD开发,请使用 Windows 窗体。如果您希望生成一个架构良好、可维护、可扩展、资源友好、多用户的业务线应用程序,请考虑 ASP.NET MVC + HTML 5 + jQuery...我使用这些技术的项目产生了更好的结果为我的客户更快地取得成果。MVC 提供了与 WPF 相同的所有模板,而 jQuery 支持动画和复杂的交互。更重要的是,ASP.NET MVC + jQuery 解决方案不需要您的最终用户拥有具有良好图形硬件的现代桌面。

回答by Mark

I'm seven months into using WPF on what has now become a core system for my customer, and I'd like to share some more thoughts with you about the experience of learning and using WPF as a line of business presentation platform.

我在现在已成为我客户的核心系统上使用 WPF 已经七个月了,我想与您分享更多关于学习和使用 WPF 作为业务线演示平台的经验的想法。

In general, the comments I made above still hold... The design time support for WPF isn't here yet. If you're in a big rush to get a rich-client application out of the door, go with Windows Forms. Period. Microsoft aren't in any hurry to discontinue the GDI / Windows Forms platform, so you can count on good support for a fair time into the future.

总的来说,我上面的评论仍然成立...... WPF 的设计时支持还没有到来。如果您急于推出富客户端应用程序,请使用 Windows 窗体。时期。Microsoft 并不急于停止 GDI/Windows 窗体平台,因此您可以在未来相当长的一段时间内获得良好的支持。

WPF is not easy to master, but that shouldn't be where you leave your descision about whether or not to invest your time and energy into learning WPF. Despite its present lack of maturity, WPF is built around some useful, modern concepts.

WPF 并不容易掌握,但这不应该是您决定是否投入时间和精力来学习 WPF 的地方。尽管目前尚不成熟,但 WPF 是围绕一些有用的现代概念构建的。

In WPF, for example, your investment in well-written business objects with sound validating logic is a solid investment. Unlike Windows Forms, WPF's data binding is briming with features that allow interface controls to react to invalid user input without writing GUI codeto detect those errors. This is valuable.

例如,在 WPF 中,您对编写良好且具有良好验证逻辑的业务对象的投资是一项可靠的投资。与 Windows 窗体不同,WPF 的数据绑定充满了一些功能,允许界面控件对无效的用户输入做出反应,而无需编写 GUI 代码来检测这些错误。这是有价值的。

The styling and templating capabilities in WPF have proven to be valuable too. Despite the common misconception that the only use for styling and templating is to create on-screen eye-candy, the truth is that these features significantly simplify the coding of a user interface which gives rich feedback - like buttons that disable/enable themselves base on the state of the underlying business logic layer, or tooltips which intelligently find their text based on the state of the object under the cursor, etc.

WPF 中的样式和模板功能也被证明是有价值的。尽管普遍的误解是样式和模板的唯一用途是在屏幕上创建令人赏心悦目的糖果,但事实是这些功能显着简化了用户界面的编码,从而提供了丰富的反馈 - 例如禁用/启用自己的按钮基于底层业务逻辑层的状态,或根据光标下对象的状态智能查找其文本的工具提示等。

These all add up to incredibly valuable features for "nothing fancy" business applications, simply because they make it easy to keep the interface congruent with the underlying data.

这些都为“没什么特别的”业务应用程序提供了非常有价值的功能,仅仅是因为它们可以轻松地使界面与基础数据保持一致。

In a nutshell:

简而言之:

  • In Windows Forms you design your user interface, then write code to drive that user interface, which generally also includes code to drive your data objects.
  • In WPF you invest in the business layer that drives your data objects, then design an interface that listensto your data objects.
  • 在 Windows 窗体中,您可以设计用户界面,然后编写代码来驱动该用户界面,这通常还包括用于驱动数据对象的代码。
  • 在 WPF 中,您投资于驱动数据对象的业务层,然后设计一个侦听数据对象的接口。

It's a seemingly subtle difference, but it makes a huge difference in your ability to re-use code... which begs the question: "Is the Windows Forms vs WPF question actually an investment decision?"

这是一个看似细微的差异,但它对您重用代码的能力产生了巨大的影响……这就引出了一个问题:“Windows 窗体与 WPF 问题实际上是一个投资决策吗?”

(This seems to have become my favourite thread.)

(这似乎已成为我最喜欢的线程。)

回答by Ray Burns

Are there any compelling reasons to use WPF

是否有任何令人信服的理由使用 WPF

Absolutely! WPF is absolutely incredible! It will be a major benefit for practically any project because it has so many features and abilities that Windows Forms lacks.

绝对地!WPF绝对是不可思议的!对于几乎任何项目来说,这都将是一个主要的好处,因为它具有 Windows 窗体所缺乏的许多特性和能力。

For business applications the biggest wins will be:

对于商业应用程序,最大的胜利将是:

  • The fantastic data binding and templating make the biggest difference. Once a decent data model is in place, it only takes a few clicks to create a data template and use Expression Blendto configure exactly how your object will look using drag-and-drop. And binding to things like color or shape is trivial.
  • Screen layout is incredibly flexible. Not only can everything in WPF smoothly adjust to container size and shape changes, but items can trivially be enlarged and rotated, and even extend outside their containing frame.
  • Ordinary objects can be presented any way you like, can easily have different presentations in different screens, can share presentation, and can adapt their presentation to changes in data values.
  • If you need to print, rendering to the printer is trivial. Properly configured, WPF makes Crystal Reportsor SQL Server Reporting Services(SSRS) look like a child's toy.
  • Your user interface will look and feel much more dynamic, including nice features such as buttons that animate when you pass the mouse over them.
  • 奇妙的数据绑定和模板是最大的不同。一旦合适的数据模型就位,只需单击几下即可创建数据模板并使用Expression Blend通过拖放来准确配置对象的外观。与颜色或形状等事物的绑定是微不足道的。
  • 屏幕布局非常灵活。WPF 中的所有内容不仅可以平滑地适应容器大小和形状的变化,而且可以轻松地放大和旋转项目,甚至可以扩展到它们的包含框架之外。
  • 普通对象可以以您喜欢的任何方式呈现,可以轻松地在不同屏幕上进行不同的呈现,可以共享呈现,并且可以根据数据值的变化调整其呈现。
  • 如果您需要打印,渲染到打印机是微不足道的。正确配置后,WPF 使Crystal ReportsSQL Server Reporting Services(SSRS) 看起来像孩子的玩具。
  • 您的用户界面将看起来和感觉上更加动态,包括很好的功能,例如当您将鼠标移过它们时动画按钮。

For utilities and games, other advantages come to the forefront:

对于公用事业和游戏,其他优势更为突出:

  • You can easily add shapes, lines, and arbitrary drawings to your application without using an external editor. Every component of these can be data-bound and animated, or controlled by code. In Windows Forms you ususally just have to import a bitmap and use it as-is unless you want to go to a lot of work.,
  • Animations are cool! Users will be really impressed, as long as you don't overdo it. They can also help people see what is going on and reduce the need for hilighting. For example, when dragging an object you can animate the target to show what will happen if you drop it.
  • Colors, gradient fills, brushes, fancy fonts, rotation of any objects, tile brushes, etc. Anything you want graphically is yours for the asking.
  • Incredibly customizable. I needed to draw railroad tracks for one application, so I could drop a train on them. A couple of hours later I had railroad tracks I could draw anywhere on the screen using Bézier curves, and they would join and switch automatically.
  • 您可以轻松地将形状、线条和任意绘图添加到您的应用程序中,而无需使用外部编辑器。其中的每个组件都可以进行数据绑定和动画处理,或者由代码控制。在 Windows 窗体中,您通常只需要导入位图并按原样使用它,除非您想要进行大量工作。
  • 动画很酷!只要您不过度使用,用户就会留下深刻的印象。它们还可以帮助人们了解正在发生的事情并减少对突出显示的需要。例如,在拖动对象时,您可以为目标设置动画以显示如果放下它会发生什么。
  • 颜色、渐变填充、画笔、花哨的字体、任何对象的旋转、平铺画笔等。任何你想要的图形都是你的。
  • 令人难以置信的可定制。我需要为一个应用程序绘制铁轨,这样我就可以在上面放下一列火车。几个小时后,我有了可以使用Bézier 曲线在屏幕上任意位置绘制的铁路轨道,并且它们会自动连接和切换。

The bottom line is that any significant-size GUI you could build in Windows Forms can be built in WPF in a third of the effort (or less) and look way, way better.

最重要的是,您可以在 Windows 窗体中构建的任何大型 GUI 都可以在 WPF 中构建,只需三分之一(或更少)的工作量,并且看起来更好。

Does WPF require more resources (RAM in particular)

WPF 是否需要更多资源(尤其是 RAM)

You do pay a price compared to Windows Forms, but it is a small one.

与 Windows 窗体相比,您确实付出了代价,但代价很小。

  • RAM can go up or down depending on your implementation. WPF stores its data more efficiently so individual objects are smaller, but there tend to be more objects in WPF than in Windows Forms so this balances out, and either one can come out ahead.
  • CPU will go up compared to Windows Forms. In my experience, the actual update of WPF objects onscreen takes about twice as much CPU as normal Windows Forms rendering. If your application spends most of its time updating the screen, WPF may not be for you. But in that case you're probably not using Windows Forms either: Most serious games are written directly to DirectX.
  • Disk usage will be slightly less for WPF because it takes so much less code than Windows Forms. The data will be the same size, of course.
  • RAM 可以上升或下降,具体取决于您的实施。WPF 更有效地存储其数据,因此单个对象更小,但 WPF 中的对象往往比 Windows 窗体中的对象多,因此这可以平衡,并且任何一个都可以领先。
  • 与 Windows 窗体相比,CPU 会上升。根据我的经验,屏幕上 WPF 对象的实际更新所占用的 CPU 大约是正常 Windows 窗体渲染的两倍。如果您的应用程序大部分时间都在更新屏幕,则 WPF 可能不适合您。但在这种情况下,您可能也没有使用 Windows 窗体:大多数严肃游戏都是直接写入DirectX 的
  • WPF 的磁盘使用量会稍微少一些,因为它比 Windows 窗体需要的代码少得多。当然,数据将是相同的大小。

One more note about CPU use: Animations and transforms (motion, translation, etc.) is actually more efficient on WPF than in Windows Forms because of its retained mode storage. It is the initial getting of the objects up there that is slower.

关于 CPU 使用的另一个注意事项:动画和转换(运动、平移等)在 WPF 上实际上比在 Windows 窗体中更有效,因为它保留了模式存储。物体到达那里的初始速度较慢。

Maintenance overhead

维护开销

WPF is a hugewin over Windows Forms when it comes to maintenance. Since everything is done in 1/5 as much code as before, there is 1/5 as much to maintain. Plus all the boilerplate stuff is gone so you can focus on the code that actually does the work.

在维护方面,WPF 是Windows 窗体的巨大胜利。由于所有事情都是在 1/5 的代码中完成的,因此需要维护 1/5 的代码。此外,所有样板内容都消失了,因此您可以专注于实际执行工作的代码。

Benefits of XAML

XAML 的好处

XAMLis the core of WPF. Although WPF can be used without XAML, XAML makes it incredibly easy to use. XAML has HTML's ability to easily specify a user interface, but its built-in tags are much more powerful, and you can easily define your own. (In fact, it is normal to do so).

XAML是 WPF 的核心。虽然 WPF 可以在没有 XAML 的情况下使用,但 XAML 使它非常容易使用。XAML 具有 HTML 轻松指定用户界面的能力,但其内置标签功能要强大得多,您可以轻松定义自己的标签。(其实这样做很正常)。

Some specific advantages of XAML:

XAML 的一些特定优势:

  • Your entire UI is defined in a text file that is easy to read and manipulate, both for users and tools
  • MarkupExtensions allow Bindings to be specified in a clear and simple way
  • Type converters allow properties with complex types to be easily specified. For example, you can say Brush="Green" or you can specify a radial gradient brush with three stops.
  • You can create your own elements
  • You can easily leverage WPF's powerful "attached properties"
  • 您的整个 UI 定义在一个文本文件中,该文件易于用户和工具阅读和操作
  • MarkupExtensions 允许以清晰简单的方式指定绑定
  • 类型转换器允许轻松指定具有复杂类型的属性。例如,您可以说 Brush="Green" 或者您可以指定具有三个停止点的径向渐变画笔。
  • 您可以创建自己的元素
  • 您可以轻松利用 WPF 强大的“附加属性”

Other insights

其他见解

I dreamed of something like WPF for many years. Many people have implemented portions of this functionality, but to get it all in one place and at such a price ($0) is amazing.

多年来,我一直梦想着像 WPF 这样的东西。许多人已经实现了此功能的部分功能,但是以如此低的价格 (0 美元) 将所有功能集中到一个地方,真是太棒了。

WPF is a huge paradigm shift from Windows Forms and will take some getting used to, but the time spend learning it will pay itself back many-fold.

WPF 是 Windows 窗体的一个巨大范式转变,需要一些时间来适应,但是花在学习它上的时间会得到很多回报。

WPF still has a few warts even five years later, but its power will totally blow you away once you experience it. If someone tries to drag you back to Windows Forms, you'll only go kicking and screaming.

即使五年后,WPF 仍然有一些疣,但是一旦您体验过它,它的强大功能就会让您大吃一惊。如果有人试图将您拖回 Windows 窗体,您只会大喊大叫。

Tips: - Do get a copy of Expression Blend for development - Do edit XAML by hand occasionally - Don't give up when it seems strange at first

提示: - 获取 Expression Blend 的副本以进行开发 - 偶尔手动编辑 XAML - 一开始看起来很奇怪时不要放弃

回答by AndyL

WPF enables you to do some amazing things, and I LOVE it... but I always feel obligated to qualify my recommendations, whenever developers ask me whether I think they should be moving to the new technology.

WPF 使您能够做一些了不起的事情,我很喜欢它……但每当开发人员问我是否认为他们应该转向新技术时,我总是觉得有必要确认我的建议。

Are your developers willing (preferrably, EAGER) to spend the time it takes to learn to use WPF effectively? I never would have thought to say this about MFC, or Windows Forms, or even unmanaged DirectX, but you probably do NOT want a team trying to "pick up" WPF over the course of a normal dev. cycle for a shipping product!

您的开发人员是否愿意(最好是 EAGER)花时间学习有效地使用 WPF?我从没想过会这样说 MFC、Windows 窗体,甚至非托管 DirectX,但您可能不希望团队在正常开发过程中尝试“选择”WPF。循环运输产品!

Do at least one or two of your developers have some design sensibilities, and do individuals with final design authority have a decent understanding of development issues, so you can leverage WPF capabilities to create something which is actually BETTER, instead of just more "colorful", featuring gratuitous animation?

是否至少有一两个开发人员具有一些设计敏感性,并且具有最终设计权限的个人是否对开发问题有很好的理解,以便您可以利用 WPF 功能来创建实际上更好的东西,而不仅仅是更“丰富多彩” ,以免费动画为特色?

Does some percentage of your target customer base run on integrated graphics chip sets that might not support the features you were planning -- or are they still running Windows 2000, which would eliminate them as customers altogether? Some people would also ask whether your customers actually CARE about enhanced visuals but, having lived through internal company "Our business customers don't care about colors and pictures" debates in the early '90s, I know that well-designed solutions from your competitors will MAKE them care, and the real question is whether the conditions are right, to enable you to offer something that will make them care NOW.

您的目标客户群中是否有一部分运行在可能不支持您计划的功能的集成图形芯片组上——或者他们仍在运行 Windows 2000,这将完全消除他们作为客户的需求?有些人还会问您的客户是否真的关心增强的视觉效果,但是,在 90 年代初经历过公司内部“我们的商业客户不关心颜色和图片”的辩论后,我知道您的竞争对手设计的精心设计的解决方案会让他们关心,真正的问题是条件是否合适,使您能够提供一些让他们现在关心的东西。

Does the project involve grounds-up development, at least for the presentation layer, to avoid the additional complexity of trying to hook into incompatible legacy scaffolding (Interop with Win Forms is NOT seamless)?

该项目是否涉及基础开发,至少对于表示层,以避免尝试连接不兼容的遗留脚手架(与 Win Forms 互操作不是无缝的)的额外复杂性?

Can your manager accept (or be distracted from noticing) a significant DROP in developer productivity for four to six months?

您的经理能否接受(或分散注意力)在四到六个月内开发人员生产力显着下降?

This last issue is due to what I like to think of as the "FizzBin" nature of WPF, with ten different ways to implement any task, and no apparent reason to prefer one approach to another, and little guidance available to help you make a choice. Not only will the shortcomings of whatever choice you make become clear only much later in the project, but you are virtually guaranteed to have every developer on your project adopting a different approach, resulting in a major maintenance headache. Most frustrating of all are the inconsistencies that constantly trip you up, as you try to learn the framework.

最后一个问题是由于我喜欢将 WPF 视为“FizzBin”性质,有十种不同的方式来实现任何任务,并且没有明显的理由偏向于另一种方法,并且几乎没有指导可帮助您制作选择。您所做的任何选择的缺点不仅会在项目后期变得明显,而且您几乎可以保证项目中的每个开发人员都采用不同的方法,从而导致严重的维护问题。最令人沮丧的是,当您尝试学习框架时,不一致会不断地让您失望。

You can find more in-depth WPF-related information in an entry on my blog:

您可以在我的博客的条目中找到更深入的 WPF 相关信息:

http://missedmemo.com/blog/2008/09/13/WPFTheFizzBinAPI.aspx

http://missedmemo.com/blog/2008/09/13/WPFTheFizzBinAPI.aspx

回答by cletus

WPF requires either Windows Vista or Windows XP SP2, which is not an onerous requirement, but it is a relevant one. If you want to run on Windows 2000 (which some people still do), then WPF won't work for you.

WPF 需要 Windows Vista 或 Windows XP SP2,这不是一个繁重的要求,但它是相关的。如果您想在 Windows 2000 上运行(有些人仍然这样做),那么 WPF 将不适合您。

WPF is also a newer technology and not as proven as Windows Forms so you might choose Windows Forms as a less risky option, particularly for larger applications.

WPF 也是一项较新的技术,不像 Windows Forms 那样成熟,因此您可以选择 Windows Forms 作为风险较小的选项,尤其是对于较大的应用程序。

That being said, yes WPF is the future. Visual Studio 2010 is being rewritten in WPF, which will probably be the largest WPF application to date and it will also be a real test for the technology.

话虽如此,是的 WPF 是未来。Visual Studio 2010 正在用 WPF 重写,这可能是迄今为止最大的 WPF 应用程序,也将是对该技术的真正考验。

Obviously, legacy Windows Forms applications would be another situation where it is the correct choice.

显然,旧的 Windows 窗体应用程序将是另一种情况,它是正确的选择。

回答by John Rudy

As others have said, there are advantages and disadvantages either way you go here. The advantages of WPF, as others have said, include:

正如其他人所说,无论采用哪种方式,都有优点和缺点。正如其他人所说,WPF 的优点包括:

  • The ability to make very rich UIs relativelyeasily.
  • Easier animation and special effects
  • Inherent scalability (use the Windows Vista magnifier tool on a WPF application, and on a Windows Forms application: Note that in the WPF application, all the vector art scales beautifully)
  • (OPINION ALERT) I feel it's "easier" to do document-oriented systems in WPF
  • 能够相对容易地制作非常丰富的 UI 。
  • 更简单的动画和特效
  • 固有的可伸缩性(在 WPF 应用程序和 Windows 窗体应用程序上使用 Windows Vista 放大镜工具:请注意,在 WPF 应用程序中,所有矢量图都可以很好地缩放)
  • (OPINION ALERT) 我觉得在 WPF 中做面向文档的系统“更容易”

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

但是,WPF 有一些缺点,其中 Windows 窗体名列前茅:

  • WPF's in-box control suite is far more limited than that of Windows Forms.
  • There's greater support in the third-party control space for Windows Forms. (That's changing, of course, but think about it: Windows Forms has been around since 2001; WPF just a few years. By advantage of time, Windows Forms has greater support in the community.)
  • Most developers already know Windows Forms; WPF provides a new learning curve
  • WPF 的内置控件套件远比 Windows Forms 的有限。
  • 对 Windows 窗体的第三方控件空间有更大的支持。(当然,这种情况正在发生变化,但请想一想:Windows Forms 自 2001 年以来就已经存在;WPF 才几年。趁着时间,Windows Forms 在社区中获得了更大的支持。)
  • 大多数开发人员已经知道 Windows 窗体;WPF 提供了新的学习曲线

Finally, bear in mind that you can create great, attractive and engaging UIs in either tool, if you do the work (or use the right third-party tools). At the end of the day, neither is necessarily better in all circumstances. Use what feels right for the project.

最后,请记住,如果您完成工作(或使用正确的第三方工具),您可以在任一工具中创建出色、有吸引力且引人入胜的 UI。归根结底,两者都不一定在所有情况下都更好。使用适合项目的东西。

回答by Mark

The programming model for WPF is more open and flexible than Windows Forms is, but like ASP.NET MVC, it requires a little more discipline in terms of correctly implementing Model-View-ViewModel patterns.

WPF 的编程模型比 Windows 窗体更加开放和灵活,但与 ASP.NET MVC 一样,它在正确实现 Model-View-ViewModel 模式方面需要更多的纪律。

My first LOBapplication with WPF ended up as an utter failuire, because it was a resource hog which brought my end-user's very-low-end laptops grinding to a halt... and this was ultimately because I just lept in with WPF + LINQ to SQLand expected a good result... and this is where WPF diverges so strongly from Windows Forms... In Windows Forms, you can get away with that sort of thing. WPF is much heavier on resources than Windows Forms, and if you don't architect your application to be lean, you end up with a 800-pound gorilla.

我的第一个使用 WPF 的LOB应用程序最终完全失败了,因为它占用了大量资源,使我的最终用户的超低端笔记本电脑陷入停顿……这最终是因为我刚刚加入了 WPF + LINQ to SQL并期待一个好的结果......这就是 WPF 与 Windows 窗体如此强烈不同的地方......在 Windows 窗体中,你可以摆脱那种事情。WPF 在资源上比 Windows 窗体重得多,如果您不将应用程序构建为精简版,您最终会得到一个 800 磅的大猩猩。

Don't shy away from WPF... explore it. But be aware that the acceptable sins of Windows Forms coding won't produce good results in WPF. They're fundamentally different engines, which lend themselves to fundamentally different coding patterns.

不要回避WPF...探索它。但请注意,Windows 窗体编码的可接受的错误不会在 WPF 中产生好的结果。它们是根本不同的引擎,它们适用于根本不同的编码模式。

Last Word: If you do go ahead with WPF, get well acquianted with data virtualization for use with lists and grids. What is a simple data-bound ListItem or GridCell ends up being a hefty logical + visual object-graph in WPF, and if you don't learn how to virtualize, you application won't perform well on large data sets.

最后一句话:如果您继续使用 WPF,请熟悉用于列表和网格的数据虚拟化。什么是简单的数据绑定 ListItem 或 GridCell 最终会成为 WPF 中庞大的逻辑 + 视觉对象图,如果您不学习如何虚​​拟化,您的应用程序将无法在大型数据集上表现良好。

回答by denis phillips

There is a very steep learning curve to WPF, and I recommend you get the obvious books first (Adam Nathan, Sells/Griffiths, and Chris Anderson) and blogs (Josh Smith, etc.). Just be prepared for it, and make sure your project allows you the time to learn WPF.

WPF 有一个非常陡峭的学习曲线,我建议您首先阅读明显的书籍(Adam NathanSells/GriffithsChris Anderson)和博客(Josh Smith等)。只需为此做好准备,并确保您的项目允许您有时间学习 WPF。

In addition to learning the technology, spend some time learning the patterns used to construct WPF applications. Model View ViewModel(MVVM) seems to be the one that has gained a great deal of acceptance.

除了学习技术之外,还要花一些时间学习用于构建 WPF 应用程序的模式。模型视图视图模型(MVVM) 似乎是获得了大量接受的模型。

Personally, I think WPF is worth it but be forewarned. Also note that you effectively restrict your users to Windows XP SP2+ and Windows Vista. We've made that decision, but you may have some different requirements.

就个人而言,我认为 WPF 是值得的,但要预先警告。另请注意,您实际上将用户限制为 Windows XP SP2+ 和 Windows Vista。我们已经做出了这个决定,但您可能有一些不同的要求。

回答by Oleg

Both of technologies have their pros and cons. In a large application with a "classic" UI I'd use Windows Forms. In an application which require a rich user interface (skinning, animations, changing user interface) I'd choose WPF. Please check the article WPF vs. Windows Formscomparing WPF and Windows Forms.

这两种技术各有优缺点。在具有“经典”UI 的大型应用程序中,我会使用 Windows 窗体。在需要丰富用户界面(外观、动画、更改用户界面)的应用程序中,我会选择 WPF。请查看文章WPF 与 Windows 窗体比较 WPF 和 Windows 窗体。

回答by Steve Wortham

Aside from the flexibility in UI design, there are some technical advantages to WPF:

除了 UI 设计的灵活性外,WPF 还有一些技术优势:

1.) WPF doesn't rely on GDI objects.Well, I think it uses 2 GDI objects for the instance of the window itself, but that's practically nothing. I've been involved to a certain extent in a very large internal Windows Forms application. The people in our office sometimes run 3 or 4 instances of it simultaneously. The problem is that they frequently run into the 10,000 GDI object limit inherent to Windows 2000, XP and Vista. When that happens the entire OS becomes unresponsive and you'll start to see visual artifacts. The only way to clear it up is to close applications down.

1.) WPF 不依赖于 GDI 对象。好吧,我认为它使用 2 个 GDI 对象作为窗口本身的实例,但这实际上没什么。我在一定程度上参与了一个非常大的内部 Windows 窗体应用程序。我们办公室的人有时会同时运行 3 或 4 个实例。问题是它们经常遇到 Windows 2000、XP 和 Vista 固有的 10,000 GDI 对象限制。当这种情况发生时,整个操作系统变得无响应,您将开始看到视觉伪影。清除它的唯一方法是关闭应用程序。

2.) WPF utilizes the GPU.The ability for WPF to off-load some of the UI processing to the GPU is brilliant. I only expect this aspect of it to get better with time. As a former OpenGL programming hobbyist I can appreciate the power that comes from the GPU. I mean, my $100 video card has 112 cores running at 1.5 GHz each (and that's not top of the line by any means). That kind of parallel processing power can put any quad-core CPU to shame.

2.) WPF 利用 GPU。WPF 将一些 UI 处理卸载到 GPU 的能力非常出色。我只希望它的这方面随着时间的推移而变得更好。作为前 OpenGL 编程爱好者,我可以欣赏 GPU 的强大功能。我的意思是,我 100 美元的显卡有 112 个内核,每个内核都以 1.5 GHz 的频率运行(无论如何这都不是顶级的)。这种并行处理能力可以让任何四核 CPU 相形见绌。

However, WPF is still pretty new. It won't run on Windows 2000. And in fact, a WPF application can be slow to start up after a fresh reboot. I talk about all of this on my blog: http://blog.bucketsoft.com/2009/05/wpf-is-like-fat-super-hero.html

但是,WPF 仍然很新。它不能在 Windows 2000 上运行。事实上,WPF 应用程序在重新启动后启动速度可能很慢。我在我的博客上讨论了所有这些:http: //blog.bucketsoft.com/2009/05/wpf-is-like-fat-super-hero.html