简体   繁体   English

在MVVM Light中访问其他ViewModel中的属性

[英]Accessing Properties in other ViewModels in MVVM Light

I have a main ViewModel containing a List of items which I'm using in a certain amount of UserControls , which are displayed in a ContentControl on the main view. 我有一个主ViewModel其中包含我正在一定数量的UserControls使用的项的列表,这些项显示在主视图的ContentControl My current way of exchanging data between the ViewModels exists of making a reference to each of the ViewModels in the main ViewModel , and one of the main ViewModel in every UserControl . 我的电流之间交换数据的方式ViewModels存在使得对每个的一个参考的ViewModels在主ViewModel ,主要的一个ViewModel在每一个UserControl However I do not know is this is the right way of doing this, since I have a ViewModelLocator and I kind of expect this class to have some functionality to support something like this. 但是我不知道这是这样做的正确方法,因为我有一个ViewModelLocator并且我有点希望此类具有某些功能来支持此类功能。

Can anyone please tell me if what I'm doing this OK, or if there is a better way of doing this in MVVM Light? 谁能告诉我我在做什么,还是在MVVM Light中有更好的方法?

EDIT: 编辑:

I found this when I was searching for something different, would this be a better solution? 我在寻找其他东西时发现了这个,这会是一个更好的解决方案吗?

private ViewModelLocator locator = new ViewModelLocator();

And then using the locator Properties to reference each ViewModel? 然后使用定位器属性来引用每个ViewModel?

EDIT2: 编辑2:

Apparently what I thought would work doesn't, at first I had only references in the main ViewModel and this worked, but when I try this in the UserControls it kind of crashes my app. 显然我认为不会起作用,起初,我在主ViewModel仅具有引用,并且起作用了,但是当我在UserControls尝试该操作时,它会使我的应用程序崩溃。 I'm currently trying the possible solution of first edit. 我目前正在尝试首次编辑的可能解决方案。

MainViewModel.cs (others are similar, with reference only to the main ViewModel) MainViewModel.cs(其他类似,仅参考主ViewModel)

public class MainViewModel : ViewModelBase
{
    private ItemAddViewModel itemAddViewModel;
    private ItemsViewModel itemsViewModel;

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        ItemsList = Item.GetItemsList();

        itemAddViewModel = ServiceLocator.Current.GetInstance<ItemAddViewModel>();
        itemsViewModel = ServiceLocator.Current.GetInstance<ItemsViewModel>();

        ShowItemsView();
    }
...
    private void ShowItemsView()
    {
        CurrentControl = itemsViewModel;
    }
...

You can actually use the ViewModelLocator. 您实际上可以使用ViewModelLocator。 Defaultly is uses the Inversion of Control Container, so even if you create a new instance of the Locator, you will get the same instance of singleton viewmodels from the container. 默认情况下是使用“控制容器反转”,因此,即使您创建了定位器的新实例,也将从容器中获得相同的单例视图模型实例。

The Locator class: Locator类:

static ViewModelLocator()
{
    ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
    SimpleIoc.Default.Register<ViewModel1>();
    SimpleIoc.Default.Register<ViewModel2>();
    SimpleIoc.Default.Register<ViewModel3>();
}

public ViewModel1 ViewModel1
{
    get
    {
        return ServiceLocator.Current.GetInstance<ViewModel1>();
    }
}

public ViewModel2 ViewModel2
{
    get
    {
        return ServiceLocator.Current.GetInstance<ViewModel2>();
    }
}

public ViewModel3 ViewModel3
{
    get
    {
        return ServiceLocator.Current.GetInstance<ViewModel3>();
    }
}

then from code you can access it as 然后可以从代码中访问它

var vm1 = (new ViewModelLocator ()).ViewModel1;

you get the one and only instance of viewmodel. 您将获得viewmodel的唯一实例。

from xaml: Resources (defaultly is in Application.Resources in App.xaml) 来自xaml:资源(默认位于App.xaml中的Application.Resources中)

<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

and DataContext for views (either user controls or windows or whatever) 和用于视图的DataContext(用户控件或Windows或其他任何控件)

<UserControl
    ... 
    DataContext="{Binding ViewModel1, Source={StaticResource Locator}}"
    ...    >

If all you need is to bind a property from the main viewmodel, while inside the content control, just use this syntax: 如果您只需要在内容控件内部绑定主视图模型的属性,则只需使用以下语法:

   ... Binding="{DataContext.mainvmpropertyname, ElementName=xxxx}"

where xxxx is the Name attached to the content control (or any control that has the main viewmodel as its DataContext). 其中xxxx是附加到内容控件(或任何具有主viewmodel作为其DataContext的控件)的名称。 Alternatively, you can use relative binding instead of element name. 或者,您可以使用相对绑定代替元素名称。

You could access the public properties of the ViewModel Locator programatically by getting the Locator from the Apps resources: 您可以通过从Apps资源获取Locator来以编程方式访问ViewModel Locator的公共属性:

MyViewModel vm = (App.Current.Resources["Locator"] as ViewModelLocator).MyViewModel

or by creating another static instance in the ViewModelLocator class: 或通过在ViewModelLocator类中创建另一个静态实例:

public class ViewModelLocator
{
     public static ViewModelLocator Instance = new ViewModelLocator();

     static ViewModelLocator(){ ... }

     public MainViewModel Main
     {
     ...
     }
}

Similar Thread 类似线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM