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

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

问题描述

我正在尝试在 Silverlight 4 中编写可测试的 ViewModel.我目前正在使用 MVVM light.

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.

但是,我的 VM 像这样绑定到我的视图:

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 将视图模型解析为视图,就像上面的 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 -->

您可以让您的模拟视图模型定位器返回安全且易于阅读的数据供 Blend 使用,并且在运行时您将使用您的真实服务.

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天全站免登陆