wpf 从代码访问 MVVM Light ViewModelLocator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33504877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:31:07  来源:igfitidea点击:

Access MVVM Light ViewModelLocator from code

c#.netwpfmvvm-light

提问by MuhKuh

I am trying to implement an application with the MVVM Light Toolkit, but I am somehow stuck with the ViewModelLocator.

我正在尝试使用 MVVM Light Toolkit 实现一个应用程序,但不知何故我被 ViewModelLocator 卡住了。

While it is clear to me how to access it from the views in XAML and code-behind of the views, I have some problems accessing it from other view models.

虽然我很清楚如何从 XAML 中的视图和视图的代码隐藏中访问它,但我在从其他视图模型访问它时遇到了一些问题。

In App.xaml:

在 App.xaml 中:

<Application.Resources>
  <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:MvvmLight1.ViewModel" x:Key="Locator" />
</Application.Resources>

In the View:

在视图中:

DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"

But how to access for example the MainViewModel from some other view model? It seems previous releases of the ViewModelLocator snippets had a static method to the different view models. But those do not exist anymore, so I assume this was not the way to go. But what is? Or am I doing something completely against the pattern if I want to access the locator from a view model?

但是如何从其他视图模型访问例如 MainViewModel ?似乎以前版本的 ViewModelLocator 片段对不同的视图模型有一个静态方法。但那些不再存在,所以我认为这不是要走的路。但什么是?或者,如果我想从视图模型访问定位器,我是否正在完全违反模式?

回答by Depechie

Depends a bit on how you have set up the view model locator, but normally you should be using an IOC container to register all the different ViewModels.

取决于您如何设置视图模型定位器,但通常您应该使用 IOC 容器来注册所有不同的 ViewModel。

For example when using the one of MVVM Light it would be like this: SimpleIoc.Default.Register<MainViewModel>();

例如,当使用 MVVM Light 之一时,它会是这样的: SimpleIoc.Default.Register<MainViewModel>();

So when this is available, you can use that in any other class - so also in another viewmodel by using: SimpleIoc.Default.GetInstance<MainViewModel>();

因此,当它可用时,您可以在任何其他类中使用它 - 在另一个视图模型中也可以使用: SimpleIoc.Default.GetInstance<MainViewModel>();

Or if you told the ServiceLocator you are using SimpleIoc it would be like this: ( the registering ) ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

或者,如果您告诉 ServiceLocator 您正在使用 SimpleIoc,它将是这样的:(注册) ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

And getting it back ServiceLocator.Current.GetInstance<MainViewModel>();

并取回 ServiceLocator.Current.GetInstance<MainViewModel>();

回答by Corcus

Depechie's answer is correct in suggesting that you use the IoC container to retrieve the ViewModels in the same way that the ViewModelLocator does.

Depechie 的回答是正确的,建议您使用 IoC 容器以与 ViewModelLocator 相同的方式检索 ViewModel。

I will however propose another method that will retrieve the ViewModel in the same way that it is done in your XAML.

然而,我将提出另一种方法,该方法将以与在 XAML 中完成的方式相同的方式检索 ViewModel。

XAML (Page.xaml)

XAML (Page.xaml)

DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"

Code (Page.xaml.cs)

代码 (Page.xaml.cs)

this.DataContext = App.Current.Resources["Locator"] as ViewModelLocator).ViewModelName

The above pieces of XAML and Code are equivalent. I hope this helps you undestand the process the app does to retrieve the ViewModels.

上面的 XAML 和代码片段是等效的。我希望这可以帮助您理解应用程序检索 ViewModel 的过程。