如何使用mvvmcross在屏幕之间传递数据 [英] How to pass data across screens using mvvmcross

查看:81
本文介绍了如何使用mvvmcross在屏幕之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mvvmcross xamarin项目,试图找出针对特定情况跨屏幕发送数据的最佳方法.

I have an mvvmcross xamarin project I am trying to figure out the best way I should approach sending data across screens for a particular situation.

我的第一个屏幕是搜索过滤器,它包含各种条件供用户选择以过滤结果.第二个屏幕是结果列表屏幕.

My first screen is a search filter, it contains all kinds of criteria for the users to select for filtering results. The second screen is the results list screen.

我已经看到许多解决此问题的方法:

I've seen numerous options for approaching this problem:

1)使用Init方法-我遇到的问题是,当我传递自定义对象时,值始终为null/default.如果我传递一个字符串,它可以正常工作.如果我可以传递自定义对象,则可以传递EntityFilter或结果的实体列表.我确实必须在过滤器页面上查询实体,因为它们在一个过滤器上选择的内容会根据剩余的实体而影响其他实体.我已将以下内容添加到AssemblyInfo.cs中,但仍然无法正常工作.

1) Use the Init method - The issue I'm having here is that when I pass custom objects, the values are always null/default. If I pass a string it works fine. If I could pass custom objects, I would either pass the EntityFilter or a List of Entities which is the results. I do have to query the entities on the filter page because what they select on one filter can affect others based on what entities remain. I have added the following to the AssemblyInfo.cs and it still doesn't work.

[assembly: InternalsVisibleTo("Cirrious.MvvmCross")]

2)使用消息传递.这里的问题是,更改过滤器时未创建结果页,因此消息传递无法正常工作.

2) Use Messaging. The problem here is that the result page isn't created when the filter is changed, so messaging won't work.

3)使用内存中对象.我创建了一个结果服务,其中只包含实体列表的公共属性.它已在IoC中注册为惰性单例(所有服务也是如此).此方法有效,是我当前的解决方案.但这并不正确".感觉好像我做错了什么或违反了最佳做法.这是应该做的方式吗?

3) Use an in memory object. I created a results service that contains nothing but a public property of List of Entities. It's registered as lazy singleton in the IoC (as are all services). This method works and is my current solution. But it doesn't "feel" right. It feels like I'm doing something wrong or breaking best practices. Is this the way it should be done?

推荐答案

这是一个相当常见的问题.

This is a fairly frequently asked question.

最近有一个关于使用自定义类型进行导航的问题,网址为: v3中的导航参数-请注意,原始发布者在那里提出了有关其问题的一种解决方案.

There's a recent question about using custom types for navigation at: Custom types in Navigation parameters in v3 - note that the original poster proposes one solution in their question there.

关于此主题还有其他几个问题.所有这些的基本答案是,在许多平台上,导航都使用序列化技术,例如Xaml Urls或Android Intent.因此,因为逻辑删除"意味着您永远无法确保新"页面与旧"页面位于相同的内存空间,因此默认情况下,MvvmCross坚持对所有ShowViewModel使用简单的可序列化类型导航.

There are also several other questions around on this topic. The basic answer for all of these is that on many platforms navigation uses serialization techniques - e.g. Xaml Urls or Android Intents. Because of this, and because 'tombstoning' means that you can never be sure a "new" page will be in the same memory space as the "old" one, then by default MvvmCross insists on using simple serializable types for all ShowViewModel navigation.

导航到只允许简单的可序列化类型的原因相同,一直重复到MvvmCross v1-例如将对象传递到导航到"导航的最佳方法是什么? MVVMCross中的视图模型?

This same reasoning for why navigation only allows simple serializable types is repeated all the way back to MvvmCross v1 - e.g. What is the best way to pass objects to "navigated to" viewmodel in MVVMCross?

好消息是,这个主题在官方项目文档中也引起了人们的关注-但这些文档仍在进行中,例如参见 https://github.com/MvvmCross/dev.mvvmcross.com/blob/master/_posts/1970-01-01-page-navigation.markdown

The good news is that this topic is also getting some love in the official project docs - but these docs are still a bit of a work in progress - e.g. see https://github.com/MvvmCross/dev.mvvmcross.com/blob/master/_posts/1970-01-01-page-navigation.markdown

这篇关于如何使用mvvmcross在屏幕之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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