我是VB程序员,我认为这是我的问题源于此处。通常我可以弄明白,但是嘿。
我有一个视图模型,想要访问我的统一容器 - 我使用Prism和silverlight。所以我试试:
dim container as IUnityContainer
Private Sub New(container as IUnityContainer)
Me.Container = container
end sub
然而,当我尝试从我的视图中实例化我的视图模型时,我得到的错误是没有为New子例程提供所有参数。那么如何才能访问容器?我确信它只是我作为一个dingbat,但C#示例都将它放在与该类同名的子中......
解!
视图模型需要由已经使用依赖注入的东西初始化。由于我使用统一意味着我们需要获得正在使用的容器的实例。由于引导程序沿着线路的某些点触发了所有内容,因此需要跟踪它。
在我的情况下,我使用了视图第一种方法,所以(我讨厌这样做,但目前还不确定更好的方法)我通过传递给视图的容器解析viewmodel ...听起来很混乱但是这是视图的整个代码隐藏:
Imports Microsoft.Practices.Unity
Partial Public Class CustomerList
Inherits UserControl
' This grabs the container when the class is created
Pblic Sub New(container As IUnityContainer)
InitializeComponent()
'Important -> This resolves the class and adds it to the container
Me.DataContext = container.Resolve(Of CustomerListViewModel)()
End Sub
End Class
现在,当我跳转到我的viewmodel时,我可以访问正确的容器!
如果您需要澄清,请告诉我