从另一个ViewModel访问一个ViewModel中的属性 [英] Accessing a property in one ViewModel from another

查看:259
本文介绍了从另一个ViewModel访问一个ViewModel中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望主视图模型具有特定列表,然后从其他许多视图模型进行访问.

例如,在MainViewModel.cs中,我将列出50个数字, 然后在NumListViewModel.cs中,我想访问它以便将其显示为列表,而在AddNumViewModel.cs中,我希望能够更新该列表.

有人建议我使用事件/evenaggerator,但不幸的是,就我所知,我所能做的就是将一个视图中的num发送给另一个视图,并告诉它更新列表,但这是问题所在是,随着程序的增长,我将需要在主视图模型中拥有很多订阅者,并且当实际发生某些事情时,我将不得不根据订阅者的数量来发布"事件,这使得维护变得更加困难. /p>

我还找到了另一个答案,指示在mainVM内创建另一个VM的实例,并将参数设置为"this",这是对mainVM的引用. 它可以工作,但是再一次,它可能会变得很长.

所以我的问题是,是否有一种更好的方法可以从另一个VM访问属性?
就像从字面上来看,该类的实例将其保存在mainVM中一样,然后可以从其他VM更新/访问它,而不必显式地编程哪个VM可以.会让生活变得如此轻松.

在回答时,请尽量避免提出框架建议. 尽管有一些非常好的,但我希望至少能够自己完成.

例如:

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:是的,我知道已经有人问过了,是的,我已经完成了研究,但是我发现我发现的答案太静态"了吗?

解决方案

如果您要 direct 从外部ViewModel访问列表,则可以选择:

  1. 将列表作为构造函数参数或公共属性传递给OtherVM.然后,OtherVM可以将其视为成员.

  2. 将MainVM作为构造函数参数或公共属性传递给OtherVM.然后,OtherVM可以通过首先访问MainVM来访问列表.

  3. 为MainVM提供一个名为默认"或实例"的静态属性,以便您可以从OtherVM内部访问MainVM的静态实例,而无需将其分配为成员字段.

示例:

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

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?

解决方案

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

这篇关于从另一个ViewModel访问一个ViewModel中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆