将EasyBind与NumberProperty的子类一起使用

问题描述 投票:0回答:1

介绍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());

doubleValue()部分让我有些不舒服。每当我combine某个NumberProperty的子类时,EasyBind都会传递Number而不是DoubleInteger,...

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

java javafx-8 easybind
1个回答
0
投票

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

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

您可以考虑将heightProperty实现为ObjectProperty<Double>,这将使您省略对doubleValue()的调用;但这可能会使应用程序的其他部分难以编写代码(特别是如果您对高度有其他绑定的话)。我不确定我是否会认为需要致电doubleValue()问题。

© www.soinside.com 2019 - 2024. All rights reserved.