Last active
August 14, 2016 05:52
-
-
Save nuitsjp/55b5964bfdf92599909aed03e0f31924 to your computer and use it in GitHub Desktop.
【Xamarin】Prism.Forms入門 ViewModelLocator
This file contains 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 void ConfigureViewModelLocator() | |
{ | |
base.ConfigureViewModelLocator(); // <- 必須!DIされなくなります。 | |
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver( | |
viewType => | |
{ | |
var viewName = viewType.FullName; | |
viewName = viewName.Replace(".Views.", ".ViewModels."); | |
string viewModelName; | |
if (viewName.EndsWith("Page")) | |
{ | |
viewModelName = $"{viewName.Substring(0, viewName.LastIndexOf("Page", StringComparison.Ordinal))}ViewModel"; | |
} | |
else | |
{ | |
var suffix = viewName.EndsWith("View") ? "Model" : "ViewModel"; | |
viewModelName = $"{viewName}{suffix}"; | |
} | |
var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName; | |
return Type.GetType(string.Format(CultureInfo.InvariantCulture, "{0}, {1}", viewModelName, viewAssemblyName)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment