如何导航到扩展抽象类的ViewModel? [英] How to navigate to a ViewModel that extends an abstract class?

查看:93
本文介绍了如何导航到扩展抽象类的ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MVVMCross建议的程序集架构来尽可能多地共享视图模型:

I am trying to share viewmodels as much as possible using MVVMCross's suggested assembly architecture:

MyApp.App
└ ViewModels
  ├ GlobalViewModel1
  └ GlobalViewModel2 (abstract)

MyApp.Droid
├ Views
│ ├ View1
│ └ View2
└ ViewModels
  └ ChildOfGlobalViewModel2

MyApp.Store
├ Views
│ ├ View1
│ └ View2
└ ViewModels
  └ ChildOfGlobalViewModel2

我可以正确导航到View1及其关联的视图模型GlobalViewModel1,但是当我要导航到View2时,它变得更加复杂,因为它的视图模型在一个平台与另一个平台之间略有不同.

I can correctly navigate to View1 and its associated viewmodel GlobalViewModel1, but it gets more complicated when I want to navigate to View2 because its viewmodel varies slightly from one platform to another.

我假设,如果我在每个平台上注册了Mvx.RegisterType<GlobalViewModel2, ChildOfGlobalViewModel2>(),那么GlobalViewModel1何时将能够使用this.ShowViewModel<GlobalViewModel2>()进行导航.显然我错了,因为当应用程序尝试实例化抽象类时,我得到了一个异常.

I assumed that if I registered Mvx.RegisterType<GlobalViewModel2, ChildOfGlobalViewModel2>() on each platform, then when GlobalViewModel1 would be able to navigate by using this.ShowViewModel<GlobalViewModel2>(). Apparently I was wrong, since I get an exception when the app tries to instantiate the abstract class.

我确实通过覆盖InitializeViewLookup来手动查看查找初始化,因此我尝试将视图与父类和子类相关联,但是这些配置均未成功.

I do view look up initialization manually by overriding InitializeViewLookup, so I tried associating the view to the parent and the child classes, but none of those configurations was successful.

我已经通过注入所需的行为解决了该问题,因为它与将其封装在一个类中有关,但是在某些情况下,这种方法只会迫使我创建多个类,这些类将使代码的可读性降低并且应用程序将来很难维护.

I already solved the problem once by injecting the desired behaviour because it was pertinent to encapsulate it in a class, but in some cases this approach would only force me to create a number of classes that will make the code less readable and the apps harder to maintain in the future.

我是否缺少某些配置,还是应该更改方法?如果是这样,推荐的方法是什么?

Is there some configuration I am missing or should I change my approach? If so, what is the recommended approach?

推荐答案

MvvmCross中的默认设置是通过viewmodel导航-这不包括通过继承进行导航.

The default setup in MvvmCross is navigation by viewmodel - this doesn't include navigation by inheritance.

如果您确实愿意,可以覆盖mvx的某些部分以支持您的抽象方案.但是,我认为这会付出更多的努力.

You could override parts of mvx in order to support your abstract scenario if you really wanted to. However, I think this would be more effort than it is worth.

相反,我建议:

  • 注册具有单个属性G2Type的服务IG2Service.然后,每个平台都可以set您的Global2的VM类型,然后您的导航可以在执行导航之前调用get.注意:使用此技术,您将无法使用ShowViewModel<T>替代方法进行这种导航,但是非通用替代方法应该可以正常工作.
  • 或(如果可能)重构代码,以便您的g2视图模型通过依赖注入而不是通过继承使用平台特定的代码.
  • registering a service IG2Service which has a single property called G2Type. Each platform could then set the VM Type for your Global2 and your navigation could then call the get before performing the navigation. Note: using this technique, you won't be able to use the ShowViewModel<T> overrides to navigate this way, but the non-generic overrides should work fine.
  • or (if possible) refactoring your code so that your g2 view models use platform specific code via dependency injection rather than via inheritance.

这篇关于如何导航到扩展抽象类的ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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