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
123 views
in Technique[技术] by (71.8m points)

c# - Accessing a property in one ViewModel from another

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

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

1 Reply

0 votes
by (71.8m points)

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);

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

...