【发布时间】:2011-05-01 07:42:48
【问题描述】:
我正在尝试在 Silverlight 4 中编写可测试的 ViewModel。我目前正在使用 MVVM light。
我正在使用 AutoFac,而 IoCContainer 工作正常。但是要注入绑定到 Views 的 ViewModels 的构造函数中,我有这个构造函数链接:
public UserViewModel() : this(IoCContainer.Resolve<IUserServiceAsync>())
{
}
public UserViewModel(IUserServiceAsync userService)
{
if (this.IsInDesignMode) return;
_userService = userService;
}
感觉不干净,但这是我迄今为止找到的最佳选择。这可行,我的应用程序可以按需要运行,并且可以通过反转控制进行测试。
但是,我的虚拟机像这样绑定到我的视图:
<UserControl.DataContext>
<ViewModel:UserViewModel />
</UserControl.DataContext>
在我的 XAML 页面属性中,VS2010 和 Blend 中的设计模式都不起作用。
是否有更好的方法来实现我在 Silverlight 中尝试的仍然适用于设计模式的内容?失去设计模式不会破坏交易,但在学习 XAML 时会很方便。不过,更清洁的无连锁方式会很好!
我认为也许可以使用 AutoFac / IoC 将视图模型解析为视图,与上面的 XAML 标记方法相反,然后沿着这条路线走下去?
谢谢。
【问题讨论】:
标签: c# silverlight silverlight-4.0 ioc-container mvvm-light