Silverlight的构造注入视图模型+设计模式 [英] Silverlight Constructor Injection into View Model + Design Mode

查看:158
本文介绍了Silverlight的构造注入视图模型+设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去认真处理目前使用MVVM光在Silverlight 4,我写可验证的ViewModels。

Im trying to get to grips with writing testable ViewModels in Silverlight 4. Im currently using MVVM light.

使用AutoFac和IoCContainer林正在做的工作的罚款。然而,要注入的ViewModels的构造,这必将意见我有这样的构造链接:

Im using AutoFac and the IoCContainer is doing its job fine. However to inject into the constructor of ViewModels, which are bound to Views I have this constructor chaining:

    public UserViewModel() : this(IoCContainer.Resolve<IUserServiceAsync>())
    {

    }

    public UserViewModel(IUserServiceAsync userService) 
    {
        if (this.IsInDesignMode) return;

        _userService = userService;
    }



这并不感觉干净,但我发现这样最好的选择远。这个工作和我的应用程序可以作为理想的,并且是可测试倒置的控制。

Which doesn't feel clean, but is the best option i have found so far. This works and my app works as desired, and is testable with control inverted.

然而,我的虚拟机绑定到我的看法是这样的:

However, with my VM bound to my view like this:

 <UserControl.DataContext>
            <ViewModel:UserViewModel />
 </UserControl.DataContext>

在我的XAML页面属性,设计模式,在这两个VS2010和Blend不工作。

In my XAML page attributes, design mode in both VS2010 and Blend doesnt work.

时有更好的方式来实现什么即时在Silverlight仍然与设计模式下工作努力?丢失设计模式的心不是一个大忌,但同时学习XAML将是得心应手。一个清洁无链接的方式将是很好的,但!

Is there are nicer way to achieve what im trying in Silverlight that still works with design mode? Losing design mode isnt a deal breaker but will be handy while learning XAML. A cleaner none chained way would be nice though!

林认为它也许可以使用AutoFac / IoC的解决的ViewModels到意见,结合在上面的XAML标记的办法,并沿着这条路走下去?

Im thinking it maybe possible to use AutoFac / IoC to resolve viewmodels to views, as apposed to the XAML mark-up approach above, and go down this route?

感谢。

推荐答案

无需实现第一个构造函数,我建议你实施ViewModelLocator,像这样的:

Instead of implementing the first constructor, I suggest you implement a ViewModelLocator, like this:

public class ViewModelLocator
{

    IoCContainer Container { get; set; }

    public IUserViewModel UserViewModel
    {
        get
        {
            return IoCContainer.Resolve<IUserViewModel>();
        }
    }

}

然后在XAML声明的定位为静态资源:

Then in XAML you declare the locator as a static resource:

<local:ViewModelLocator x:Key="ViewModelLocator"/>



当你初始化应用程序,就必须提供定位与容器的实例:

While you initialize your application, it is necessary to provide the locator with the instance of the container:

var viewModelLocator = Application.Current.Resources["ViewModelLocator"] as ViewModelLocator;
if(viewModelLocator == null) { // throw exception here }
viewModelLocator.Container = IoCContainer;

然后在XAML可以干净地使用资源:

Then in XAML you can use the resource cleanly:

<UserControl
    DataContext="{Binding Path=UserViewModel, Source={StaticResource ViewModelLocator}}"
    />
    <!-- The other user control properties -->

有关设计时间,可以实现MockViewModelLocator:

For design time, you can implement a MockViewModelLocator:

public class MockViewModelLocator
{

    public IUserViewModel UserViewModel
    {
        get
        {
            return new MockUserViewModel();
        }
    }

}



声明它在XAML适当的:

Declare it in XAML appropriately:

<local:MockViewModelLocator x:Key="MockViewModelLocator"/>



终于在用户的控制使用它:

And finally use it in your user control:

<UserControl
    d:DataContext="{Binding Path=UserViewModel, Source={StaticResource MockViewModelLocator}}"
    DataContext="{Binding Path=UserViewModel, Source={StaticResource ViewModelLocator}}"
    />
    <!-- The other user control properties -->

您可以让你的模拟视图模型定位器返回的安全,方便地混合使用,在运行时可读数据你会使用你的实实在在的服务。

You can make your mock view model locator return safe and easily readable data for Blend to use and during runtime you will be using your real service.

这样,您就不会失去设计时间的数据,你不必牺牲依赖注入方法的清洁度在您的视图模型

This way you do not lose design time data and you do not have to sacrifice the cleanliness of the dependency injection methodology in your view models.

我希望这有助于。

这篇关于Silverlight的构造注入视图模型+设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆