Skip to content

DevExpress-Examples/wpf-mvvm-framework-register-poco-type-in-dependency-injection-container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Register a POCO View Model in a Dependency Injection Container

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)));

Files to Look At

Documentation

Does This Example Address Your Development Requirements/Objectives?

(you will be redirected to DevExpress.com to submit your response)

About

This example illustrates how to register a POCO View Model in a various Dependency Injection containers

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages