[英]Using EasyBind with subclass of NumberProperty
Before introducing EasyBind -在介绍 EasyBind 之前 -
DoubleBinding contentHeight = Bindings.createDoubleBinding(
() -> getHeight() - getInsets().getTop() - getInsets().getBottom(),
heightProperty(), insetsProperty());
After introducing EasyBind -引入 EasyBind 后 -
Binding<Double> contentHeight = EasyBind.combine(
heightProperty(), insetsProperty(),
(h, i) -> h.doubleValue() - i.getTop() - i.getBottom());
I'm somewhat uncomfortable regarding doubleValue()
part.我对doubleValue()
部分有些不舒服。 Every time I combine
some subclass of NumberProperty
, EasyBind passes Number
instead of ie Double
, Integer
, ...每次我combine
NumberProperty
一些子类时,EasyBind 都会传递Number
而不是Double
, Integer
, ...
Is there some way to avoid doubleValue()
?有什么方法可以避免doubleValue()
吗?
It's not EasyBind that's causing the need for you to call doubleValue()
- it's a consequence of the JavaFX API.不是 EasyBind 导致您需要调用doubleValue()
- 这是 JavaFX API 的结果。
EasyBind.combine()
has a parameter list (ObservableValue<A>, ObservableValue<B>, BiFunction<A,B,R>)
, and returns a Binding<R>
. EasyBind.combine()
有一个参数列表(ObservableValue<A>, ObservableValue<B>, BiFunction<A,B,R>)
,并返回一个Binding<R>
。 For the first parameter you're passing in a DoubleProperty
.对于第一个参数,您传入的是DoubleProperty
。 The issue is that DoubleProperty
(somewhat counter-intuitively) implements ObservableValue<Number>
, not ObservableValue<Double>
.问题是DoubleProperty
(有点违反直觉)实现了ObservableValue<Number>
,而不是ObservableValue<Double>
。 The combine
method invokes your BiFunction on the result of calling getValue()
on the first two parameters: ie it calls getValue()
on your DoubleProperty
, which returns a Number
, not a Double
. combine
方法在前两个参数上调用getValue()
的结果调用您的 BiFunction :即它在DoubleProperty
上调用getValue()
,它返回一个Number
,而不是一个Double
。 Thus your BiFunction
has to be a BiFunction<Number, Insets, Double>
(mapping a Number
and Insets
to a Double
).因此,您的BiFunction
必须是BiFunction<Number, Insets, Double>
(将Number
和Insets
映射到Double
)。
You could consider implementing your heightProperty
as a ObjectProperty<Double>
, which would allow you to omit the call to doubleValue()
;您可以考虑将heightProperty
实现为ObjectProperty<Double>
,这将允许您省略对doubleValue()
的调用; but it might make other parts of your application harder to code (specifically if you have other bindings to the height).但它可能会使您的应用程序的其他部分更难编码(特别是如果您对高度有其他绑定)。 I'm not sure I would consider the need to call doubleValue()
a problem.我不确定我是否认为需要调用doubleValue()
是一个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.