This example illustrates how to register a POCO View Model in a various Dependency Injection containers.
To bind a View to a ViewModel, create a MarkupExtension that resolves the correct ViewModel type, as shown in the DISource class. Register the resolver at the application startup:
protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
Container = BuildUpContainer();
DISource.Resolver = Resolve;
}
object Resolve(Type type, object key, string name) {
if(type == null)
return null;
if(key != null)
return Container.ResolveKeyed(key, type);
if(name != null)
return Container.ResolveNamed(name, type);
return Container.Resolve(type);
}
Specify the DataContext in XAML in the following manner:
DataContext="{common:DISource Type=common:MainViewModel}"
To use a POCO View Model in a DI container, use the ViewModelSource.GetPOCOType
method to register the POCO type generated in runtime:
Container.RegisterType(typeof(MainViewModel), ViewModelSource.GetPOCOType(typeof(MainViewModel)));