JavaFX 与 WPF 相比如何?

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

How does JavaFX compare to WPF?

wpfjavafx-2javafx

提问by Aviad P.

I'm mostly a C# programmer, I stopped writing Java about 10 years ago, but I try to keep up with the technology in Java by reading articles, talking with friends, etc.

我主要是一名 C# 程序员,大约 10 年前我停止编写 Java,但我尝试通过阅读文章、与朋友交谈等来跟上 Java 技术。

I've heard about the new rich GUI framework called JavaFX, but couldn't find any resource comparing it with non-Java parallels.

我听说过名为 JavaFX 的新富 GUI 框架,但找不到任何资源将其与非 Java 并行项进行比较。

Since I'm very familiar with C# and WPF, I'd like to get a feel about how similar or different the two technologies are.

由于我对 C# 和 WPF 非常熟悉,因此我想了解一下这两种技术的相似或不同之处。

EDIT: Seeing as there's no answers coming, I'll try to be more specific:

编辑:看到没有答案,我会尝试更具体:

  1. WPF uses XAML to create the visual tree, does JavaFX have something similar?
  2. WPF is best used with binding to a view model in an MVVM pattern, does JavaFX also make extensive use of binding?
  3. WPF utilizes the GPU for rendering, does JavaFX do the same?
  4. How does Silverlight compare to JavaFX when run through a browser on a net pc?
  1. WPF 使用 XAML 创建可视化树,JavaFX 有类似的吗?
  2. WPF 最适合用于绑定到 MVVM 模式中的视图模型,JavaFX 是否也广泛使用了绑定?
  3. WPF 使用 GPU 进行渲染,JavaFX 是否也这样做?
  4. 当通过网络 PC 上的浏览​​器运行时,Silverlight 与 JavaFX 相比如何?

... More to come...

……更多……

I'm changing this to community wiki so that the comparisons can keep getting updated (hopefully).

我正在将其更改为社区维基,以便比较可以不断更新(希望如此)。

回答by Benjamin Gale

I have been learning JavaFX for the last couple of weeks. Here is a high level overview of how it compares to WPF in my eyes:

过去几周我一直在学习 JavaFX。这是我眼中它与 WPF 的比较的高级概述:

All of my comments are related to JavaFX 2.0. This information will probably be subject to change as the platform is still fairly immature and is being actively developed.

我所有的评论都与 JavaFX 2.0 相关。由于该平台还相当不成熟并且正在积极开发中,因此这些信息可能会发生变化。

Graphics

图形

Like WPF, JavaFX uses a retained graphics rendering system. The user interface comprises a scene graph which is composed of 'nodes' which can be thought of as conceptually similar to WPF's UIElement.

与 WPF 一样,JavaFX 使用保留的图形渲染系统。用户界面包含一个场景图,该图由“节点”组成,可以认为在概念上类似于 WPF UIElement

JavaFX will offload the graphics rendering to the GPU if available. The graphics system uses DirectX on Windows and OpenGL on other platforms.

如果可用,JavaFX 会将图形渲染卸载到 GPU。图形系统在 Windows 上使用 DirectX,在其他平台上使用 OpenGL。

Markup

标记

JavaFX user interfaces can be created both in code and via FXML markup which is similar to XAML in that the object graph can be created by nesting elements.

JavaFX 用户界面既可以在代码中创建,也可以通过 FXML 标记创建,这类似于 XAML,因为可以通过嵌套元素创建对象图。

FXML has some similar features to XAML such as property binding (simple expressions only) and binding to event handlers (any onEventmethod). Event handlers can be declared in-line but typically you would bind to an event in the associated controller.

FXML 具有一些与 XAML 相似的功能,例如属性绑定(仅限简单表达式)和绑定到事件处理程序(任何onEvent方法)。事件处理程序可以内联声明,但通常您会绑定到关联控制器中的事件。

FXML files can have an associated controller which allows you to declare complex event handlers and to set up bindings between properties. This is a controller in the MVC sense and is not the same as a viewModel in the WPF world (typically a controller will have references to nodes and controls).

FXML 文件可以有一个关联的控制器,它允许您声明复杂的事件处理程序并设置属性之间的绑定。这是 MVC 意义上的控制器,与 WPF 世界中的 viewModel 不同(通常控制器将引用节点和控件)。

One difference to WPF is that it appears that the FXML is notcompiled into an intermediate binary representation like BAML. I haven't noticed any performance issues yet but have not used the system extensively. I have noticed though, that FXML usually tends to be shorter than any XAML as the platform still encourages you to write code and styles are declared separately.

与 WPF 的一个不同之处在于,FXML 似乎没有被编译成像 BAML 这样的中间二进制表示。我还没有注意到任何性能问题,但还没有广泛使用该系统。但我注意到,FXML 通常比任何 XAML 都短,因为该平台仍然鼓励您编写代码并且样式是单独声明的。

An introduction to FXML can be found here.

可以在此处找到 FXML 的介绍。

A scene builder is provided free (as in beer), so if you don't like hand coding the UI you can drag and drop elements, set properties and bind to code in your controller and the FXML will be generated automatically. Obviously the scene builder is nowhere near as powerful as Expression Blend but it is still better than the 'designer' provided by Visual Studio.

免费提供场景构建器(如啤酒),因此如果您不喜欢手动编码 UI,您可以拖放元素、设置属性并绑定到控制器中的代码,FXML 将自动生成。显然,场景构建器远不如 Expression Blend 强大,但它仍然比 Visual Studio 提供的“设计器”更好。

Binding

捆绑

JavaFX has a very powerful property and binding system. The Java Bean pattern has been extended to include classes that encapsulate a property (similar to the way WPF dependency properties represent properties). These classes implement interfaces that provide invalidation and change notification.

JavaFX 具有非常强大的属性和绑定系统。Java Bean 模式已扩展为包括封装属性的类(类似于 WPF 依赖属性表示属性的方式)。这些类实现提供失效和更改通知的接口。

There is a distinction between invalidation notifications and change notifications. Invalidations just tell you that the binding expression is now invalid and needs to be recalculated; the recalculation does not actually occur until you request the property value via its get()or getValue()methods. If you have registered a change listener, however, then the expression will be re-evaluated immediately and anything that is bound to that property will reflect the changes.

失效通知和更改通知之间存在区别。Invalidations 只是告诉你绑定表达式现在无效,需要重新计算;在您通过其get()getValue()方法请求属性值之前,重新计算实际上不会发生。但是,如果您注册了更改侦听器,则表达式将立即重新评估,并且绑定到该属性的任何内容都将反映更改。

JavaFX exposes these properties in a similar way to WPF with a get and set property and a method that returns an instance of the property wrapper (which are not static like WPF properties).

JavaFX 以类似于 WPF 的方式公开这些属性,具有 get 和 set 属性以及返回属性包装器实例的方法(它们不像 WPF 属性那样是静态的)。

Complex bindings can be created between multipleproperties. Want an integer property to be the sum of two others (a = b + c)? No problem, JavaFX provides a Fluent API to express these kind of relationships E.G.

可以在多个属性之间创建复杂的绑定。想要一个整数属性是另外两个的总和 (a = b + c)?没问题,JavaFX 提供了一个 Fluent API 来表达这些关系 EG

A.Add(B, C);

A.Add(B, C);

If the value of either B or C changes then the appropriate notifications will be raised so that the system knows that A needs to be re-evaluated. Note that in this case, an exception will be thrown if you try and set the value of A as it is bound to the other properties so it does not make sense in this context.

如果 B 或 C 的值发生变化,则会发出适当的通知,以便系统知道需要重新评估 A。请注意,在这种情况下,如果您尝试设置 A 的值,因为它绑定到其他属性,因此在这种情况下没有意义,则会引发异常。

These expressions can be fairly complex E.G. a = (b + c) * (d - e)and can include any number of properties. The fluent API is fairly easy to read and use but is not as nice as some of the Fluent API's provided by some of the Microsoft libraries but this is more down to the Java language limitations rather than JavaFX itself.

这些表达式可以是相当复杂的 EG,a = (b + c) * (d - e)并且可以包含任意数量的属性。fluent API 相当容易阅读和使用,但不如某些 Microsoft 库提供的一些 Fluent API 好,但这更多是由于 Java 语言限制而不是 JavaFX 本身。

Simple bi-directional bindings can be created between properties of the same type so that if one is updated the other automatically reflects the change.

可以在相同类型的属性之间创建简单的双向绑定,以便在更新一个属性时,另一个自动反映更改。

JavaFX also provides a low level API to customise bindings yourself if you want to create a custom binding expression that is not provided by the API or if you are concerned about performance.

如果您想创建 API 未提供的自定义绑定表达式,或者您担心性能,JavaFX 还提供了一个低级 API 来自己自定义绑定。

One of the biggest differences between JavaFX and WPF is that bindings are primarily carried out in code in JavaFX vs. the WPF way of establishing bindings in mark-up.

JavaFX 和 WPF 之间最大的区别之一是绑定主要在 JavaFX 中的代码中执行,而 WPF 在标记中建立绑定的方式。

An introduction to properties and bindings can be found here.

可以在此处找到对属性和绑定的介绍。

Styles

样式

JavaFX uses CSS to change the looks of the nodes contained in the scene graph. There is a full specification available which explains the types and the properties that can be set on each node type.

JavaFX 使用 CSS 来更改场景图中包含的节点的外观。有一个完整的规范可用,它解释了可以在每个节点类型上设置的类型和属性。

JavaFX also provides some additions that help to improve CSS such as variables that can be defined and used elsewhere E.G.

JavaFX 还提供了一些有助于改进 CSS 的附加功能,例如可以在其他地方定义和使用的变量 EG

.button {
    my-custom-color: RGB(234, 44, 78);
}

.my-control {
    -fx-background-color: my-custom-color
}

It also provides a couple of functions that allow you to derive colours from other previously defined colours which is useful for creating things like gradients. This means a base palette of colours can be defined and the rest can be generated from these values (this is what the default JavaFX CSS file does).

它还提供了一些函数,允许您从其他先前定义的颜色中派生颜色,这对于创建诸如渐变之类的东西非常有用。这意味着可以定义一个基本的颜色调色板,其余的可以从这些值生成(这是默认的 JavaFX CSS 文件所做的)。

JavaFX CSS does not allow you to define the typeof layout used by a node (as of writing this all layout needs to be performed in code). This works really well for me as this was the one aspect of CSS that really caused me pain when using it with HTML.

JavaFX CSS 不允许您定义节点使用的布局类型(在撰写本文时,所有布局都需要在代码中执行)。这对我来说非常有效,因为这是 CSS 的一个方面,在将它与 HTML 一起使用时确实让我感到痛苦。

Personally I prefer CSS to XAML styles which tend to be too verbose for my liking.

就我个人而言,我更喜欢 CSS 而非 XAML 样式,因为 XAML 样式对于我的喜好来说过于冗长。

A guide to JavaFX CSS can be found here.

可以在此处找到 JavaFX CSS 指南。

Layout

布局

JavaFX provides a number of layout panes that are similar to those provided by WPF. One difference I have noticed is that the measure and layout contract is defined further up the inheritance chain in the Regionclass.

JavaFX 提供了许多与 WPF 提供的布局窗格类似的布局窗格。我注意到的一个区别是度量和布局契约是在Region类中的继承链上进一步定义的。

As previously mentioned, Layout cannot be carried out using CSS but can be expressed using the code, FXML or created using the scene builder (which is ultimately converted to FXML).

如前所述,布局不能使用 CSS 执行,但可以使用代码、FXML 表示或使用场景构建器(最终转换为 FXML)创建。

Controls

控件

JavaFX provides an ever growing library of controls that we have come to expect. One major difference between JavaFX and WPF is that the controls are essentially black boxes and cannot be re-templated in the way that WPF controls can. They also seem to expose far less properties than the WPF controls.

JavaFX 提供了我们所期望的不断增长的控件库。JavaFX 和 WPF 之间的一个主要区别是控件本质上是黑匣子,不能像 WPF 控件那样重新模板化。它们似乎也比 WPF 控件公开的属性少得多。

The controls do expose some of the implementation specific regions to CSS allowing specific areas of a control to be targetted by your styles. This is known as the substructureof the control. E.G. a CheckBoxexposes two substructures; the box and the check mark allowing each part of the control to be styled independently. Note that as described earlier only the lookof a control can be altered using CSS but the feelcannot. E.G. you cannot dramatically alter the way a TabPanelays out its content by altering its internal layout panel in the way you can with the WPF TabControl.

这些控件确实向 CSS 公开了一些特定于实现的区域,从而允许您的样式针对控件的特定区域。这称为控件的子结构。EG aCheckBox暴露了两个子结构;框和复选标记允许控件的每个部分单独设置样式。请注意,如前所述,只能使用 CSS 更改控件的外观,但不能更改其感觉。例如,您无法像TabPane使用 WPF 那样通过改变其内部布局面板来显着改变布局其内容的方式TabControl

Whilst this sounds fairly limiting, the preferred way of creating custom controls in JavaFX seems to be using composition along the lines of deriving from a layout panel to position standard controls and re-styling them using CSS.

虽然这听起来相当有限,但在 JavaFX 中创建自定义控件的首选方法似乎是使用组合,沿着从布局面板派生的路线来定位标准控件并使用 CSS 重新设置它们的样式。

Conclusion

结论

Overall I am very impressed with what JavaFX has to offer at the moment. Whilst it is no where near as mature as WPF it is being actively developed and Oracle certainly seem to be backing this. Time will tell if it's successful or not.

总的来说,JavaFX 目前提供的功能给我留下了深刻的印象。虽然它不像 WPF 那样成熟,但它正在积极开发中,甲骨文似乎肯定支持这一点。时间会证明它是否成功。

I would recommend giving JavaFX a try. Read the documentationand try putting together a small application and see what you think.

我建议尝试一下 JavaFX。阅读文档并尝试组合一个小应用程序,看看您的想法。

You should also check out FXExperience.comwhich is updated regularly with information from the development team.

您还应该查看FXExperience.com,该网站会根据开发团队的信息定期更新。

回答by Jay Askren

I think the best way to to get a feel for JavaFX is to just try it out. There are some good tutorials on the JavaFX website. Here's a couple:

我认为体验 JavaFX 的最佳方式就是尝试一下。JavaFX 网站上有一些很好的教程。这是一对夫妇:

They are pretty quick and give you a good feel for the language. There are many others on the JavaFXsite if you are interested in more tutorials and articles.

他们很快,让你对语言有很好的感觉。如果您对更多教程和文章感兴趣,JavaFX站点上还有许多其他站点。

For specific answers to your questions:

对于您的问题的具体答案:

  1. JavaFX has it's own declarative language for creating the "visual tree" which is not an xml derivative. The UI is based on a scene graph so you can apply various effects and animation to any node in the graph. See the tutorials for more information. There is also a designer tool for JavaFX (which I have not yet tried).
  2. JavaFX has binding built into the language.
  3. JavaFX on the desktop uses Java AWT/Swing which does use GPU rendering. Every version of Java seems to offload more of its graphics to the GPU. Chris Campbell from Sun has blogged some about GPU acceleration. I'm not sure if the mobile version of JavaFX has GPU acceleration. I found that earlier versions of JavaFX weren't performant enough for what I needed, but I know the latest version does have significant performance improvements over previous versions and they are still working on making it faster.
  4. JavaFx uses Java Applets to run in the browser. As of Java 6 update 10, the Java applet framework has been reworked and though it is not as seamless as Adobe flash, it is much improved. I'm not sure how it compares to Silverlight other than I've had trouble getting Silverlight to work on Linux, but did get JavaFX working on Linux.
  1. JavaFX 有自己的声明性语言来创建“可视化树”,这不是 xml 派生的。UI 基于场景图,因此您可以将各种效果和动画应用于图中的任何节点。有关更多信息,请参阅教程。还有一个用于 JavaFX 的设计器工具(我还没有尝试过)。
  2. JavaFX 具有内置于语言中的绑定
  3. 桌面上的 JavaFX 使用 Java AWT/Swing,它使用 GPU 渲染。每个版本的 Java 似乎都将更多的图形卸载到 GPU。Sun 的 Chris Campbell 发表了一些关于GPU 加速的博客。我不确定 JavaFX 的移动版本是否具有 GPU 加速。我发现早期版本的 JavaFX 的性能不足以满足我的需要,但我知道最新版本确实比以前的版本有显着的性能改进,他们仍在努力使其更快。
  4. JavaFx 使用 Java Applets 在浏览器中运行。从 Java 6 更新 10 开始,Java 小程序框架已经过重新设计,虽然它不像 Adob​​e flash 那样无缝,但已经有了很大改进。除了让 Silverlight 在 Linux 上工作时遇到问题之外,我不确定它与 Silverlight 相比如何,但确实让 JavaFX 在 Linux 上工作。

Here's another related question.

这是另一个相关问题