什么是传递对象到&QUOT的最佳途径;导航到"在MVVMCross视图模型? [英] What is the best way to pass objects to "navigated to" viewmodel in MVVMCross?

查看:111
本文介绍了什么是传递对象到&QUOT的最佳途径;导航到"在MVVMCross视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经包含其中有一个玩家属性,该属性是玩家对象的列表一个团队视图模型。在TeamView球队深加载,因此玩家数据已经在内存中。

什么是给定的选择Player类的实例传递给PlayerView的最佳方式?

问题是,MVVMCross视图模型构造只能包含在当前版本字符串的属性。

我有以下观点:


  1. 传递选定播放器的ID和Team.Players属性分配作为一个ViewModel到PlayerView。这可能是一个合理的解决方案,如果选择的球员只是在PlayerView注目选手和PlayerView是真正的玩家的观点,在这里用户可以在其他球队的球员之间滑动为好。


  2. 有一个ASP.Net MVC像ViewBag服务,可以只携带导航动作之间的数据,在字典一样存储,并传递给PlayerView参数是viewbag:PlayerId123,这是一个特殊的键指向类的实例。


  3. 序列化选择的对象为字符串,并把它作为序列化对象的构造函数。这是可能的,但我不喜欢这种解决方案。



解决方案

在一般的导航,MvvmCross只允许字符串的ViewModels之间传递。

这样做的原因是,在导航需要通过机制如Xaml中尤里斯或Android意图在一个平台级别上完成

有关你建议的情况下,一般模式我会typcially使用方法是:


  • 的TeamViewModel使用注入ITeamService网络获取数据团队

  • 的TeamViewModel还使用注射单ITeamCache缓存小组

  • 的导航通过调用偏偏喜欢:

this.RequestNavigate< PlayerViewModel>(新{teamId,playerId})


  • 的PlayerViewModel然后接收TeamId和PlayerId在其构造,并采用ITeamCache收集正确的球员

这code可能是这样的:

 公共类TeamViewModel
     :MvxViewModel
     ,IMvxServiceConsumer< ITeamCache>
 {
     公共TeamViewModel(字符串teamId,串playerId)
     {
         VAR teamCache = this.GetService< ITeamCache>();
         播放器= teamCache.GetPlayer(teamId,playerId);
         如果(玩家== NULL)
         {
             // TODO - 不知何故处理此错误!
         }
     }     公共Player播放器{搞定;组; }
 }

注意,code以上测试播放器是。这是因为有一个与你的假设一个问题在TeamView球队深加载,因此玩家数据已经在内存中。

问题是,像Android和WP7平台,操作系统可以自由地从内存中删除应用程序,并稍后再重新启动它。这种被称为墓碑上WP7 的,只是好像是叫的Killed 在Android上。

在这种情况下,则该操作系统可能会后,当用户返回重新启动应用程序。此重新启动将直接到用户最后的活动,它会记住后退堆栈 - 那么这将是到你的应用程序正常补充水分任何需要的物品放回内存

下面是一些很小的图片解释这个...

有关更多详细信息,请参见 Xamarin 并的MSDN


有关您的球队/球员的情况下,你也许可以通过补液,以应付:


  • 实施ITeamCache为文件支持的对象 - 例如它可以使用JSON文件或SQLite数据库作为持久性存储的内存中的数据

  • 实现在code一些逻辑,在需要的时候从网络上的数据重新提取

  • 实现在这些情况下,一些紧急导航的背回家的策略 - 因为这些情况不会发生,往往在富有现代气息的手机资源很多应用。

  • 只需崩溃 - 虽然这是不可取...

这并不奇怪,许多应用程序不处理立碑很好...


注 - 小物件,你的选择3(序列化)就可以工作得很好 - 但是,这不会应用在哪里发生水化和用户,然后从PlayerViewModel到TeamViewModel导航回帮你的情况。


有关更多的一些内MvvmCross在Android生命周期的最新变化,请参阅http://slodge.blogspot.co.uk/2012/05/android-application-initialization-and.html

I've a ViewModel which contains a Team which has a Players property which is a list of Player objects. Within TeamView the Team is deep loaded, so player data is already in the memory.

What is the best way to pass a given selected Player class instance to a PlayerView?

The problem is that MVVMCross ViewModel constructors can only contain string properties in current version.

I've the following ideas:

  1. Pass the Id of the selected Player and assign the Team.Players property as a ViewModel to the PlayerView. This can be a reasonable solution if the selected player is just the focused player in the PlayerView and the PlayerView is really a "players" view, where the user can swipe between the other team players as well.

  2. Have a ASP.Net MVC like ViewBag service which can carry data only between navigate actions, in a Dictionary like storage, and the parameter passed to PlayerView is a "viewbag:PlayerId123" which is a special key pointing to the class instance.

  3. Serialize the selected object to string and pass it as serialized object to the constructor. It is possible but I don't like this solution.

解决方案

In general navigation, MvvmCross only allows strings to be passed between ViewModels.

The reason for this is that the navigation needs to be done at a platform level via mechanisms such as Xaml Uris or Android Intents.

For the situation you suggest, the general pattern I would typcially use is:

  • that the TeamViewModel gets the team data from the network using an injected ITeamService
  • that the TeamViewModel also uses an injected singleton ITeamCache to cache the Team
  • that the navigation happens via a call like:

this.RequestNavigate<PlayerViewModel>(new { teamId, playerId })

  • the PlayerViewModel then receives the TeamId and PlayerId in its constructor, and uses the ITeamCache to collect the right player

This code might look like:

 public class TeamViewModel 
     : MvxViewModel
     , IMvxServiceConsumer<ITeamCache>
 {
     public TeamViewModel(string teamId, string playerId)
     {
         var teamCache = this.GetService<ITeamCache>();
         Player = teamCache.GetPlayer(teamId, playerId);
         if (Player == null)
         {
             // todo - handle this error somehow!
         }
     }

     public Player Player { get; set; }
 }

Notice that the code above tests for whether Player is null. This is because there is a problem with your assumption "Within TeamView the Team is deep loaded, so player data is already in the memory."

The problem is that in platforms like Android and WP7, the operating system is free to remove your application from memory and to then restart it later. This is referred to as Tombstoning on WP7, but just seems to be called Killed on Android.

In these cases, then the operating system may restart your application later when the user navigates back. This restart will go direct to the activity where the user last was, and it will remember the back stack - it will then be up to your application to properly rehydrate any required objects back into memory.

Here are some very small pictures explaining this...

For more detail, see Xamarin and MSDN


For your Team/Player case, you might be able to cope with rehydration by:

  • Implementing the ITeamCache as a file-backed object - e.g. it could use a JSON file or a SQLite database as a persistent store for the in-memory data
  • Implementing some logic in your code that refetches data from the network when needed
  • Implementing some emergency-navigate-back-home strategy in these cases - as these cases don't happen that often in many applications on modern resource rich phones.
  • Just crashing - although this isn't advisable...

It's no surprise, that many applications don't handle tombstoning very well...


Note - for small objects, your option 3 (serialization) can works well - however, this wouldn't help you with the situation where app rehydration occurs and a user then navigates back from a PlayerViewModel to a TeamViewModel.


For more on some of the recent changes on Android lifecyle within MvvmCross, see http://slodge.blogspot.co.uk/2012/05/android-application-initialization-and.html

这篇关于什么是传递对象到&QUOT的最佳途径;导航到&QUOT;在MVVMCross视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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