2

我参考http://kodein.org/Kodein-DI/?6.1/android#view-model-factory使用了kodein viewmodel,出现了错误。

零件代号

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))
}
4

1 回答 1

2

很简单,只需要直接将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) }
    }
} 
于 2019-06-22T17:40:58.083 回答