function - 在 scala 中将函数与条件逻辑绑定(bind)

标签 function scala binding scalafx

我是来自 JavaFX 1.3 的 scala 新手,这是我在 stackoverflow 上的第一篇文章

在 JavaFX 1.3 中我可以做这样的事情

property : bind if (condition) value1 else value2

在 Scala 中,我尝试做这样的事情:

property <== function1

def function1() = {
  if (condition)
    value1
  else
    value2
}

但是,它似乎并没有动态工作。当阶段出现时,函数条件中的表达式仅计算一次。我有点期待该表达式中的值能够实时计算。

具体来说,我想将某些东西的大小调整到一定的限制,并且我正在使用绑定(bind)来实现它。因此,我希望绑定(bind)函数继续评估表达式,并在调整其他内容的大小时为我提供适当的宽度。

无论如何,我将粘贴下面的实际代码:

var stageWidth = DoubleProperty(0)
var stageHeight = DoubleProperty(0)

stageWidth <== stage.scene.width
stageHeight <== stage.scene.height

var origStageWidth = DoubleProperty(stage.scene.width.toDouble)
val origStageHeight = DoubleProperty(stage.scene.height.toDouble)

val origTextClipperWidth = DoubleProperty(textClipper.width.toDouble)
val origTextClipperHeight = DoubleProperty(textClipper.height.toDouble)
val minWidth = DoubleProperty(100)

val origButtonWidth = DoubleProperty(button.prefWidth.toDouble)

textClipper.width <== resize

def resize() ={
    var boolResult = (stageWidth - origStageWidth) + origTextClipperWidth > minWidth
    if (boolResult.value) {
        (stageWidth - origStageWidth) + origTextClipperWidth
    } else {
        minWidth
    }
}

textClipper.height <== (stageHeight - origStageHeight) + origTextClipperHeight

预先感谢您的帮助。

最佳答案

标准函数/方法不是 scalafx.beans.Observable,因此它没有所需的“钩子(Hook)”来使其绑定(bind)无效。

不久前,我只是为了这个目的,做了一些简化绑定(bind)创建的方法。

以下代码用于将函数绑定(bind)到字符串值

import scalafx.Includes._
import scalafx.beans.binding.StringBinding
import scalafx.beans.Observable
import scalafx.collections._
import javafx.collections.ObservableList
import javafx.beans.{ binding => jfxbb }
import jfxbb.ListBinding

def createStringBinding(dependency: Observable*)(computeFunction: => String): StringBinding = 
  new jfxbb.StringBinding {
  //invalidated when the passed dependency becomes invalid
  dependency.foreach(this.bind(_))
  //use the function to compute the new value
  override def computeValue: String = computeFunction
}

根据您的情况,您应该进行双重绑定(bind)

//THIS CODE IS NOT TESTED, MAYBE IT NEEDS A LITTLE TWEAKING

def createDoubleBinding(dependency: Observable*)(computeFunction: => Double): DoubleBinding = 
  new jfxbb.DoubleBinding {
  //invalidated when the passed dependency becomes invalid
  dependency.foreach(this.bind(_))
  //use the function to compute the new value
  override def computeValue: Double = computeFunction
}

//and use it like
val resize = createDoubleBinding(
  stageWidth,
  stageHeight,
  origStageWidth,
  origStageHeight,
  minWidth,
  origButtonWidth) {
    var boolResult = (stageWidth - origStageWidth) + origTextClipperWidth > minWidth
    if (boolResult.value) {
      (stageWidth - origStageWidth) + origTextClipperWidth
    } else {
      minWidth
    }
}

textClipper.width <== resize

我认为可以使用适合 javafx.beans.binding 包的可用类的类型参数来概括 createXXXBinding,但我不确定这是否是一项简单的任务,因为该类层次结构没有帮助...

关于function - 在 scala 中将函数与条件逻辑绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17440878/

相关文章:

function - 在 Octave 中声明函数时未定义参数

scala - 如何正确处理 Future.sequence 中的个别异常?

可选泛型类型的 Swift 扩展

function - p5.j​​s 如何使函数 setup() 像这样工作?

function - 将谷歌表中的日期区域设置从公历转换为 Jalali 日历

scala - scala中的无符号整数

scala - 为什么函数不是尾递归的?

c - 对于 UDP 套接字,IP 地址是否在 close() 返回时不受限制?

C# DataGrid 填充了多层列表

python - 为什么 list/dict 属性在装饰器之外保留值,而整数属性却没有?