ViewModelLocator MVVM Light中的ViewModel [英] ViewModels in ViewModelLocator MVVM Light

查看:266
本文介绍了ViewModelLocator MVVM Light中的ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将所有ViewModel存储在SimpleIoc中是否正确?例如,我有三个页面MainPage,Photos,Directorys(因此有三个ViewModels-> MainVM,PhotosVM,DirectoriesVM).我应该在每个页面中将DataContext设置为ViewModelLocator中的视图模型属性"还是将ViewModels嵌套为MainVM中的属性,并将每个页面DataContext绑定到Main.PhotosVMProperty,Main.DirectoriesVMProperty等?任何人都可以向我解释IoC的想法和目的吗?

Is it correct to store all my ViewModels in SimpleIoc? For instance I am having three pages MainPage, Photos, Directories (therefore three ViewModels -> MainVM, PhotosVM, DirectoriesVM). Should I set DataContext in each page to View Model Property in ViewModelLocator or nest ViewModels as properties in MainVM and bind each page DataContext to Main.PhotosVMProperty, Main.DirectoriesVMProperty and so on? Could anyone explain me idea and purpose of IoC ?

推荐答案

首先,让我们看一下ViewModelLocator的功能以及为什么使用它:

First, lets look at what ViewModelLocator does and why we use it:

ViewModelLocator在我们的App.xaml页面上声明为一个对象,并且是应用程序单例.我们将拥有一个,并且在应用程序运行时只有其中一个可供使用.

ViewModelLocator is declared as an object on our App.xaml page and is an application singleton. We're going to have one, and only one of them available to the application when it runs.

ViewModelLocator是MVVM Light中所有ViewModel的源代码.对于每个ViewModel,我们将在ViewModelLocator上具有一个属性,该属性使我们能够获取视图的ViewModel.这段代码如下:

ViewModelLocator is the source for all our ViewModels in MVVM Light. For each ViewModel we'll have a property on the ViewModelLocator that allows us to get a ViewModel for a View. This code looks like this:

public class ViewModelLocator
{
    public MainPageViewModel MainPage
    {
        get { return new MainPageViewModel(); }
    }
}

这是我的App.xaml的一部分:

This is a piece of my App.xaml:

<Application.Resources>
    <vm:ViewModelLocator
        x:Key="ViewModelLocator" />
</Application.Resources>

这是View.xaml的一部分

This is a piece from View.xaml

DataContext="{Binding MainPage, Source={StaticResource ViewModelLocator}}"

到目前为止,一切都很好.要回答您的第一个问题,您是否必须在MVVM Light中使用Ioc?不需要.您不需要将视图模型提供给由ViewModelLocator完全构建并实例化的视图.

So far so good. To answer your first question, do you have to use Ioc in MVVM Light? No. There's no need as your viewmodel will be given to your view fully built and instantiated by the ViewModelLocator.

现在,您要回答第二个问题:IoC的目的是什么?

Now, onto your second question: What's the purpose of IoC?

IoC旨在允许您执行以下操作:

IoC is designed to allow you to do the following:

使用Mvvm Light,您可以像这样进行上述操作:

With Mvvm Light you do the above like this:

public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        if (ViewModelBase.IsInDesignModeStatic)
        {
            SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
        }
        else
        {
            SimpleIoc.Default.Register<IDataService, DataService>();         
        }

        SimpleIoc.Default.Register<MainViewModel>();
    }

    public MainViewModel Main
    {
        get { return SimpleIoc.Default.GetInstance<MainViewModel>(); }
    }
}

public class MainViewModel
{
    public ObservableCollection<Foo> Foos { get; set; }

    public MainViewModel(IDataService dataService)
    {
        _dataService=dataService;
        Foos=_dataService.GetFoos();
    }
}

调用时解析MainViewModel时

When I resolve my MainViewModel when I call

SimpleIoc.Default.GetInstance<MainViewModel>()

内部发生的事情是SimpleIoc检查MainViewModel是否具有任何依赖项(其构造函数中的参数).然后,它通过查看已向其注册的接口来尝试解析这些参数.它以递归方式执行此操作,因此,如果DataService具有依赖项,则将其实例化,并在实例化时将其传递给DataService构造函数.

what happens internally is that the SimpleIoc checks to see if the MainViewModel has any dependencies (parameters in its constructor). It then tries to resolve these parameters by looking at the interfaces that have been registered with it. It does this recursively, so if DataService had a dependency it would be instantiated and passed to the DataService constructor when it was being instantiated as well.

我为什么要做所有这些工作?

Why would I do all this work?

  1. 使您的班级易于单元测试
  2. 使您的代码由界面驱动.这意味着您引用的是接口而不是具体的类
  3. 使您的代码松散耦合.这意味着有人可以更改接口的实现,而使用该接口的类则不在乎,也不必重新编码.
  4. 以自动方式解决您的类依赖性.
  5. 在MVVM Light中,您将看到它可以告诉它何时以设计模式(ViewModelBase.IsInDesignModeStatic)运行,这意味着您可以创建设计时服务以提供视图模型数据,从而使Visual Studio中的视图包含实际数据.
  1. Make your classes easily unit testable
  2. Make your code interface-driven. This means that you're referencing interfaces rather than concrete classes
  3. Make your code loosely coupled. This means that someone can change the implementation of an interface and classes that consume that interface don't care and don't have to be re-coded.
  4. Resolve your classes dependencies in an automated way.
  5. In MVVM Light, you'll see that it can tell when it's running in design-mode (ViewModelBase.IsInDesignModeStatic), this means that you can create design-time services to provide your viewmodels data so your View in Visual Studio contains actual data.

这篇关于ViewModelLocator MVVM Light中的ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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