MVVM 模式和嵌套视图模型 - 通信和查找列表

发布于 2024-08-31 23:21:56 字数 1391 浏览 1 评论 0原文

我正在将 Prism 用于我正在创建的新应用程序。有几个查找列表将在应用程序的多个位置使用。因此,定义一次并在需要该功能的任何地方使用它是有意义的。我当前的解决方案是使用类型化数据模板来呈现内容控件内的控件。

   <DataTemplate DataType={x:Type ListOfCountriesViewModel}>
       <ComboBox ItemsSource={Binding Countries} SelectedItem="{Binding SelectedCountry"/>      </DataTemplate>
   <DataTemplate DataType={x:Type ListOfRegionsViewModel}>
       <ComboBox ItemsSource={Binding Countries} SelectedItem={Binding SelectedRegion} />   </DataTemplate>

    public class ParentViewModel
   {
        SelectedCountry get; set;
        SelectedRegion get; set;
        ListOfCountriesViewModel CountriesVM;
        ListOfRegionsViewModel RgnsVM;
   }

然后在我的窗口中,我有 2 个内容控件和其余控件

<ContentControl Content="{Binding CountriesVM}"></ContentControl>
<ContentControl Content="{Binding RgnsVM}"></ContentControl>
<Rest of controls on view>

目前我正在工作,并且组合框的 SelectedItems 正在通过子视图模型中的 EventAggregator 发布事件,然后在父视图模型中订阅这些事件。

我不确定这是最好的方法,因为我可以想象我很快就会遇到很多事件,而且它会变得难以处理。此外,如果我要在另一个窗口上使用相同的视图模型,它将发布该事件,并且该父视图模型会订阅该事件,这可能会产生意想不到的后果。

我的问题是:-

  1. 这是将查找列表放入可跨屏幕重复使用的视图中的最佳方式吗?
  2. 如何使绑定到子视图模型的组合框在父视图模型上设置相关属性而不使用事件/中介器。例如,在这种情况下 SelectedCountry 是什么?
  3. 对于我正在尝试做的事情还有其他实施建议吗?

我有一种感觉,我错过了一些明显的东西,而且有太多信息,很难知道什么是正确的,所以我们将非常感激地收到任何帮助。

I am using Prism for a new application that I am creating. There are several lookup lists that will be used in several places in the application. Therefore it makes sense to define it once and use that everywhere I need that functionality. My current solution is to use typed data templates to render the controls inside a content control.

   <DataTemplate DataType={x:Type ListOfCountriesViewModel}>
       <ComboBox ItemsSource={Binding Countries} SelectedItem="{Binding SelectedCountry"/>      </DataTemplate>
   <DataTemplate DataType={x:Type ListOfRegionsViewModel}>
       <ComboBox ItemsSource={Binding Countries} SelectedItem={Binding SelectedRegion} />   </DataTemplate>

    public class ParentViewModel
   {
        SelectedCountry get; set;
        SelectedRegion get; set;
        ListOfCountriesViewModel CountriesVM;
        ListOfRegionsViewModel RgnsVM;
   }

Then in my window I have 2 content controls and the rest of the controls

<ContentControl Content="{Binding CountriesVM}"></ContentControl>
<ContentControl Content="{Binding RgnsVM}"></ContentControl>
<Rest of controls on view>

At the moment I have this working and the SelectedItems for the combo boxes are publising events via EventAggregator from the child view models which are then subscribed to in the parent view model.

I am not sure that this is the best way to go as I can imagine I would end up with a lot of events very quickly and it will become unwieldy. Also if I was to use the same view model on another window it will publish the event and this parent viewmodel is subscribed to it which could have unintended consequences.

My questions are :-

  1. Is this the best way to put lookup lists in a view which can be re-used across screens?
  2. How do I make it so that the combobox which is bound to the child viewmodel sets the relevant property on the parent viewmodel without using events / mediator. e.g in this case SelectedCountry for example?
  3. Any alternative implementation proposals for what I am trying to do?

I have a feeling I am missing something obvious and there is so much info it is hard to know what is right so any help would be most gratefully received.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2024-09-07 23:21:56

在您的场景中,父母似乎知道孩子的类型。在这种情况下你不能只使用属性吗? :

ListOfCountriesVM 公开 SelectedCountry 的属性。

在父 VM 中,SelectedCountry 的实现仅返回 ListOfCountriesVM.SelectedCountry。

您可以对任何知道自己拥有 ListOfCountriesVM 的父视图模型执行此操作。

根据我的经验,消息传递/事件聚合实际上应该仅在发布者不关心谁在收听或有多少听众的情况下使用。例如:

  1. 向应用程序中的其他模块发送通知 向
  2. 同一模块中您无法保证存在的视图模型发送通知。

在您的情况下,订阅者(父级)已经知道发布者(子级)是谁,因此使用属性的简单解决方案似乎是最合适的。我认为这种特定场景不需要基于消息传递或事件的解决方案。

In your scenario, it seems that the parent is aware of the type of child. Can't you just use properties in this situation? :

ListOfCountriesVM exposes a property for SelectedCountry.

In the parent VM, the implementation of SelectedCountry simply returns ListOfCountriesVM.SelectedCountry.

You could do this for any Parent View Model that is aware that it owns a ListOfCountriesVM.

In my experience, messaging/event aggregation should really only be used in scenarios where the publisher doesn't care who's listening or how many listeners there are. For example:

  1. Sending notifications to other modules in the application
  2. Sending notifications to view models in the same module that you can't guarantee exist.

In your situation, the subscriber, (the parent) already knows who the publisher (the child) is, so a simple solution using properties seems to be the most appropriate. I don't think this particular scenario warrants a messaging or event based solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文