问题描述
推理:
大家好。我正在构建一个进化模拟器作为个人项目。我在文本字段上设置了一些参数,例如模拟速度和“有机体”的数量。这些将被应用程序的多个组件访问。因为我还想对一些参数使用验证,所以我设置了一个这样的 ViewModel:
class ParametersModel: ViewModel() {
// These properties would likely be DoubleProperty,but for simplicity lets pretend they are strings
val simulationSpeed = bind { SimpleStringProperty() }
val organismsGenerated = bind { SimpleStringProperty() }
}
... 然后对文本字段执行验证测试:
val model = ParametersModel()
textfield(model.simulationSpeed).required()
这没问题,但问题在于我将模型属性定义为对空 SimpleDoubleProperty
的绑定,这是多余的,因为我从不提交此模型(程序应始终读取键入时更改)。同时,我不能简单地定义模型属性:
class ParametersModel: ViewModel() {
val simulationSpeed = SimpleStringProperty()
val organismsGenerated = SimpleStringProperty()
}
因为我随后收到有关验证的错误:
The addValidator extension can only be used on inputs that are already bound bidirectionally to a property in a Viewmodel. Use validator.addValidator() instead or make the property's bean field point to a ViewModel.
我可以采取的另一个选择是创建一个名为 GlobalProperties 之类的类,这将保留我的属性和 ValidationContext。然后我可以使用 validationContext.addValidator
添加验证器并传递文本字段。但在这一点上,我觉得我只是在编写一个等效的 ViewModel。
问题:
ViewModel 是保持由文本字段设置的“全局”访问参数的正确方法吗?如果是这样,有没有办法不必将模型属性设置为空属性的绑定,因为我不需要提交任何内容?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)