Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

c# - 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. 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. 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.

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?

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?

EDIT2:

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. I'm currently trying the possible solution of first edit.

MainViewModel.cs (others are similar, with reference only to the main 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;
    }
...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can actually use the 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:

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.

from xaml: Resources (defaultly is in Application.Resources in App.xaml)

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

and DataContext for views (either user controls or windows or whatever)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...