IoC 和设计时间
我有一个 WPF 应用程序,我用它来学习 MVVM 和 IoC。
问题是视图之一使用的模型期望从 IoC 容器的构造函数中提取其依赖项之一。
在 Visual Studio 设计器中处理此视图时,它无法显示设计,因为模型中引发了异常。
有办法解决这个问题吗? 我是否将依赖项拉到了代码中的错误位置,或者是否有一种方法可以传递构造的依赖项(也许通过构造函数注入)。
目前,IoC 容器是在 App.xaml.cs 中的代码中设置的。
IoC 容器是您自己的,摘自 MSDN 上的这篇文章 - http ://msdn.microsoft.com/en-us/magazine/cc337885.aspx
I have a WPF application which I am using to learn MVVM and IoC.
The problem is that the Model used by one of the Views expects to pull one of its dependancies in the constructor from an IoC container.
When working on this View in the Visual Studio designer it cannot show the design because an exception is being raised in the model.
Is there a way around this?
Am I pulling my dependancies in the wrong place in code or is there a way I can pass in constructed dependancies, perhaps through Constructor injection.
At present the IoC container is setup in code in App.xaml.cs.
The IoC container is a roll-your-own taken from this article on MSDN - http://msdn.microsoft.com/en-us/magazine/cc337885.aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近面临类似的问题。我通过以下检查解决了这个问题:
I was facing a similar problem recently. I worked around it with the following check:
我发现问题是由于在 Xaml 中将 ViewModel 声明为视图的数据上下文引起的,通过将其移动到视图构造函数,Xaml 在设计器中正确呈现。
I found the problem was caused by declaring the ViewModel as the View's datacontext within the Xaml, by moving this to the View constructor the Xaml renders properly in the designer.