如何推迟类的解决

如何推迟类的解决

本文介绍了如何推迟类的解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:

我有这样的应用程序:

我使用Ninject作为我的DI容器,我与WPF

1,有 ClassB的被注入 ClassA的它被喷成 1stViewModel 被注入 1stView

1-There is ClassB which is injected into ClassA Which is Injected into 1stViewModel which is injected into 1stView

2 - 同样有 ClassR 被注入 ClassQ 至极注入 2ndViewModel 被注入 2ndView

2- Again There is ClassR which is injected into ClassQ Wich is Injected into 2ndViewModel which is injected into 2ndView

3,而出现这种情况的第3和第4个窗口

3-And this happens for the 3rd and 4th window

问题:

1 - 我能做些什么来解决各阶级在需要的时候(当用户点击第一个按钮或2日)不在我的作文的根?

1- What can I do to Resolve the Classes when they are needed (when user clicks on 1st Button or 2nd) not in my composition root?

2 - 如果我不注入我的的ViewModels 进入我的意见,我希望可以对新实例我的视图模型里面我看来,这显然需要一个参数,即 IClassA 为第1和 IClassQ 进行第2次例如。我该怎么办呢?

2- If I don't Inject my ViewModels into my Views and I want to make new instance of my ViewModel inside my View, obviously it needs a parameter which is IClassA for the 1st and IClassQ for the 2nd for example. How can I do it?

推荐答案

使用ninject.extensions.factory与您可以定义或给定的接口定义 ToFactory 绑定在构造函数;你可以注入一个延迟< T&GT。更多详情请看这里 https://github.com/ninject/ninject扩展的维基。 extensions.factory /维基

Use ninject.extensions.factory with that you can define ToFactory bindings for a given interface you define or you can inject a Lazy<T> in your constructor. More details see the wiki of the extension here https://github.com/ninject/ninject.extensions.factory/wiki

这篇关于如何推迟类的解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:13