当我使用ViewModel.Factory时,出现“ Kodein未找到bind <kodein>()的绑定”错误

时间:2019-03-26 02:14:09

标签: android kodein

我使用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))
}

1 个答案:

答案 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) }
    }
}