![](/img/trans.png)
[英]I don't understand the argument passing of the lambda expression in the listener
[英]Passing lambda as argument in Kodein
我正在使用Kodein在Android上进行依赖注入(当然是在Kotlin中),但我正在努力解决一个问题:我似乎无法将lambda作为参数传递给工厂。 它正确编译但在运行时失败(我认为Kodein可以防止这种情况)。
在我的Application类中,我执行以下绑定:
class MyApplication : Application(), KodeinAware {
override val kodein by Kodein.lazy {
...
bind<SimpleButtonListener>() with factory { func: () -> Unit -> SimpleButtonListener(func) }
}
}
在我的活动中,我这样调用它:
val onClick = { startActivity(EmailIntent()) }
val clickListener : SimpleButtonListener by with(onClick).instance()
我也尝试过这个失败:
val clickListener : SimpleButtonListener by with({ startActivity(EmailIntent()) }).instance()
但是我跑的时候总是遇到同样的问题:
com.github.salomonbrys.kodein.Kodein $ NotFoundException:找不到bind()的工厂? {? }
... bind()与工厂{Function0 - > SimpleButtonListener}
我对Kotlin还很新,所以我不确定我到底出了什么问题。 在我错过的语言中是否有一个怪癖或成语,或者Kodein在lambda作为参数的限制?
这是Kodein 4中的一个错误,在下一版Kodein(版本5.0)中已得到纠正。
同时,这是修复:
val clickListener : SimpleButtonListener by With(generic(), onClick).instance()
很抱歉给您带来不便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.