C# 什么是 WPF,它与 WinForms 相比如何?

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

What is WPF and how does it compare to WinForms?

c#wpfwindows

提问by Kredns

I've been looking at WPF, but I've never really worked in it (except for 15 minutes, which prompted this question). I looked at this postbut its really about the "Flash" of a WPF. So what is the difference between a Windows Forms application and a WPF application?

我一直在研究 WPF,但我从未真正在其中工作过(除了 15 分钟,它提示了这个问题)。我看了这篇文章,但它实际上是关于 WPF 的“Flash”。那么 Windows 窗体应用程序和 WPF 应用程序之间有什么区别呢?

采纳答案by Scott Dorman

WPF is a vector graphics based UI presentation layer where WinForms is not. Why is that important/interesting? By being vector based, it allows the presentation layer to smoothly scale UI elements to any size without distortion.

WPF 是一个基于矢量图形的 UI 表示层,而 WinForms 则不是。为什么这很重要/有趣?通过基于矢量,它允许表示层平滑地将 UI 元素缩放到任何大小而不会失真。

WPF is also a composable presentation system, which means that pretty much any UI element can be made up of any other UI element. This allows you to easily build up complex UI elements from simpler ones.

WPF 也是一个可组合的展示系统,这意味着几乎任何 UI 元素都可以由任何其他 UI 元素组成。这使您可以轻松地从简单的 UI 元素构建复杂的 UI 元素。

WPF is also fully databinding aware, which means that you can bind any property of a UI element to a .NET object (or a property/method of an object), a property of another UI element, or data. Yes, WinForms supports databinding but in a much more limited way.

WPF 还完全了解数据绑定,这意味着您可以将 UI 元素的任何属性绑定到 .NET 对象(或对象的属性/方法)、另一个 UI 元素的属性或数据。是的,WinForms 支持数据绑定,但方式更为有限。

Finally, WPF is "skinable" or "themeable", which means that as a developer you can use a list box because those are the behaviors you need but someone can "skin" it to look like something completely different.

最后,WPF 是“可换肤的”或“可主题化的”,这意味着作为开发人员,您可以使用列表框,因为这些是您需要的行为,但有人可以对其进行“换肤”以使其看起来完全不同。

Think of a list box of images. You want the content to actually be the image but you still want list box behaviors. This is completely trivial to do in WPF by simply using a listbox and changing the content presentation to contain an image rather than text.

想想一个图像列表框。您希望内容实际上是图像,但您仍然希望列表框的行为。在 WPF 中,通过简单地使用列表框并将内容呈现更改为包含图像而不是文本,这完全是微不足道的。

回答by Kevin Babcock

To answer your question, a WPF application isa Windows application. WPF is Microsoft's new framework (actually, it is a subsystem of the .NET framework 3.0) for writing rich Windows applications. It is meant as an eventual replacement for WinForms (although admittedly the adoption rate is much slower than MS hoped for).

为了回答您的问题,WPF 应用程序一个 Windows 应用程序。WPF 是 Microsoft 的新框架(实际上是 .NET 框架 3.0 的一个子系统),用于编写丰富的 Windows 应用程序。它是作为 WinForms 的最终替代品(尽管不可否认,采用率比 MS 所希望的要慢得多)。

回答by Raumornie

A good way of looking at this might start with asking what exactly Winforms is.

看待这个问题的一个好方法可能是从询问 Winforms 到底是什么开始。

Both Winforms and WPF are frameworks designed to make the UI layer of an application easier to code. The really old folks around here might be able to speak about how writing the windows version of "Hello, World" could take 4 pages or so of code. Also, rocks were a good deal softer then and we had to fight of giant lizards while we coded. The Winforms library and designer takes a lot of the common tasks and makes them easier to write.

Winforms 和 WPF 都是旨在使应用程序的 UI 层更易于编码的框架。这里真正的老人们可能会谈论编写 Windows 版本的“Hello, World”可能需要 4 页左右的代码。此外,当时的岩石要软得多,我们在编码时不得不与巨大的蜥蜴战斗。Winforms 库和设计器承担了许多常见任务,并使它们更易于编写。

WPF does the same thing, but with the awareness that those common tasks might now include much more visually interesting things, in addition to including a lot of things that Winforms did not necessarily consider to be part of the UI layer. The way WPF supports commanding, triggers, and databinding are all great parts of the framework, but the core reason for it is the same core reason Winforms had for existing in the first place.

WPF 做同样的事情,但意识到这些常见任务现在可能包括更多视觉上有趣的东西,此外还包括很多 Winforms 不一定认为是 UI 层一部分的东西。WPF 支持命令、触发器和数据绑定的方式都是该框架的重要组成部分,但其核心原因与 Winforms 最初存在的核心原因相同。

WPFs improvement here is that, instead of giving you the option of either writing a completely custom control from scratch or forcing you to use a single set of controls with limited customization capabilities, you may now separate the function of a control from its appearance. The ability to describe how our controls look in XAML and keep that separate from how the controls work in code is very similar to the HTML/Code model that web programmers are used to working with.

WPF 的改进之处在于,您现在可以将控件的功能与其外观分开,而不是让您选择从头开始编写完全自定义的控件或强制您使用具有有限自定义功能的一组控件。描述我们的控件在 XAML 中的外观并将其与代码中控件的工作方式分开的能力与 Web 程序员习惯使用的 HTML/代码模型非常相似。

A good WPF application follows the same model that a good Winforms application would; keeping as much stuff as possible out of the UI layer. The core logic of the application and the data layer should be the same, but there are now easier ways of making the visuals more impressive, which is likely why most of the information you've seen on it involves the flashier visual stuff. If you're looking to learn WPF, you can actually start by using it almost exactly as you would Winforms and then refactoring the other features in as you grasp them. For an excellent example of this, I highly recommend Scott Hanselman's series of blog posts on the development of BabySmash, which start here. It's a great walkthrough of the process, both in code and in thought.

一个好的 WPF 应用程序遵循一个好的 Winforms 应用程序所遵循的模型;将尽可能多的东西保留在 UI 层之外。应用程序和数据层的核心逻辑应该是相同的,但现在有更简单的方法可以让视觉效果更令人印象深刻,这可能就是为什么你在上面看到的大部分信息都涉及更炫的视觉内容。如果您想学习 WPF,您实际上可以像使用 Winforms 一样使用它,然后在掌握它们时重构其他功能。作为一个很好的例子,我强烈推荐 Scott Hanselman 的关于 BabySmash 开发的系列博客文章,从这里开始。无论是在代码中还是在思想上,这是一个很好的过程演练。

回答by Scott Hanselman

Start here, there's dozens and dozens of videos: http://www.windowsclient.net/learn

从这里开始,有几十个视频:http: //www.windowsclient.net/learn

In this case, the marketing schpiel is pretty good:

在这种情况下,营销 schpiel 非常好:

**"Windows Forms is a set of classes in the .NET Framework that enables rapid development of rich Windows client applications, with powerful, extensible libraries for user-interface controls and graphics. You can incorporate WPF in your Windows Forms applications through WPF-Windows Forms interoperability in the .NET 3.5 Framework."*

**“Windows Forms 是 .NET Framework 中的一组类,它支持丰富的 Windows 客户端应用程序的快速开发,具有强大的、可扩展的用户界面控件和图形库。您可以通过 WPF 将 WPF 合并到您的 Windows Forms 应用程序中—— .NET 3.5 框架中的 Windows 窗体互操作性。"*

*"WPF, a component of Microsoft .NET Framework 3.5, empowers you to build the next-generation of Windows user experiences. WPF supports UI, media, documents, hardware acceleration, vector graphics, scalability to different form factors, interactive data visualization, and superior content readability."**

*“WPF 是 Microsoft .NET Framework 3.5 的一个组件,使您能够构建下一代 Windows 用户体验。WPF 支持 UI、媒体、文档、硬件加速、矢量图形、不同外形的可扩展性、交互式数据可视化、和卓越的内容可读性。”**

With WPF you get WAY better graphics support, way more powerful and flexible databinding (if you're working with Data, use WPF.)

使用 WPF,您可以获得更好的图形支持,更强大和更灵活的数据绑定(如果您正在使用数据,请使用 WPF。)

回答by Shahid

WPF stands for Windows Presentation Foundation. While WinForms technology depends on GDI/GDI+, WPF is built directly on top of DirectX. That means you can do much more than you can with WinForms, while getting still using the .NET Framework Class library. You can build rich user interfaces, 2D/3D games, presentations and much more. WPF is much like a Flash (swf) movie, without the element "Movie". WPF is based on .NET and can be used to build rich client side web enabled applications.

WPF代表W¯¯INDOWS Presentation ˚Foundation。WinForms 技术依赖于 GDI/GDI+,而 WPF 直接构建在 DirectX 之上。这意味着您可以比使用 WinForms 做更多的事情,同时仍然可以使用 .NET Framework 类库。您可以构建丰富的用户界面、2D/3D 游戏、演示等等。WPF 很像 Flash (swf) 电影,没有“电影”元素。WPF 基于 .NET,可用于构建富客户端 Web 应用程序。

回答by a1ashiish

  1. WPF is essentially a new API for creating a Graphical User Interfaces for the Windows Platform.
  2. WPF is more than just a next-generation presentation system for building Windows Client application along with visually stunning user interfaces.
  3. WPF is builds on top of the DirectX (Direct3D), instead of relying on the older GDI/GDI+ subsystem.
  4. WPF is a vector graphics based UI presentation layer and being vector based it allows the presentation layer to smoothly scale UI elements to any size without distortion.
  5. WPF is “skin-able” and “theme-able”. It means WPF allows changing the look and feel of any UI control.
  1. WPF 本质上是一个用于为 Windows 平台创建图形用户界面的新 API。
  2. WPF 不仅仅是用于构建 Windows 客户端应用程序以及视觉上令人惊叹的用户界面的下一代演示系统。
  3. WPF 构建在 DirectX (Direct3D) 之上,而不是依赖于较旧的 GDI/GDI+ 子系统。
  4. WPF 是基于矢量图形的 UI 表示层,并且基于矢量,它允许表示层将 UI 元素平滑地缩放到任何大小而不会失真。
  5. WPF 是“可换肤的”和“可主题化的”。这意味着 WPF 允许更改任何 UI 控件的外观和感觉。

回答by david.barkhuizen

From an implementation perspective, one of the main differences is that WPF uses a strictly enforced model-view-viewmodel M-V-VM architecture, with the view being defined in a custom markup language, XAML, the viewmodel being a class with accessible properties, and the model effectively being the data access layer. WinForms, by comparison, has no such explicit separation - although by convention the different aspects may be grouped in different C# files, its all still code. In practice this makes WPF development more like web DOM development, with the WPF framework running in the background and mediating.

从实现的角度来看,主要区别之一是 WPF 使用严格执行的模型-视图-视图模型 MV-VM 架构,视图是用自定义标记语言 XAML 定义的,视图模型是具有可访问属性的类,并且该模型实际上是数据访问层。相比之下,WinForms 没有这种明确的分离——尽管按照惯例,不同的方面可能被分组在不同的 C# 文件中,但它仍然是代码。在实践中,这使得 WPF 开发更像 Web DOM 开发,WPF 框架在后台运行并进行中介。