【问题标题】:Using EasyBind with subclass of NumberProperty将 EasyBind 与 NumberProperty 的子类一起使用
【发布时间】:2014-11-17 08:07:27
【问题描述】:

在介绍 EasyBind 之前 -

DoubleBinding contentHeight = Bindings.createDoubleBinding(
    () -> getHeight() - getInsets().getTop() - getInsets().getBottom(),
    heightProperty(), insetsProperty());

引入 EasyBind 之后 -

Binding<Double> contentHeight = EasyBind.combine(
    heightProperty(), insetsProperty(),
    (h, i) -> h.doubleValue() - i.getTop() - i.getBottom());

我对@9​​87654323@ 部分感到有些不自在。每次我combine NumberProperty 的某个子类时,EasyBind 都会传递 Number 而不是 DoubleInteger、...

有什么办法可以避免doubleValue()

【问题讨论】:

    标签: java javafx-8 easybind


    【解决方案1】:

    不是 EasyBind 导致您需要调用 doubleValue() - 这是 JavaFX API 的结果。

    EasyBind.combine() 有一个参数列表(ObservableValue&lt;A&gt;, ObservableValue&lt;B&gt;, BiFunction&lt;A,B,R&gt;),并返回一个Binding&lt;R&gt;。对于第一个参数,您将传入 DoubleProperty。问题是DoubleProperty(有点违反直觉)实现了ObservableValue&lt;Number&gt;,而不是ObservableValue&lt;Double&gt;combine 方法在前两个参数上调用 getValue() 的结果上调用 BiFunction:即,它在 DoubleProperty 上调用 getValue(),它返回 Number,而不是 Double。因此,您的 BiFunction 必须是 BiFunction&lt;Number, Insets, Double&gt;(将 NumberInsets 映射到 Double)。

    您可以考虑将您的heightProperty 实现为ObjectProperty&lt;Double&gt;,这样您就可以省略对doubleValue() 的调用;但它可能会使您的应用程序的其他部分更难编码(特别是如果您对高度有其他绑定)。我不确定我是否会考虑拨打doubleValue() 的问题。

    【讨论】:

    • 感谢您的详细解答。我只是觉得打电话给doubleValue() 有点多余,但很好。将heightProperty 实现为ObjectProperty&lt;Double&gt; 是不合适的,因为它来自javafx.scene.layout.Pane
    猜你喜欢
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多