Skip to content

Instantly share code, notes, and snippets.

@BrightShadow
Created October 2, 2017 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrightShadow/2e529c917fb525a01cfdb8cabee15ad8 to your computer and use it in GitHub Desktop.
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
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