WPF 和 Windows 窗体应用程序的显示差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18947133/
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
Difference in display of WPF and Windows Forms applications
提问by user2804762
Actually, I have started learning WPF. I have few months of experience in developing Windows Forms applications. Though, I am getting the meaning of a WPF application, but still I am not able to differentiate the difference between two, on the basis of their output.
其实,我已经开始学习WPF了。我在开发 Windows 窗体应用程序方面有几个月的经验。虽然,我明白了 WPF 应用程序的含义,但我仍然无法根据它们的输出来区分两者之间的区别。
With reference to this Link: Device Independent Pixel (DPI), I have learnt that whenever the operating system render a WPF application it manages its size itself according to its resolution.
参考此链接:设备独立像素 (DPI),我了解到每当操作系统呈现 WPF 应用程序时,它都会根据其分辨率自行管理其大小。
So to check this difference, I created two demo applications in both frameworks and changed the resolutions as well.. but I didn't find any satisfactory difference. Which could explain it is a WPF application and this one is a Windows Forms application.
因此,为了检查这种差异,我在两个框架中创建了两个演示应用程序并更改了分辨率……但我没有发现任何令人满意的差异。这可以解释它是一个 WPF 应用程序,而这个是一个 Windows 窗体应用程序。
It does not create any scroll bar on maximizing and doesn't make the button big or small on changing the resolution.
它不会在最大化时创建任何滚动条,也不会在更改分辨率时使按钮变大或变小。
I have read somewhere that Visual Studio 2010 has been rewritten in WPF. But in my experimentation I saw that, (on changing the resolution of desktop) it makes text and graphics unreadable/blurry. On re-sizing its window, everything was getting hidden except the menu-bar. And the menu-bar content was shifting its positioning, e.g. far right one menu items were shifting down. Why?
我在某处读到 Visual Studio 2010 已用 WPF 重写。但是在我的实验中,我看到(在更改桌面分辨率时)它会使文本和图形不可读/模糊。在重新调整其窗口大小时,除了菜单栏之外,所有内容都被隐藏了。并且菜单栏内容正在改变其定位,例如最右边的一个菜单项正在向下移动。为什么?
Kindly make me correct & explain a little more bit (this display issue) too.
请让我更正并解释更多(这个显示问题)。
回答by Bizhan
To answer this question properly I should write a whole chapter, but I keep it short:
为了正确回答这个问题,我应该写一整章,但我保持简短:
There are three major differences between a WPF application and a Windows Forms application: Layout, Render, Presentation
WPF 应用程序和 Windows 窗体应用程序之间存在三个主要区别:布局、渲染、演示
Layout:
布局:
WPF layout systemprovides a greater flexibility in arranging the elements on the fly. It is based on the Element Bounding Box(as opposed to precise pixelsin WinForms) and Measureand Arrangemechanics (as opposed to UpdateLayout
in WinForms) that automatically and dynamically finds the place for each element without any need for a manual update.
WPF 布局系统在动态排列元素方面提供了更大的灵活性。它基于元素边界框(与WinForms 中的精确像素相反)和测量和排列机制(与UpdateLayout
WinForms 中相反),可自动动态地为每个元素找到位置,无需手动更新。
Basically, all elements bounding box are measured first and then are arranged using multiple methods such as Measure, MeasureCore, ArrangeCore, MeasureOverride, etc.
基本上,所有元素边界框都会先测量,然后使用Measure、MeasureCore、ArrangeCore、MeasureOverride等多种方法进行排列。
Unlike WinForms, where you have a pixel-perfect size for everything, in WPF you have much more options and complexity such as Width, ActualWidthand DesiredSize(and even Transformsas LayoutTransform) for the same element.
不同的WinForms,你有一个完美的像素大小的一切,在WPF你有很多更多的选择和复杂性,如宽度,ActualWidth的和DesiredSize(甚至变换为LayoutTransform对同一元素)。
This is why
这就是为什么
As you type in a WPF TextBox, its width might increase and push other elements away or even push some elements into a new row (like the menu bar you've observed)
As the size of a control changes, it affects the available space for other elements. So their size and location might change accordingly.
When the window is being re-sized or resolution is changed, it immediately updates the layout and changes the size of elements in order to fill or fit the space. Here you'll find out more about Layouts.
using
Margin
alone (without using layout capabilities) to arrange elements is not the best idea in WPF.
当您输入 WPF 文本框时,其宽度可能会增加并将其他元素推开,甚至将某些元素推入新行(如您观察到的菜单栏)
随着控件大小的变化,它会影响其他元素的可用空间。所以它们的大小和位置可能会相应地改变。
当窗口被重新调整大小或分辨率改变时,它会立即更新布局并更改元素的大小以填充或适应空间。 在这里,您将了解有关布局的更多信息。
Margin
单独使用(不使用布局功能)来排列元素在 WPF 中并不是最好的主意。
Render:
使成为:
WPF uses double
data type for Layout (as opposed to pixel-perfect WinForms) and therefore you might see the edges blurry sometimes, but it can be avoided with SnapToDevicePixels=true
.
WPF 使用double
布局数据类型(而不是像素完美的 WinForms),因此您有时可能会看到边缘模糊,但可以通过SnapToDevicePixels=true
.
WPF is much more efficient in utilizing the GPU to render a GUI. Try a grid of 30x30 TextBoxes in a Windows Forms application and a WPF application. No matter how messy you write the WPF, it never blinks and it still runs much faster than Windows Forms. Even adding a handful of animations, visual effects and styles on them does not hurt your performance like in Windows Forms.
WPF 在利用 GPU 呈现 GUI 方面效率更高。在 Windows 窗体应用程序和 WPF 应用程序中尝试使用 30x30 文本框的网格。无论您编写的 WPF 多么混乱,它都不会闪烁,而且运行速度仍然比 Windows 窗体快得多。即使在其上添加少量动画、视觉效果和样式,也不会像在 Windows 窗体中那样影响您的性能。
Remark: To avoid a speed decrease and blinking in a Windows Forms application, you should set DoubleBuffer of the form to "true".
备注:为避免 Windows Forms 应用程序的速度下降和闪烁,您应该将表单的 DoubleBuffer 设置为“true”。
You can use any Transform
as RenderTransformto easily implement smooth zoom/rotate, or develop custom GPU-based shader effects, and much more in WPF. (I think everyone agrees that doing such things in WinForms is feasible but real painand you most likely will give up and move to GDI+or DXif not out of frustration then because of the bad performance.)
您可以使用 anyTransform
作为RenderTransform轻松实现平滑缩放/旋转,或开发基于 GPU 的自定义着色器效果,以及 WPF 中的更多功能。(我想每个人都同意在 WinForms 中做这样的事情是可行的,但真的很痛苦,如果不是因为性能不佳,你很可能会放弃并转向GDI+或DX。)
And the last and the most important:
最后也是最重要的:
Focus on presentation:
重点介绍:
When develping WPF Applications you have to stop thinking in Windows Forms: No more UI events, accessing controls by their namesand writing logic in code-behindand start to think in WPF: Binding, Commands, Resources, Styles, Templates, Converters, DependencyProperties and their callbacks.
在开发 WPF 应用程序时,您必须停止在 Windows 窗体中思考:不再有UI 事件、通过控件名称访问控件并在代码隐藏中编写逻辑,而开始在 WPF 中思考:绑定、命令、资源、样式、模板、转换器、DependencyProperties和他们的回调。
The real power of WPF lies in separation of 'View' and 'Logic', Which can be achieved using the MVVM pattern.
It makes the most visually-complicated problems quite simple and easy to develop and easy to write Unit Tests for.
Once you got the hang of it, you will realize there's no limit in how you can present the dataor show off an awesome GUI looks.
WPF 的真正威力在于“视图”和“逻辑”的分离,这可以使用MVVM 模式来实现。
它使视觉上最复杂的问题变得非常简单,易于开发,并且易于为.
一旦掌握了窍门,您就会意识到展示数据或展示令人敬畏的 GUI 外观的方式是没有限制的。
If you've planned to switch to WPF, you've made the right decision. Always stick to MVVMand you'll be fine! i.e. avoid code behind(coding in .xaml.cs files), XAML names(unless used only within XAML) and UI eventsas much as possible.
如果您计划改用 WPF,那么您就做出了正确的决定。始终坚持 MVVM,你会没事的!即尽可能避免代码隐藏(在 .xaml.cs 文件中编码)、XAML 名称(除非仅在 XAML 中使用)和UI 事件。
回答by Madhavan
Windows Forms(WinForms) and Windows Presentation Foundation(WPF) are two different ways of building the user interface for your application. Windows Formsis the older technology and its controls are found in the System.Windows.Forms namespace
. WPFis a newer technology and its controls are found in the System.Windows.Controls
namespace.
Windows 窗体(WinForms) 和Windows Presentation Foundation(WPF) 是为应用程序构建用户界面的两种不同方式。Windows 窗体是较旧的技术,其控件位于System.Windows.Forms namespace
. WPF是一项较新的技术,其控件位于System.Windows.Controls
命名空间中。
WPF
WPF
Pros:
优点:
- Powerful styling and skinning structure
- Easy to create your own look and feel
- Does support Windows Forms
- The future technology for developing Windows Vista applications
- The ability to reuse existing code
- Highly advanced data binding possible
- 强大的造型和蒙皮结构
- 轻松创建自己的外观和感觉
- 是否支持 Windows 窗体
- 用于开发 Windows Vista 应用程序的未来技术
- 重用现有代码的能力
- 可实现高度先进的数据绑定
Cons:
缺点:
- Declarative vs. procedural code
- Requires .NET Framework 3.0
- Compared to Windows Forms, still in development phase
- Requires Dx9 compatible video card for advanced graphics
- 声明性与程序性代码
- 需要 .NET 框架 3.0
- 与 Windows Forms 相比,仍处于开发阶段
- 需要兼容 Dx9 的显卡才能获得高级图形
Windows Forms
Windows 窗体
Pros:
优点:
- Extensive documentation to be found on the Internet
- Plenty of examples
- Does support WPF
- 可在 Internet 上找到的大量文档
- 大量的例子
- 是否支持 WPF
Cons:
缺点:
- How long will this be supported? (I've read somewhere that Microsoft is just developing WPF now, only maintenance for Windows Forms).
- Design your own look and feel in an application is a lot of work.
- 这将支持多长时间?(我在某处读到微软现在只是在开发 WPF,只维护 Windows 窗体)。
- 在应用程序中设计您自己的外观和感觉需要大量工作。