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

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

问题描述

我希望主视图模型有一个特定的列表,然后可以从许多其他视图模型访问.

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

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

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.

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

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.

我还找到了另一个答案,指示在 mainVM 中创建 anotherVM 的一个实例,并将参数设置为this",这是对 mainVM 的引用.它有效,但话又说回来,它可能会很长.

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.

所以我的问题是,是否有一种更好的方法可以从另一个 VM 访问属性?
就像字面上有一个在 mainVM 中保存列表的类的实例,然后只需能够从其他 VM 更新/访问它,而无需显式编程哪个 VM 可以.会让生活变得更轻松.

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.

例如:

MainVM.cs:

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

OtherVM.cs:

OtherVM.cs:

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

PS:是的,我知道已经有人问过了,是的,我已经做了研究,但我发现的答案太静态"了,我猜?

推荐答案

如果您希望直接从外部 ViewModel 访问列表,那么您的选择是:

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

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

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

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

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.

为 MainVM 提供一个名为Default"或Ins​​tance"的静态属性,以便您可以从 OtherVM 中访问 MainVM 的静态实例,而无需将其分配为成员字段.

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.

示例:

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 访问一个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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