gpt4 book ai didi

c# - 模型应该如何在 MVVM 中实现?

转载 作者:行者123 更新时间:2023-12-03 11:00:14 27 4
gpt4 key购买 nike

我正在创建一个与一个或多个服务通信的简单网络应用程序,因此我计划使用一些队列(用于传出消息和传入消息)、一个表、一个包含每个事件连接状态的列表等:在其他换句话说,这些是确保应用程序本身运行所需的数据结构。

此应用程序还必须配备图形界面,显示应用程序本身的部分内部功能:例如,队列的填充状态、连接的状态(检测到的速度等)等。根据Model-View-ViewModel 模式,模型由要在 GUI 中显示的数据组成:在此应用程序中,上述数据结构表示模型。换句话说,Model 实现了应用程序的业务逻辑。

ViewModel 应该实现 INotifyPropertyChanged接口(interface)是为了通知 View 发生了变化,但是 Model 如何与 ViewModel 通信呢?看完this article ,我意识到 INotifyPropertyChanged接口(interface)由模型实现。 This answer解释了一点,但它让我有点困惑:

INotifyPropertyChanged - Should go into ViewModel and Model (if needed)



为什么,如果需要?
我应该什么时候实现这个接口(interface)?
我什么时候不应该实现它?

此外, Dictionary 没有实现 INotifyPropertyChanged接口(interface):如果我使用它,我应该用实现这个接口(interface)的类来包装它吗?

最后,模型应该是只读的,这意味着用户不能使用 GUI 更改内部数据结构的内容。如何做到这一点?

最佳答案

how does the Model to communicate with the ViewModel
任何你想要的方式。在我们编写的大多数应用程序中, View 模型都会调用业务逻辑层(模型)。但是,如果您需要在模型更改时立即通知 View 模型(事件),您可以在模型上实现 INotifyPropertyChanged。或者您可以简单地让 View 模型订阅模型上的事件。
Moreover, the Dictionary does not implement the INotifyPropertyChanged interface: if I use it, should I wrap it with a class which implements this interface?
您只需要让 View 模型实现 INotifyPropertyChanged。 View 模型(字典)中的属性将简单地调用 NotifyPropertyChanged(或任何您的实现看起来像)。
Finally, the model should be read-only, meaning that the user does not be able to change the contents of internal data structures using the GUI. How to accomplish this?
不要向用户提供让他们更改数据的功能。以一种方式进行绑定(bind),或者干脆不为他们提供进行更改的 API。

关于c# - 模型应该如何在 MVVM 中实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11996464/

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