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

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

问题描述

将我所有的 ViewModel 存储在 SimpleIoc 中是否正确?例如,我有三个页面 MainPage、Photos、Directories(因此三个 ViewModels -> MainVM、PhotosVM、DirectoriesVM).我应该将每个页面中的 DataContext 设置为 ViewModelLocator 中的 View Model Property 还是嵌套 ViewModel 作为 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 中的 ViewModels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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