在MonoDroid/MonoTouch中使用ViewModel [英] Consuming ViewModels in MonoDroid / MonoTouch

查看:89
本文介绍了在MonoDroid/MonoTouch中使用ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经决定涉足MonoDroid和MonoTouch,并将我的WP7应用程序之一作为入门程序使用.我真的很想重用我现有的ViewModel,但是由于Android和iOS似乎都没有XAML强大的数据绑定之类的东西,所以我想问问是否有人以前走过那条路线并且可以推荐一些最佳实践或现有解决方案.

I've decided to dabble a bit in MonoDroid and MonoTouch and port one of my WP7 apps as a starter. I would really like to reuse my existing ViewModels but since both Android and iOS seem to have no such thing as XAML's strong databinding I would like to ask if anyone went that route before and can recommend some best practices or existing solutions.

推荐答案

我们现在正在使用一个应用程序执行此操作,但是首先要为iOS编写(甚至在Windows之前).当然,这里到处都是彩虹和小马.

We're doing this with an application right now, but writing for iOS first (even before Windows). It is not full of rainbows and ponies for sure.

我建议以下内容:

  • 在Windows上使用不需要您对用户执行的每个操作公开ICommand的MVVM框架(例如 Caliburn ),它也不需要在您所有的ViewModels中都依赖于它.
  • 有条件地继承ViewModelBase类的WPF特定部分,您可以使用部分类或#if iPhone指令执行此操作. INotifyPropertyChanged或ICommand是示例.
  • 使用IoC容器,将诸如将设置保存到文件系统之类的东西抽象出来非常有帮助,这在所有平台上都会大不相同.此外,它还有助于理清您的依赖关系,这对于将平台特定的代码与非平台特定的代码分开非常有帮助.
  • 使用某种类型的信使"(此处的示例),通常包含在MVVM框架.我认为,至少在iOS上,这是必须. Apple的MVC无处不在,最好以一种弱引用(和解耦)的方式来订阅全局消息.
  • 像在本地一样在每个平台上使用MVC,然后像对待手动调用每个ViewModel一样对待每个ViewModel.没有UI绑定,没有ICommand,因此请保持ViewModel的简单性.
  • 链接文件是有史以来最好的技巧.您不希望每个平台都有每个视图模型的副本,因此请确保您知道如何链接到Visual Studio和MonoDevelop中的项目内的文件.这也使#if iPhone#if Android语句成为可能.
  • Use a MVVM framework on Windows that doesn't require you to expose ICommand on every action the user takes (like Caliburn, for example), it also shouldn't require a dependency to it from within all of your ViewModels.
  • Conditionally inherit the WPF-specific pieces of your ViewModelBase class, you can do this with partial classes or an #if iPhone directive. INotifyPropertyChanged or ICommand are examples.
  • Use an IoC container, it is very helpful to abstract out things like saving settings to the filesystem which will be very different on all platforms. Also helps to sort out your dependencies as well, which is very helpful for separating out platform-specific code from non-platform specific.
  • Use a "messenger" of some kind (example here), usually included with an MVVM framework. This is a must in my opinion, at least for iOS. Apple's MVC is so all over the place, it's better to have global messages you can subscribe to in a weak referenced (and decoupled) way.
  • Use MVC on each platform like you would natively, then treat each ViewModel as you would if you were calling it manually. There are no UI bindings, no ICommand, so keep your ViewModel's simple.
  • Linking files is the best trick ever. You don't want a copy of each view model per platform, so make sure you know how to link to a file within projects in Visual Studio and MonoDevelop. This also makes #if iPhone and #if Android statements possible.

我知道您正在使用现有应用程序,因此很难.仅重用您的业务模型可能更简单,仅此而已. Android和iOS拥有自己的MVC模式,与WPF完全不同.在移动设备上,您还可能只需要每个ViewModel的子集,这样就可以更轻松地进行重写.

I know you are working with an existing application, so it's tough. It may be simpler to just reuse your business model and that's it. Android and iOS have MVC patterns of their own, and are drastically different from WPF. You might also only need a subset of each ViewModel on mobile devices, which could make it easier to just rewrite.

在我们的情况下:

  • 我们正在使用 TinyIoC (也有它自己的Messenger)
  • 在开始使用WPF时,我们将使用 Caliburn-Micro ,我们不需要Caliburn完整版中的某些功能
  • We're using TinyIoC (also has it's own messenger)
  • We will use Caliburn-Micro when we start on WPF, we don't need some of the features in full Caliburn

这篇关于在MonoDroid/MonoTouch中使用ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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