我如何知道我是否正确实现MVVM [英] How Do I know if I am implementing MVVM correctly

查看:77
本文介绍了我如何知道我是否正确实现MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用的问题。我正在编写一个程序,最终会有数据表等等。我正在尝试确定我是否正确地正确实施mvvm,因为这是我的第一个应用程序。



该应用程序目前只是该应用程序的基础程序。这是一个带有选项卡控件的简单窗口。



应用程序只是控制一些布局功能,但它即将交叉使用和操作数据。



例如:

主窗口和制表符控件在实际工作表上有非常基本的XAML。这些从全球资源词典中提取样式和设计。字典仅控制公共布局和设计元素。这些控件的视图背后有一些调用全局函数和子函数的基本代码。选项卡的特定布局及其中的任何控件都将写入XML并从中读取。此信息存储在自定义类的集合中:一个用于选项卡本身,另一个用于选项卡中的控件。每当用户更改布局时,视图都会通过其后面的小代码触发全局函数,从而对集合进行调整。当应用程序关闭时,设置将保存到XML文件。



当某些控件链接到SQL数据而不是集合时,将执行类似的流程,我想使用数据表。我知道在WinForms中将数据表绑定到许多控件相对容易。根据我所看到的信息,双向通信的最佳选择是使用一个可观察的集合。我只是不完全确定我是否正在以正确的方式进行项目,或者在我参与之前我是否应该改变现状。



任何建议或帮助文章都会非常感谢。如果有帮助,我可以阅读C#以及vb。

I have a generic question. I am writing a program that will eventually have data tables and so forth. I am trying to determine if I am on the right track for implementing mvvm correctly since this is my first application of such.

The application is currently just the foundation of the program. It is a simple window with a tab control.

The application is just controlling some layout features, but it is about to cross into using and manipulating data.

For example:
The main window and tab control have very basic XAML on there actual sheets. These pull in styles and designs from global resource dictionaries. The dictionaries control only the common layout and design elements. The views for these controls have some basic code behind that calls global functions and subs. The specific layout of the tabs and any controls within them are written into and read from XML. This information is stored into a collection of a custom class: one for the tabs themselves and the other for the controls within the tabs. Whenever the user changes the layout, the view fires a global function through its small code behind which makes adjustments to the collection. When the application is closed, the settings are saved to an XML file.

A similar flow will be done when certain controls are linked to SQL data but instead of a collection, I would like to use data tables. I know that in WinForms it was relatively easy to bind a data table to a number of controls. Based on the information that I have seen, the best option for two way communication is by using an obseravable collection. I am just not entirely sure if I am doing the project the correct way or if I should change things now before I become too involved.

Any suggestions or help articles would be greatly appreciated. I can read C# as well as vb if that helps.

推荐答案

使用MVVM的动机是什么?

参见http://blog.lab49.com/archives/2650 [ ^ ]或更具体地说: http://www.lab49.com/wp-content/uploads/2011/12/Jason_Dolinger_MVVM.wmv [ ^ ]。

干杯

Andi
What is the motivation to use MVVM?
See http://blog.lab49.com/archives/2650[^] or more speciffically: http://www.lab49.com/wp-content/uploads/2011/12/Jason_Dolinger_MVVM.wmv[^].
Cheers
Andi


我不确定我完全理解你的要求,但在我看来,你允许用户更改控件的布局,并希望将布局保存到数据库,目前你在哪里将布局保存为XML。



听起来像你在BaseVie上有一个公共方法wModel类,名为SaveLayout()或类似名称。



您的代码落后,而不是将您的集合搜索到XML将在VM上调用该方法



还需要一个合适的LoadLayout()方法。



所以你的Vm负责获取数据从数据库中提供它(无论你想将它作为DataTable提供还是预先转换成现有的集合都取决于你 - 我个人不喜欢View级别的dataTables)



您的View现在完全按照它对集合的处理方式,用VM.SaveLayout()方法调用替换后面代码中的现有SaveLayoutToXml函数。
I'm not sure I entirely understand what you are asking, but it seems to me that you are allowing the user to change layout of controls, and want to save the layout to a database, where currently you save the layout to XML.

It sounds to me like you would have a public method on a BaseViewModel class, called "SaveLayout()" or similar.

You code behind, instead of szaving your collection to XML would call that method on the VM

An appropriate "LoadLayout()" method would also be required.

So your Vm is responsible for obtaining the data from the database and providing it (whether you wanted to provide it as a DataTable or pre-converted into your existing collection is up to you - I personally don't like dataTables at the View level)

Your View then does exactly what it does with the collection now, replacing your existing "SaveLayoutToXml" function in the code behind with the VM.SaveLayout() method call.


这篇关于我如何知道我是否正确实现MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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