MVVMCross - 将相同的 ViewModel 绑定到 2 个不同的视图 [英] MVVMCross - Bind the same ViewModel to 2 different Views

查看:64
本文介绍了MVVMCross - 将相同的 ViewModel 绑定到 2 个不同的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我的场景如下:我有 1 个绑定到 ViewModel 的视图,带有一个项目列表(项目的类是我模型中的一个类).每次从这个列表视图"的列表中选择一个项目时,它的字段都会在另一个详细信息视图"中编辑.这与客户管理示例场景相同,其中从列表中选择客户(在列表视图"中),并且可以在详细信息视图"中更改客户的详细信息.在我的场景中添加的怪癖是,我希望对详细信息视图中的项目所做的更改反映在列表视图中. 更改从详细信息视图保存在 DB 中.

Basically my scenario is the following: I have 1 View bound to a ViewModel, with a List of items(the items' class is a class in my Model). Every time an item is selected from the list in this 'List View', its fields are edited in another, 'Details View'. This is identical to the Customer Management sample scenario, where a customer is selected from the List (in a 'List View') and the the Customer's details can be changed in a 'Details View'. The added quirk in my scenario, is that I want the changes done to the item in the Details View, to be reflected back in the List View. The changes are saved in DB from the Details View.

此外,根据详细信息视图中所做的更改,可以从第一个视图的列表中删除该项目.请注意,这并不是从数据库中删除记录,因为列表视图中的列表只是数据库中项目的过滤列表.我想出了两种可能的方法来做到这一点:

Also, depending on the changes done in the Details View, the item can be removed from list in the first View. Note that this is not deleting the record from the DB, because the list in the List View is just a filtered list of the items in the DB. I figured out two possible ways that I can do this:

  1. 通过将 2 个视图绑定到同一个视图模型

EDIT:如何调用第二个视图?第二个视图实际上会使用共享 ViewModel 的同一个实例吗?

-或-2. 将列表视图中选择的项目通过引用传递到详细信息视图.因此,在本例中,我将维护 1-View-to-1-ViewModel 映射.

-or- 2. By passing the item selected in the List View by reference to the Details View. Therefore in this case, I'm maintaining the 1-View-to-1-ViewModel mapping.

我的问题是:

  • 如何实现场景 1,
  • 在2中,Init()方法(与ShowViewModel()方法结合使用)是否支持传递参数参考?
  • how can I implement scenario 1,
  • in 2, does the Init() method(used in conjunction with the ShowViewModel() method) support passing parameters by reference?

如果您能在答案中包含代码片段/示例,我将不胜感激.

I would be grateful, if you could include code snippets/examples in your answers.

非常感谢,

推荐答案

绑定两个视图到同一个视图模型

binding 2 Views to the same View Model

如果您愿意,您可以自由地在任何 View 上手动设置 ViewModel 属性.只需在调用 base.OnCreate()base.OnNavigatedTo()base.ViewDidLoad() 之前设置 ViewModel = foo;)

You are free to set the ViewModel property on any View manually if you wish to. Simply set ViewModel = foo; before the call to base.OnCreate(), base.OnNavigatedTo() or base.ViewDidLoad()

在2中,Init()方法(与ShowViewModel()方法结合使用)是否支持通过引用传递参数?

in 2, does the Init() method(used in conjunction with the ShowViewModel() method) support passing parameters by reference?

MvvmCross 不支持在 ViewModel 之间传递对象.在 Windows Phone 上,导航由 url 参数执行.在 Android 上,导航由 Intent 执行 - 这些底层机制根本不允许您传递实际的对象引用.

MvvmCross does not support passing objects between ViewModels. On Windows Phone navigations are performed by url parameters. On Android, navigations are performed by Intents - these underlying mechanisms simply do not let you pass actual object references.

一般来说,我使用从数据存储到 ViewModel 的消息来处理这种情况

In general I handle this situation using messaging from the data store to the ViewModel

如果您能在答案中包含代码片段/示例,我将不胜感激.

I would be grateful, if you could include code snippets/examples in your answers.

您可以在 http://mvvmcross.wordpress 中看到我通常如何在 N=12 到 N=15 中执行此操作.com/

这篇关于MVVMCross - 将相同的 ViewModel 绑定到 2 个不同的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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