1

I've used MVVM before, but MVVM-Light confuses the hell out of me, so I'm sorry if this is dumb (it's just that there's barely any documentation).

Anyways, I've learnt that you use the ViewModelLocator only for "singleton" views from Laurent's answer here.

So how do I handle view-models that aren't in the locator? because it seems like I'm losing the benefits MVVM-Light has there.

What I do now is go to the view's code-behind and create a dependency property for my view-model and set my data-context to it.

Is there a better way? because I get no blendability whatsoever (Visual Studio and ReSharper doesn't even recognize the data context in the XAML editor). Not to mention I have no dummy data to design against.

In other words:
From what I've seen the blendability comes from a dependency injection found the locator, so what do you do when you're not using the locator?

4

1 回答 1

1

将我的评论转换为答案

AFAIK 您可以通过在从 ServiceLocator 解析 VM 时传递唯一密钥来获取新的 VM 实例。您不必为了单例而不得不使用 VMLocator。

您可以从Here获取此过程的示例。在MainWindow.xaml.cs请求新的非模态窗口时,视图的每个实例都与相应 VM 的新实例相结合,可以在代码隐藏中找到。

我如何将视图模型实例绑定到视图(不在定位器中)

^^ 不确定这是否是您所追求的,但是使用 MVVM Light(用于设计时间 VM),您可以DataContext在检查是否处于设计模式后在其构造函数中设置视图

就像是:

using GalaSoft.MvvmLight;
...

public MainWindow() {
  InitializeComponent();
  if (ViewModelBase.IsInDesignModeStatic)
    DataContext = new MainViewModel();
}
于 2013-08-06T14:13:16.373 回答