我使用Kodein视图模型引用http://kodein.org/Kodein-DI/?6.1/android#view-model-factory,出现了错误。
零件代码
bind<ViewModelProvider.Factory>() with singleton { KodeinViewModelFactory(instance()) }
class KodeinViewModelFactory(private val kodein: Kodein) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T =
kodein.direct.Instance(TT(modelClass))
}
答案 0 :(得分:1)
这很简单,您只需要直接将Kodein转移到工厂,而无需instance()
class KodeinApp : Application(), KodeinAware {
override val kodein by Kodein.lazy {
import(androidXModule(this@KodeinApp))
bind() from singleton { KodeinViewModelFactory(kodein) }
bind() from singleton { MyViewModel(kodein) }
}
}