通知模型对viewmodel进行更改的推荐方法是什么 [英] What is the recommended way to inform a model of a change to viewmodel

查看:97
本文介绍了通知模型对viewmodel进行更改的推荐方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次发现MVVM并且正在使用Caliburn.Micro。



我可以通过一个真实的例子清楚地表达我的问题:



在我的视图中,我有一个组合框,用户可以从中选择一个可用的Comport。所选项目绑定到ViewModel中的属性。

在我的一个模型中,我有一个扫描仪类,它将使用用户选择的Comport。



这意味着scanner类需要知道ViewModel中Comport属性的更改。问题是,实现这个的最佳方法是什么?



我考虑过:



< b> 1:当有变化时,直接从ViewModel设置Scanner类中的属性/调用方法:



I am discovering MVVM for the first time and am using Caliburn.Micro.

I can articulate my question with a real example:

In my View I have a combobox from which the user can select an available Comport. The selected item is bound to a property in the ViewModel.
In one of my Models I have a scanner class which will use the Comport that the user selected.

This means that the scanner class needs to know about changes to the Comport property in the ViewModel. The question is, what is the best way to implement this?

I have considered:

1: Set properties / call methods in the Scanner class directly from the ViewModel when there is a change:

public string SelectedPartScannerComPort
{
   get { return _selectedPartScannerComPort; }
   set
   {
       _selectedPartScannerComPort = value;
       NotifyOfPropertyChange(() => SelectedPartScannerComPort);
       OnSelectedPartScannerComPortChanged();  // Access model directly from this method in the ViewModel to make changes.
   }
}





...

...



...
...

private void OnSelectedPartScannerComPortChanged()
{
    Models.Equipment.PartScanner.SetupPort(SelectedPartScannerComPort); // Access the model to implement the change.
}





这有效,但这意味着ViewModel中包含了特定于模型操作的代码。



2:

我相信ViewModel中的通知属性是一个事件,所以我应该可以从模型中订阅它。



This works but it means that the ViewModel has code in it which is specific to the operation of the Model.

2:
I believe the notifying property in the ViewModel is an event, so I should be able to subscribe to it from the Model.

// In the Model:
shellViewModel.SelectedPartScanner.PropertyChanged += OnPartScannerPropertyChanged;



我无法尝试,因为我正在努力发现ShellViewModel的声明位置,以便我可以访问它订阅事件的实例。

无论如何,这意味着模型需要了解View模型。



似乎无论我将做什么,ViewModel都需要了解Model o模型需要知道ViewModel。

这是可接受的做法吗?还有更好的方法可以解决这个问题吗?



我尝试了什么:



我试过从viewmodel直接设置模型中的属性/调用方法。它有效,但我担心我误解了MVVM。


I haven't been able to try it as I am struggling to discover where the ShellViewModel is declared so that I can access its instance to subscribe to the events.
In any case, this would mean the Model needs to know about the View Model.

It seems like no matter what I will do, either the ViewModel needs to know about the Model or the Model needs to know about the ViewModel.
Is this acceptable practice? Are there better ways to go about this?

What I have tried:

I have tried directly setting properties / calling methods in the model from the viewmodel. It works but I am concerned that I am misunderstanding MVVM.

推荐答案

如果你的Model的基类实现了 INotifyPropertyChanged ,然后您的ViewModel可以订阅/取消订阅Model的 PropertyChanged 事件。
If your base class for your Model implements the INotifyPropertyChanged, then your ViewModel can subscribe/unsubscribe from the Model's PropertyChanged event.


这篇关于通知模型对viewmodel进行更改的推荐方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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