Following on from a question on StackOverflow - http://stackoverflow.com/questions/14147614/mvvmcross-navigation-issue-getting-exception-could-not-find-view-for-viewmode
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override IDictionary<Type, Type> GetViewModelViewLookup() | |
{ | |
return new Dictionary<Type,Type>() | |
{ | |
{ typeof(LoginViewModel), typeof(LoginActivity) }, | |
{ typeof(HomeViewModel), typeof(HomeActivity) } , | |
{ typeof(DetailViewModel), typeof(DetailActivity) }, | |
{ typeof(UploadViewModel), typeof(UploadActivity) } | |
}; | |
} |
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Assembly> MyViewAssemblies { get; set; } | |
protected override IDictionary<System.Type, System.Type> GetViewModelViewLookup() | |
{ | |
var toReturn = new Dictionary<Type, Type>(); | |
foreach (var assembly in MyViewAssemblies) | |
{ | |
var contributions = base.GetViewModelViewLookup(assembly, typeof (IMvxAndroidView)); | |
foreach (var kvp in contributions) | |
{ | |
toReturn[kvp.Key] = kvp.Value; | |
} | |
} | |
if (SomeCondition) | |
{ | |
toReturn[typeof(SpecialViewModel)] = typeof(SpecialView1); | |
} | |
else | |
{ | |
toReturn[typeof(SpecialViewModel)] = typeof(SpecialView2); | |
toReturn[typeof(AnotherSpecialViewModel)] = typeof(AnotherSpecialView3); | |
} | |
return toReturn; | |
} |
No comments:
Post a Comment