8

我正在迈出 MvvmCross 框架的第一步,我正在尝试确定项目和类结构方面的最佳方法。我现在最关心的是决定如何组织我的视图模型以便在它们之间共享数据,同时遵循 mvvm 指导。

我有关于视图和各自视图模型(主要和配置)的简单示例。主视图有一些控件绑定到视图模型中的属性。配置视图使用户能够更改文本颜色、列表中的项目数量等......当用户更改配置时,这应该反映在主视图中。

我的第一种方法是创建单独的视图和视图模型。但是如何通知主视图配置已更改?我在Github/Slodge下看到了 Sphero 项目,我意识到视图模型可以直接引用其他视图。这样,每次配置更改时通知主视图相当容易。但这不是 mvvm 推荐的解耦视图模型的偏差吗?

我能否就处理这种类型的类结构的最佳方法获得一些见解?

4

2 回答 2

7

the best way to approach this type of class structuring

In my opinion, the most important measure for "the best way" is to choose a way that means your app works.


The example you've looked at - sphero ball control - has examples both of independent view models (home, gangnam, about, sphero, etc), and it has an example of some view models that know about each other - the sphero view model and it's sub view models.

In this particular case, the reason those view models all know about each other is because they are all part of a single display - they are designed to sit together inside a single grid, pivot, or tabbed UI. To assist with them cooperating I've given them references to each other via IParent and IChild interfaces.

isn't this a deviation of the decoupled viewmodels that mvvm recommends?

I admit this does mean that the design isn't perfectly decoupled, and that if I need to move or reuse those child vm's in the future, then I might need to do a little code refactoring - but right now the design works well for me, and I personally am happy do that refactoring in the future if I need to.


My biggest concern now is to decide how should I organize my viewmodels in order to share data between them and at the same time to follow the mvvm guidance

If you are building a composite (tabbed) view and you're not happy using aggregated view models (like the sphero vm and it's subviews), then please don't.

I'm sure the same effect could have been achieved in sphero using one big view model, using multiple view models communicating via a messenger or using multiple view models communicating via one or more custom services.

For the particular example you give - of a configuration and main view, I don't think this fits the tabbed scenario - and I would probably code this using a messenger - that would provide me with a flexible mechanism for broadcasting and receiving change notifications around various parts of the app.

However, other designs are definitely available and viable - eg you could easily just ask the view model to refresh its configuration each time its shown (doing this would require hooking into onnavigatedto, viewwillapear and on resume calls in the views).


In summary:

  • I agree with your general goal of writing independent view models and making them communicate via services.
  • in the case of a main page and a settings page, I probably would use 2 separate vm's and a messenger
  • In the case of tabbed/pivot UIs I personally have broken away from that perfect paradigm - but there's nothing in mvx that forces you to follow me
于 2013-01-14T08:58:23.457 回答
2

在我使用 MvvmCross 构建的项目中,我决定使用视图模型来表示视图(屏幕)及其状态。我的视图模型使用 ISQLiteConnectionFactory 连接到 SQLite 数据库。我使用存储在 SQLite 数据库中的模型来表示项目中事物的状态。我的视图模型从 SQLite 数据库中获取模型,分析它们并做出反应。

例子。我有一个屏幕,用户可以在其中标记他/她想要在设备上下载一段数据。我的模型有一个标志是应该下载还是已经下载了那条数据。当我打开另一个负责下载数据的屏幕时,它会查看 SQLite 中是否有任何模型被标记为下载并对其做出反应。

这就是我决定在视图模型之间共享数据的方式。优点是数据始终保存在数据库中,强制关闭应用程序不会导致数据丢失(或未按预期发生)。

于 2013-01-15T14:03:10.230 回答