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

查看:17
本文介绍了在 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 上使用 MVVM 框架,该框架不需要您对用户执行的每个操作公开 ICommand(例如 Caliburn,例如),它也不应该要求您的所有 ViewModel 都依赖它.
  • 有条件地继承 ViewModelBase 类的 WPF 特定部分,您可以使用部分类或 #if iPhone 指令来实现.INotifyPropertyChanged 或 ICommand 是示例.
  • 使用 IoC 容器,它非常有助于抽象出诸如将设置保存到文件系统之类的内容,这些内容在所有平台上都非常不同.还有助于理清您的依赖项,这对于将特定于平台的代码与非特定于平台的代码区分开来非常有帮助.
  • 使用某种信使"(此处的示例),通常包含在MVVM 框架.我认为这是必须,至少对于 iOS 是这样.Apple 的 MVC 无处不在,最好拥有可以以弱引用(和解耦)方式订阅的全局消息.
  • 像在本机一样在每个平台上使用 MVC,然后像手动调用它一样对待每个 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(也有自己的信使)
  • 当我们开始使用 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天全站免登陆