Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want main viewmodel to have a certain list, and then access from many other viewmodels.

For example, in MainViewModel.cs I will have a list of 50 numbers, then in NumListViewModel.cs, I'd like to access it in order to show it as a list, and in AddNumViewModel.cs I'd like to be able to update that list.

It's been suggested that I use events / evenaggerator, which I did, but unfortunately, for all I know all I can do with it is send a num from one view to another and tell it to update the list, but the problem is, as the program grows, I will need to have a lot of subscribers in the main view model, and when something actually happens I will have to "publish" events according to the number of subscribers which makes it even harder to maintain.

I also found another answer, instructing to create an instance of anotherVM within the mainVM, with a parameter set to "this" which is a reference to the mainVM. It works, but then again, it could get quite long.

So my question is, is there a better way to access a property from another VM?
Like literally have the an instance of the class that holds the list in the mainVM, and then just be able to update / access it from the other VMs, without having to explicitly program which VM can. Would make life so much easier.

In your answer, please try to avoid suggesting frameworks. Although there are some really good ones, I want to be able to do at least that by myself.

For example:

MainVM.cs:

public class MainVM
{
    List lst = new List(); //Let's just say it's full...
}

OtherVM.cs:

public class OtherVM
{
    lst.Add(3);
}

PS: Yes I know it has been asked already, and yes I have done my research, BUT I the answers I found are too 'static', I guess?

See Question&Answers more detail:os

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

1 Answer

If you want direct access to the list from an external ViewModel, then your options are to:

  1. Pass the List to the OtherVM as a constructor argument or public property. Then the OtherVM can treat it like a member.

  2. Pass the MainVM to the OtherVM as a constructor argument or public property. Then the OtherVM can access the List by first accessing the MainVM.

  3. Give the MainVM a static property called "Default" or "Instance," so you can access the static instance of MainVM from within OtherVM, without assigning it as a member field.

Example:

public class MainVM
{
    private static MainVM _instance = new MainVM();
    public static MainVM Instance { get { return _instance; } }

    public List<XX> MyList { get; set; }
    //other stuff here
}

//From within OtherVM:
MainVM.Instance.MyList.Add(stuff);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...