![](/img/trans.png)
[英]Binding to a ObservableValue<ObservableList> instead of an ObservableList with EasyBind
[英]Using EasyBind with subclass of NumberProperty
在介紹 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
而不是Double
, Integer
, ...
有什么方法可以避免doubleValue()
嗎?
不是 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 :即它在DoubleProperty
上調用getValue()
,它返回一個Number
,而不是一個Double
。 因此,您的BiFunction
必須是BiFunction<Number, Insets, Double>
(將Number
和Insets
映射到Double
)。
您可以考慮將heightProperty
實現為ObjectProperty<Double>
,這將允許您省略對doubleValue()
的調用; 但它可能會使您的應用程序的其他部分更難編碼(特別是如果您對高度有其他綁定)。 我不確定我是否認為需要調用doubleValue()
是一個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.