C# 在 Windows 窗体上使用 WPF 是否更好?

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

Is it better to use WPF over Windows Forms?

c#.netwpfwinformsuser-interface

提问by Joan Venge

For a brand new application, is it better to use WPF over Windows Forms? I used Windows Forms before but not much WPF. As far as I know, WPF is the successor to Windows Forms, right?

对于一个全新的应用程序,使用 WPF 比使用 Windows 窗体更好吗?我之前使用过 Windows 窗体,但使用的 WPF 不多。据我所知,WPF 是 Windows Forms 的继承者,对吧?

The application is going to host DirectX Windows (not WPF 3D, but Managed DirectXand SlimDX) with lots of custom controls.

该应用程序将托管带有大量自定义控件的DirectX Windows(不是 WPF 3D,而是Managed DirectXSlimDX)。

EDIT: The application is a 3D related application, editor, such as modo:

EDIT: 应用是一个3D相关的应用,编辑器,比如modo

Image created using the 3D painting program modo.

使用 3D 绘画程序 modo 创建的图像。

采纳答案by Reed Copsey

----EDIT 2 -----

----编辑 2 -----

Given that you're trying to make an editor like the one you showed, I'd recommend going WPF even more. My current project has many features along those lines, as well, and we've decided that the ability to composite WPF with Direct3D content is extremely powerful. It's nice to be able to render your scene into anything - not just a rectangular window. In WinForms, you pretty much were limited to one rectangle, and you had issues with airspace there, too (subtle, but things like flickering issues when menus pull over your hwnd, etc). The WPF compositor with D3DImage gets rid of all of those issues, and lets you use your imagination to construct a very flexible UI. Things like rendering your scene in realtime on the side of a WPF3D object are possible, or using WPF controls directly on top of your d3d scene instead of trying to do the GUI in D3D, etc.

鉴于您正在尝试制作像您展示的那样的编辑器,我建议您更多地使用 WPF。我当前的项目也有许多类似的功能,我们认为将 WPF 与 Direct3D 内容组合的能力非常强大。能够将您的场景渲染成任何东西真是太好了——而不仅仅是一个矩形窗口。在 WinForms 中,您几乎被限制在一个矩形内,并且那里的空域也有问题(微妙的,但是当菜单拉过您的 hwnd 时出现闪烁问题等)。带有 D3DImage 的 WPF 合成器摆脱了所有这些问题,并让您发挥想象力来构建非常灵活的 UI。诸如在 WPF3D 对象的一侧实时渲染场景之类的事情是可能的,

-----Original---------

- - -原来的 - - - - -

If you're going to be hosting DX, you might want to consider it - especially since it gives you the ability to do scene composition with your UI and no airspace issues if you use D3DImage.

如果您要托管 DX,您可能需要考虑它 - 特别是因为它使您能够使用 UI 进行场景合成,并且如果您使用D3DImage则不会出现空域问题。

This does work with SlimDX and WPF.

这确实适用于 SlimDX 和 WPF。

----EDIT-----

- - 编辑 - - -

For more information on the disadvantages of using Direct3D with Winforms, and the advantages of WPF/DX integration, see:

有关在 Winforms 中使用 Direct3D 的缺点以及 WPF/DX 集成优点的更多信息,请参阅:

MSDN Article on Airspace

关于空域的 MSDN 文章

Codeproject arcticle on intro to D3DImage

关于 D3DImage 介绍的 Codeproject 文章

回答by JaredPar

If you're writing lots of custom controls I would choose WPF. Part of WPF's design is to be extendable, composable and the result is that it's dirt simple to write custom controls.

如果您要编写大量自定义控件,我会选择 WPF。WPF 设计的一部分是可扩展、可组合,结果是编写自定义控件非常简单。

I've done a bit of custom control writing in WinForms and it can be very challanging at time. Layout will really take some time away from you. Several weekends of my life were paid to WinForm Control Layout. Writing the equivalent controls in WPF was a cinch.

我已经在 WinForms 中编写了一些自定义控件,这在时间上可能非常具有挑战性。布局真的需要一些时间远离你。我生命中的几个周末都花在了 WinForm 控件布局上。在 WPF 中编写等效控件非常简单。

回答by Dan Blair

AFAIK WPF is the successor to WinForms, right?

AFAIK WPF 是 WinForms 的继承者,对吧?

I don't believe this is true as much as MS might like to push this. There is just too much legacy code written on Winforms for that to be a realistic thing in the near or even somewhat distant future.

我不相信这是真的,因为 MS 可能想推动这一点。在 Winforms 上编写的遗留代码太多了,以至于在不久的将来甚至有些遥远的将来都无法实现。

So with that in mind, if you would like to, and can afford to spend the time learning WPF I would do that. Otherwise I would use Winforms.

因此,考虑到这一点,如果您愿意并且有能力花时间学习 WPF,我会这样做。否则我会使用 Winforms。

回答by billb

We dealt with this question about 9 months ago. We decided to go with WPF and so far we're happy with the decision. Yes, there is a learning curve. It's fairly considerable especially coming from WinForms where you have so much to unlearn. I also recommend you have access to a designer otherwise your application will probably look a bit shabby. Also be prepared for some WPF gotchas that will have you spending hours scratching your head saying 'why was this so hard'.

我们大约在 9 个月前处理过这个问题。我们决定使用 WPF,到目前为止我们对这个决定感到满意。是的,有一个学习曲线。这是相当可观的,尤其是来自 WinForms,你有很多东西要忘掉。我还建议您访问设计器,否则您的应用程序可能看起来有点破旧。还要为一些 WPF 问题做好准备,这些问题会让您花几个小时摸不着头脑说“为什么这么难”。

But WPF is a step ahead. The data binding, templating and complete control of how you want your windows to look makes you think that this how WinForms should've been originally.

但 WPF 领先一步。数据绑定、模板化和对您希望窗口外观的完全控制使您认为这应该是 WinForms 最初的样子。

Oh yes, and be prepared to shell out a couple of dollars for some missing controls. There are a couple of things missing like a Date picker and having checkboxes on tree controls (you can actually template this out, but it's not as simple as winforms in that regard). 3.5 SP1 includes a grid control now, thankfully.

哦,是的,并准备为一些缺少的控件支付几美元。缺少一些东西,例如日期选择器和树控件上的复选框(您实际上可以将其模板化,但在这方面不像 winforms 那样简单)。谢天谢地,3.5 SP1 现在包含一个网格控件。

I'm sure I'm missing a bunch more, but that's what I can come up with off the top of my head.

我确定我错过了更多,但这就是我能想到的。

Good luck!

祝你好运!

回答by Brian ONeil

We just went through this too… we started a move to WPF over WinForms. I think that these days it is a pretty easy decision to put WinForms behind you. The tooling is getting better and better and if you can get a true designer on your team there are very few things that you can't put together.

我们也刚刚经历了这个……我们开始通过 WinForms 转向 WPF。我认为现在将 WinForms 放在你身后是一个非常容易的决定。工具越来越好,如果您的团队中可以找到一位真正的设计师,那么几乎没有什么东西是您无法组合在一起的。

We are starting to realize that our future is actually not WPF, but it is Silverlight. As Silverlight matures it is becoming more and more on par with WPF and it allows you to run cross platform. The path of Silverlight is more like Adobe Air and one day it will live outside the browser (actually you can do it now, but it is a pain). I really think that the future for .NET UI is going to be Silverlight.

我们开始意识到我们的未来实际上不是 WPF,而是 Silverlight。随着 Silverlight 的成熟,它变得越来越与 WPF 相提并论,它允许您跨平台运行。Silverlight 的路径更像是 Adob​​e Air,总有一天它会存在于浏览器之外(实际上你现在可以做到,但它很痛苦)。我真的认为 .NET UI 的未来将是 Silverlight。

回答by dkretz

In this case, I'd say follow the lead of your Vendor (i.e. Microsoft). Their own internal production increasingly features WPF; and it's scope (think Silverlight) and depth are clearly greater than WinForms. They also use it increasingly with reference to Best Practices. From all the evidence (here and elsewhere), it appears to be ready for Prime Time developement.

在这种情况下,我会说跟随你的供应商(即微软)的领导。他们自己的内部产品越来越多地采用 WPF;它的范围(想想 Silverlight)和深度显然大于 WinForms。他们还越来越多地参考最佳实践来使用它。从所有证据(这里和其他地方)来看,它似乎已经为黄金时段的开发做好了准备。

You have epressed no particular affinity for WinForms (I would guess what there is is based on familiarity). You'll need to switch eventually. A new project would be a great place to start the process, everything else being equal.

您对 WinForms 没有表现出特别的亲和力(我猜这是基于熟悉程度的)。你最终需要切换。一个新项目将是开始这个​​过程的好地方,其他一切都是平等的。

回答by Cameron MacFarland

Having just recently done a very similar product (3d viewer for mining data in WPF) I can definitely recommend WPF/SlimDX.

最近刚刚完成了一个非常相似的产品(用于在 WPF 中挖掘数据的 3d 查看器),我绝对可以推荐 WPF/SlimDX。

The tools are a bit lax (especially Visual Studio), but WPF makes it much easier to make an app with a bit of Zing, rather than the default control grey that most things have.

这些工具有点松散(尤其是 Visual Studio),但 WPF 使使用 Zing 制作应用程序变得更加容易,而不是大多数东西具有的默认控件灰色。

Using D3DImage from 3.5SP1 it's very easy to host a SlimDX device inside a control.

使用 3.5SP1 中的 D3DImage 可以很容易地在控件内托管 SlimDX 设备。

Overall I'd say WPF isn't better to use, just different. Some things are better, some are worse. But it's definitely the future.

总的来说,我会说 WPF 不是更好用,只是不同。有些事情更好,有些事情更糟。但这绝对是未来。

回答by sipwiz

I found that once I got a handle on Expression Blend and WPF I was more productive at building UIs than with Windows Forms. To my mind that's one of, if not the, biggest factor with a new technology/tool; if you can't acheive the same result as the old tool faster with the new tool then all the bells and whistles, that you only use 10% of the time, won't make up for it.

我发现一旦我掌握了 Expression Blend 和 WPF,我在构建 UI 方面的效率就比使用 Windows 窗体高。在我看来,这是新技术/工具的最大因素之一;如果您无法使用新工具更快地获得与旧工具相同的结果,那么您仅使用 10% 的时间的所有花里胡哨都无法弥补。

Apart from that there are other advantages WPF has over Forms:

除此之外,WPF 与 Forms 相比还有其他优势:

  • Having the interface in XAML makes it so much easier to fine tune all those obscure properties or to cut and paste a whole section of one control into another,

  • WPF makes it a lot easier to modularise an interface, breaking a screen down control by control. Forms can do the same to an extent but I always found it a battle,

  • Some of the bells and whistles are cool. I've found the use of animations (storyboards) really useful for data entry screens where you need different entry boxes depending on which product is chosen or such. With Forms you'd have to create a separate panel/form for each data entry screen, with WPF I use an animation to hide and move the various text boxes around.

  • 在 XAML 中拥有界面使得微调所有这些晦涩的属性或将一个控件的整个部分剪切并粘贴到另一个控件中变得更加容易,

  • WPF 使模块化界面变得更加容易,逐个控制地分解屏幕。表格在一定程度上可以做同样的事情,但我总是觉得这是一场战斗,

  • 有些花里胡哨很酷。我发现动画(故事板)的使用对于数据输入屏幕非常有用,您需要根据选择的产品等不同的输入框。使用表单,您必须为每个数据输入屏幕创建一个单独的面板/表单,使用 WPF,我使用动画来隐藏和移动各种文本框。

回答by John

In your case, WPF makes sense. For me, it's an application by application choice because there are a lot of things you can do really well in Windows Forms (like rapid development of business applications). I'm using WPF to write a nice GUI for a media center application (personal project for a media console on my 50-inch plasma screen).

在你的情况下,WPF 是有道理的。对我来说,这是一个由应用程序选择的应用程序,因为在 Windows 窗体中有很多事情你可以做得很好(比如快速开发业务应用程序)。我正在使用 WPF 为媒体中心应用程序(我的 50 英寸等离子屏幕上的媒体控制台的个人项目)编写一个漂亮的 GUI。

However, if I'm writing a data entry and display application, I typically go with Windows Forms for speed of development. The XAMLeditor is better in Visual Studio 2010 but still has a way to go. Without a reference there's no IntelliSense help on a lot of the property attributes so it's impossible to know what goes in them (which is where I spend a lot of my time trying to find those values, either through a book or examples on the web). That said, if you do WPF write, you can make some very compelling user interfaces.

但是,如果我正在编写数据输入和显示应用程序,我通常会使用 Windows 窗体来加快开发速度。在XAML编辑器是更好的Visual Studio 2010中,但仍然有很长的路要走。如果没有参考,很多属性属性就没有 IntelliSense 帮助,因此无法知道它们中的内容(这是我花费大量时间试图找到这些值的地方,无论是通过书籍还是网络上的示例) . 也就是说,如果您编写 WPF,您可以制作一些非常引人注目的用户界面。

回答by Dane

I know this is an old post, but I think it's still a valid question.

我知道这是一个旧帖子,但我认为这仍然是一个有效的问题。

I used WinForms for 3 years at the first company I worked for. There were some really good programmers there, and they found some very clever ways to do some very cool things. WinForms has stood the test of time. It is a solid, reliable and efficient platform. Many people, and many companies both large and small have been using WinForms for years, and the technology has proven itself time and time again. Inertia like that is hard to fight, even if you are Microsoft.

我在我工作的第一家公司使用了 3 年的 WinForms。那里有一些非常优秀的程序员,他们找到了一些非常聪明的方法来做一些非常酷的事情。WinForms 经受住了时间的考验。它是一个坚实、可靠和高效的平台。许多人和许多大大小小的公司多年来一直在使用 WinForms,并且该技术已经一次又一次地证明了自己。即使你是微软,这样的惯性也很难克服。

Still, WPF is definitely the future. Microsoft made that clear by making products such as Visual Studio 2010 and Expression Blend with it. (In using WPF for internal projects, Microsoft was forced to fix many of the issues, such as font rendering and speed issues, that prevented people from adopting it at first. Visual Studio 2010 shows that WPF has long been ready for production.

尽管如此,WPF 绝对是未来。微软通过制作 Visual Studio 2010 和 Expression Blend 等产品来明确说明这一点。(在将 WPF 用于内部项目时,Microsoft 被迫修复了许多问题,例如字体渲染和速度问题,这些问题最初阻止人们采用它。Visual Studio 2010 表明 WPF 早已准备好投入生产。

So, is it better to write your program in WinForms or WPF? The answer is this : WPF. I say this for two reasons. Firstly, because Microsoft is supporting it internally, we can assume that it will be around for a very long time. Microsoft has shown us that it is the next tool of choice which leads me to my next point: it has become superior to WinForms (some people I have spoken to disagree, but it is the truth nonetheless, and is becoming more obvious every day). WPF has a host of features that WinForms will (probably) never be given, such as integrated support amazing data binding, and timeline based animations that make it easy to make amazing themes for your apps, and will have designers begging to make the switch. There is much more to WPF than can ever be described in a comment, but these are two big ones for me :)

那么,用 WinForms 或 WPF 编写程序更好吗?答案是这样的:WPF。我这样说有两个原因。首先,因为微软在内部支持它,我们可以假设它会存在很长时间。微软已经向我们展示了它是下一个选择的工具,这让我想到了我的下一个观点:它已经变得优于 WinForms(我说过有些人不同意,但这是事实,而且每天都变得越来越明显) . WPF 有许多 WinForms(可能)永远不会提供的功能,例如集成支持惊人的数据绑定和基于时间线的动画,可以轻松为您的应用程序制作令人惊叹的主题,并且会让设计师乞求进行转换。WPF 的内容比评论中所能描述的要多得多,但对我来说这是两个重要的 :)

This being said, I know there is nothing you can do in WPF that you cannot do in WinForms. You can do anything in any language if you want to badly enough. The point is that where WinForm's will leave you scouring google for how somebody else did something cool, WPF ships with built-in support for all the features that Microsoft considers to be the future of Windows and puts them right at your fingertips.

话虽如此,我知道在 WPF 中没有什么是在 WinForms 中不能做的。如果你非常想要,你可以用任何语言做任何事情。关键是 WinForm 会让你在谷歌上搜索其他人如何做一些很酷的事情,WPF 附带对 Microsoft 认为是 Windows 未来的所有功能的内置支持,并使它们触手可及。

Over time, people will become used to new features build directly into Windows, and will come to expect them to be available in all the applications they use. This will reach the point where anything that doesn't support them will feel clunky, less usable, and seem less worth spending money on. (I see Touch integration as the most obvious example of this. Although there are touch screen solutions that clamp onto a monitor and emulate a mouse, on any computer, touch screen technology has come a very long way, and with Windows 8 blurring the line between the desktop and mobile devices, people will likely want to use applications in a way that will be exceptionally difficult to implement if your application uses WinForms.)

随着时间的推移,人们将习惯于直接内置于 Windows 中的新功能,并期望它们在他们使用的所有应用程序中都可用。这将达到任何不支持它们的东西都会感觉笨重,不太可用,并且看起来不值得花钱的地步。(我认为触摸集成是最明显的例子。虽然有一些触摸屏解决方案可以夹在显示器上并模拟鼠标,但在任何计算机上,触摸屏技术已经取得了长足的进步,而 Windows 8 模糊了界限在桌面和移动设备之间,如果您的应用程序使用 WinForms,人们可能希望以一种特别难以实现的方式使用应用程序。)

Sooner or later, I believe that support and development for WinForms will stop, and everybody that left themselves behind will be forced to port, and quickly. However, there are too many programs out there that rely on WinForms for Microsoft to be able to drop it in the near future. Who knows what will happen. Perhaps WinForms will be to Microsoft what Itanium Processors are to Intel, the product that has enough users that to keep it going, even though there are better solutions available. ( http://arstechnica.com/business/news/2011/06/ask-ars-why-itaniumask-ars-with-xeons-improvement-why-bother-with-itanium.ars)

迟早,我相信对 WinForms 的支持和开发将会停止,每个离开自己的人都将被迫移植,而且很快。然而,有太多的程序依赖于 Microsoft 的 WinForms,无法在不久的将来放弃它。谁知道会发生什么。或许 WinForms 之于微软就像安腾处理器之于英特尔一样,即使有更好的解决方案,该产品也有足够的用户来维持它的发展。(http://arstechnica.com/business/news/2011/06/ask-ars-why-itaniumask-ars-with-xeons-improvement-why-bother-with-itanium.ars

So my advice would be this: regarding new development, make the switch to WPF. Send some staff on training courses, or let them play for a while so they can get a better feel for the technology. The learning curve is going to be steep, but doing this will increase your team's skill set because by learning WPF, they will also be well on their way to learning SilverLight, which is similar enough to WPF for people familiar with WPF to be be able to learn it quickly, but is designed for the web rather than the desktop. This is valuable because apps rewritten in WPF will far easier to convert to Web Apps later down the line. WPF is new, but it was made for a reason, not simply for the sake of making something else for people to learn. It was designed to make applications more user friendly, intuitive, and portable. It was designed to help developers make their applications fit into the new age of computers.

所以我的建议是:关于新的开发,切换到 WPF。派一些员工参加培训课程,或者让他们玩一会儿,这样他们就可以更好地了解这项技术。学习曲线会很陡峭,但这样做会提高团队的技能,因为通过学习 WPF,他们也将在学习 SilverLight 的道路上走得很好,这与 WPF 非常相似,熟悉 WPF 的人能够快速学习它,但它是为网络而不是桌面设计的。这很有价值,因为在 WPF 中重写的应用程序以后更容易转换为 Web 应用程序。WPF 是新的,但它是有原因的,而不仅仅是为了制作其他东西供人们学习。它旨在使应用程序更加用户友好、直观和便携。

Regarding applications written in WinForms : I think it's safe to assume that WinForms will still be supported for quite a long time simply because of the amount of support it has gained. I would recommend letting your team familiarize themselves with WPF, and slowly start moving your applications over to WPF. Take all the time that Microsoft allows and don't rush the process, but always keep in mind that, one way or another, WinForms day's are over, and although nobody can be sure what will happen, support for the legacy product will be dropped as soon as it can be justified. Even if WinForms applications are kept around for many years, it is important to remember that times are changing, and we all need to change with them.

关于用 WinForms 编写的应用程序:我认为可以安全地假设 WinForms 仍将在相当长的一段时间内得到支持,因为它已经获得了大量的支持。我建议让您的团队熟悉 WPF,然后慢慢开始将您的应用程序转移到 WPF。在 Microsoft 允许的所有时间里,不要急于完成这个过程,但请始终记住,WinForms 的一天已经结束了,尽管没有人能确定会发生什么,但对旧产品的支持将被取消只要有理由就可以。即使 WinForms 应用程序已经存在多年,重要的是要记住时代在变化,我们都需要随着时代的变化而变化。