如何在 Kotlin/TornadoFX 中关闭视图后执行清理操作?
How can I perform clean up actions upon closing a view in Kotlin/TornadoFX?
我一直在搜索文档和 IDE 自动完成建议,但无法解决这个问题。我发现最接近的是 onDelete(),但它并没有像我想象的那样工作。
我只需要一种在视图关闭时运行清理代码的方法。
这是使用文档中的简单示例的失败尝试。
import tornadofx.*
class MyApp: App(MyView::class)
class MyView: View() {
// this does not print when the window is closed
override fun onDelete() {
super.onDelete()
println("Print on close!")
}
override val root = vbox {
button("Press me")
label("Waiting")
}
}
fun main(args: Array<String>) {
launch<MyApp>(args)
}
根据以下建议再次尝试失败:
import tornadofx.*
class MyApp: App(MyView::class)
class MyView: View() {
// "Closing" is never printed when closing this view"
override fun onDock() {
currentWindow?.onHidingProperty()?.onChangeOnce {
println("Closing")
}
}
override val root = vbox {
button("Press me")
label("Waiting")
}
}
fun main(args: Array<String>) {
launch<MyApp>(args)
}
onDelete
是 TornadoFX 中 Workspace
的回调,如果您在该视图处于活动状态时单击工作区中的 Delete
按钮,将被调用。您可以做的是覆盖 onDock
并向隐藏 属性:
添加一次更改侦听器
override fun onDock() {
currentWindow?.onHidingProperty()?.onChangeOnce {
println("Closing")
}
}
我现在正在我的项目中使用它。 setOnCloseRequest
我要去!
override fun onDock() {
currentWindow?.setOnCloseRequest {
println("Closing")
}
}
如果您在关闭应用程序时寻找清理代码,另一个适用于子 views/fragments/controllers 的选项是在关闭时触发的 FXEvent。事件定义如下:
object ApplicationClosingEvent : FXEvent()
在主要 App
中,我覆盖 stop()
以便触发事件:
override fun stop() {
fire(ApplicationClosingEvent)
}
由于 stop()
在应用程序线程上是 运行,事件被同步触发和处理。最后,在每个 view/fragment/controller 中,您可以像这样订阅:
subscribe<ApplicationClosingEvent> {
// Clean-up code goes here
}
使用事件而不是 window 回调的主要好处是您可以有许多不同的视图订阅它,而不是单个 window 回调。
我一直在搜索文档和 IDE 自动完成建议,但无法解决这个问题。我发现最接近的是 onDelete(),但它并没有像我想象的那样工作。
我只需要一种在视图关闭时运行清理代码的方法。
这是使用文档中的简单示例的失败尝试。
import tornadofx.*
class MyApp: App(MyView::class)
class MyView: View() {
// this does not print when the window is closed
override fun onDelete() {
super.onDelete()
println("Print on close!")
}
override val root = vbox {
button("Press me")
label("Waiting")
}
}
fun main(args: Array<String>) {
launch<MyApp>(args)
}
根据以下建议再次尝试失败:
import tornadofx.*
class MyApp: App(MyView::class)
class MyView: View() {
// "Closing" is never printed when closing this view"
override fun onDock() {
currentWindow?.onHidingProperty()?.onChangeOnce {
println("Closing")
}
}
override val root = vbox {
button("Press me")
label("Waiting")
}
}
fun main(args: Array<String>) {
launch<MyApp>(args)
}
onDelete
是 TornadoFX 中 Workspace
的回调,如果您在该视图处于活动状态时单击工作区中的 Delete
按钮,将被调用。您可以做的是覆盖 onDock
并向隐藏 属性:
override fun onDock() {
currentWindow?.onHidingProperty()?.onChangeOnce {
println("Closing")
}
}
我现在正在我的项目中使用它。 setOnCloseRequest
我要去!
override fun onDock() {
currentWindow?.setOnCloseRequest {
println("Closing")
}
}
如果您在关闭应用程序时寻找清理代码,另一个适用于子 views/fragments/controllers 的选项是在关闭时触发的 FXEvent。事件定义如下:
object ApplicationClosingEvent : FXEvent()
在主要 App
中,我覆盖 stop()
以便触发事件:
override fun stop() {
fire(ApplicationClosingEvent)
}
由于 stop()
在应用程序线程上是 运行,事件被同步触发和处理。最后,在每个 view/fragment/controller 中,您可以像这样订阅:
subscribe<ApplicationClosingEvent> {
// Clean-up code goes here
}
使用事件而不是 window 回调的主要好处是您可以有许多不同的视图订阅它,而不是单个 window 回调。