gpt4 book ai didi

c# - 如何与多个 ViewModel 共享多个 ObservableCollection?

转载 作者:太空宇宙 更新时间:2023-11-03 12:23:29 25 4
gpt4 key购买 nike

我正在编写一个包含 4 个 View 的程序:CompanyViewMembersViewWeeksViewReportsView。每一个都有对应的ViewModel和Model。我使用 PRISM BindableBase 来创建 ViewModel。数据正确绑定(bind)。

  • CompanyViewModel 包含类型为 Company 的对象。
  • MembersViewModel 包含 Member 对象的 ObservableCollection。
  • WeeksViewModel 包含 Week 对象的 ObservableCollection。

导航是通过主窗口顶部的按钮执行的,可以随时选择任何 View

Container.RegisterTypeForNavigation<CompanyView>("CompanyView");
Container.RegisterTypeForNavigation<MembersView>("MembersView");
Container.RegisterTypeForNavigation<WeeksView>("WeeksView");
Container.RegisterTypeForNavigation<ReportsView>("ReportsView");

问题是 WeeksViewModel 必须可以访问 Members ObservableCollectionReportsViewModel 必须可以访问Company ObjectMembers ObservableCollectionWeeks ObservableCollection

我不确定如何实现它。如何轻松地在 ViewModel 之间共享数据?

我已经尝试使用 PRISM IEventAggregator 在更新时发布 ObservableCollections,这可行,但是必须先访问 View 才能收听事件。如果用户之前没有点击“Weeks” View ,更新后的 MembersCollection 将不会到达 WeeksView。我可以预先初始化 View 吗?我该怎么做?

我关注了 MVVM Made Simple with Prism - Webinar ( https://www.youtube.com/watch?v=ZfBy2nfykqY ) 并发现了评论中写的相同问题。 Brian Lagunas 建议解决此问题的唯一方法是通过导航参数:

I have a small problem with the UpdateEvent passing the message from ViewAViewModel to ViewBViewModel. It doesn't seem to work if I have not visited ViewB, I assume this is because the ViewBViewModel has not yet been instantiated until the view for it has been loaded at least once.

Anybody got any ideas on this, I assume instantiating all viewmodels before they're needed is a bad idea, so how can you get default information into a viewmodel from other viewmodels before it's been instantiated?

Brian Lagunas: The only way to do it is to pass that information as a parameter when you navigate to ViewBViewModel.

我考虑过使用导航参数,但似乎您必须知道从哪里导航到哪里。例如。从 MembersView> WeeksView,将 Members Collection 作为参数传递。但是用户可以按任何顺序导航,包括在程序加载时直接导航到 WeeksView。例如WeeksView 如何从公司 View 中获取成员集合?公司不知道成员(member)收藏。

我对其他想法持开放态度,我进行了广泛的研究,但完全陷入困境:-(

非常感谢您的想法和帮助!亲切的问候,达米安

最佳答案

The problem is that WeeksViewModel must also have access to the Members ObservableCollection. And the ReportsViewModel must also have access to the Company Object, Members ObservableCollection and the Weeks ObservableCollection.

I am not sure how to implement this. How can I easily share the data between the ViewModels?

您可以使用单个 View 模型,其中包含每个特定 View 模型的属性以及要在两个或多个 View 模型之间共享的公共(public)属性,例如:

class MainViewModel
{
public CompanyViewModel CompanyViewModel { get; set; }
public MembersViewModel MembersViewModel { get; set; }
public WeeksViewModel WeeksViewModel { get; set; }

public ObservableCollection<Members> Members { get; set; }
}

然后每个 subview 都可以继承父窗口的 DataContext 并跨所有 View 模型类型绑定(bind)到它们想要的任何属性。

另一种选择是在 View 模型之间来回传递信息,可以使用直接引用、事件聚合器或共享服务。

但是,如果它们仍然必须访问彼此的属性,那么拼命尝试分离 View 模型之间的逻辑似乎毫无意义。所以我可能会选择第一种方法。

关于c# - 如何与多个 ViewModel 共享多个 ObservableCollection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46199892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com