.net - Ninject InSingletonScope() View 模型在绑定(bind)时未分配给 View

标签 .net mvvm inversion-of-control ninject

我假设在 NinjectModule 中我绑定(bind)

Bind<SplashViewModel>().ToSelf().InSingletonScope();

SplashViewModel 的一个实例有点像 Ninject 内核中的缓存,每次我调用 kernel.Get<SplashViewModel>我将得到返回给我的 View 模型的完全相同的实例。这就是所发生的事情,事实如此。

在我的NinjectUIModule我打电话:

Bind<SplashViewModel>().ToSelf().InSingletonScope();
Bind<SplashWindow>().ToMethod(context => new SplashWindow()
                                                       {
                                                           DataContext = new SplashViewModel()
                                                       });

问题是 SplashWindow获取 SplashViewModel 的不同新实例,而不是缓存在 Ninject 内核中并返回给所有其他检索方的实例。

如何将 View 模型绑定(bind)到 Ninject 模块中 View 的 DataContext,并允许稍后通过 kernel.Get 返回到应用程序中的其他代码。

最佳答案

如果您的 SplashWindow 将 SplashViewModel 作为 ctor arg,那么 Ninject 会为您处理该初始化。您甚至不需要定义 SplashWindow 绑定(bind)。

关于.net - Ninject InSingletonScope() View 模型在绑定(bind)时未分配给 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7961305/

相关文章:

c# - 带有 DI 和 IoC 的工厂方法

c# - 如何在 C# 中连接两个数组?

c# - 如何从负载均衡器后面获取客户端的 IP 地址?

c# - 在不同屏幕上显示 C# WinForm

silverlight - 在文本 block 中突出显示搜索词

c# - ViewModel 与 Model 有不同的字段吗?

c# - SSL/TLS/HTTPS 站点在 C#/.NET WebBrowser 控件中非常慢,但在 Internet Explorer 中正常

c# - 如何向 Josh Smith 的 MVVM msdn 设计添加过滤

c# - 特定类类型的 Autofac 扫描程序集

dependency-injection - 控制反转<依赖注入(inject)