Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 双重绑定没有效果_Kotlin_Tornadofx - Fatal编程技术网

Kotlin 双重绑定没有效果

Kotlin 双重绑定没有效果,kotlin,tornadofx,Kotlin,Tornadofx,我第一次尝试TornadoFX时遇到了一个我不明白的问题 我有一个物体风: enum class Direction(val displayName: String, val abbrevation: String, val deltaX: Int, val deltaY: Int) { NORTH("Észak", "É", 0, -1), NORTH_EAST("Északkelet", "ÉK", 1, -1), EAST("Kelet", "K", 1, 0),

我第一次尝试TornadoFX时遇到了一个我不明白的问题

我有一个物体

enum class Direction(val displayName: String, val abbrevation: String, val deltaX: Int, val deltaY: Int) {

    NORTH("Észak", "É", 0, -1),
    NORTH_EAST("Északkelet", "ÉK", 1, -1),
    EAST("Kelet", "K", 1, 0),
    SOUTH_EAST("Délkelet", "DK", 1, 1),
    SOUTH("Dél", "D", 0, 1),
    SOUTH_WEST("Délnyugat", "DNy", -1, 1),
    WEST("Nyugat", "Ny", -1, 0),
    NORTH_WEST("Északnyugat", "ÉNy", -1, -1);

    val diagonal: Boolean = deltaX != 0 && deltaY != 0
    val degree: Double = ordinal * 45.0

    fun turnClockwise(eighth: Int = 1) = values()[(ordinal + eighth) umod 8]
    fun turnCounterClockwise(eighth: Int = 1) = values()[(ordinal - eighth) umod 8]
    fun turn(eighth: Int = 1) = if (eighth < 0) turnCounterClockwise(eighth.absoluteValue) else turnClockwise(eighth)

    infix operator fun plus(eighth: Int) = turn(eighth)
    infix operator fun minus(eighth: Int) = turn(-eighth)

    infix operator fun minus(other: Direction) = (ordinal - other.ordinal) umod 8
}

object Wind {

    val directionProperty = SimpleObjectProperty<Direction>(Direction.NORTH)
    var direction: Direction
        get() = directionProperty.value
        set(value) {
            println("SET WIND: $value")
            directionProperty.value = value
        }
}
当我尝试使用更优雅的Kotlin风格版本时,它不会绑定:

rot.angleProperty().doubleBinding(rot.angleProperty() ) {
    println("Direction: ${Wind.direction}")
    Wind.directionProperty.value.degree * 45
}
我错过了什么吗?

函数
doubleBinding()
创建了一个
绑定
,但它不会将它绑定到任何东西

事实上,我们有两种方法来创建此绑定:

  • doubleBinding(someProperty){…}
    。对属性(this)进行操作,并希望您返回一个Double。它不可为空

  • someProperty.doubleBinding(){…}
    将该值作为参数接收,并希望您返回一个Double。该参数可为null,因此需要对此进行说明

  • 这就给您留下了两个选择:

    rot.angleProperty().bind(doubleBinding(Wind.directionProperty) {
        value.degree * 45
    })
    

    你选择哪一种主要取决于口味,尽管在某些情况下,其中一种会比另一种更自然

    rot.angleProperty().bind(doubleBinding(Wind.directionProperty) {
        value.degree * 45
    })
    
    rot.angleProperty().bind(Wind.directionProperty.doubleBinding {
        it?.degree ?: 0.0 * 45 
    })