Friday, January 04, 2013

Overriding the view lookup convention used in MvvmCross

Following on from a question on StackOverflow -

If ever you want to override the default viewmodel-view lookup setup, then you can do so by overriding the GetViewModelViewLookup method in your Setup class.

The default lookup table is generated by:
  • enumerating all the Types in the Setup.cs Assembly
  • removing any which don't implement IMvx-platform-View
  • removing any which are abstract
  • removing any which start/end with the word Base
  • removing any which are marked with the Unconventional attribute
  • removing any which are marked with the ConditionalConventional attribute and who's condition fails (this is used for e.g. iPad vs iPhone selection - see Sharing ViewModels for different Views)
  • finding the ViewModel property on each remaining Type
  • removing any who's ViewModel PropertyType starts/ends with the word Base
  • removing any who's ViewModel PropertyType is an interface
  • listing the remaining {ViewModel PropertyType, View Type} as a Dictionary lookup.

If you wish to replace this algorithm, you can - just override GetViewModelViewLookup

For example, at a trivial level, you could just hard code the ViewModel-View list:

This hard-coded list is actually what the very first versions of MvvmCross did - and it does have a small performance advantage - it makes app startup a bit quicker.

For a slightly more complicated example, you could generate an alternative list based on several assemblies, and could then supplement that list with some conditional logic:

No comments:

Post a Comment