Created
October 2, 2017 10:56
-
-
Save BrightShadow/2e529c917fb525a01cfdb8cabee15ad8 to your computer and use it in GitHub Desktop.
Class that allows to get rid of Locator in MVVM/WPF - automatic view models resolving in runtime
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
public void ConfigureViewModels(IUnityContainer container) | |
{ | |
Assembly currentAssembly = Assembly.GetCallingAssembly(); | |
IEnumerable<Type> allAssemblyTypes = currentAssembly.GetTypes(); | |
foreach (var type in allAssemblyTypes) | |
{ | |
if (this.ViewModelNamesOnly(type) != null) | |
{ | |
Type viewType = this.GetMatchingViewType(type); | |
if (viewType != null) | |
{ | |
this.AddDataTemplate(viewType, type); | |
} | |
Type interfaceType = this.GetMatchingInterfaceType(type); | |
if (interfaceType != null) | |
{ | |
container.RegisterType(interfaceType, type); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment