Just posted this in
https://github.com/MvvmCross/MvvmCross/wiki/Service-Location-and-Inversion-of-Control#wiki-what-if-i-want-to-use-property-injection-as-an-ioc-mechanism
-----
From v3.1 of MvvmCross, Property Injection is supported in the default IoC container.
To enable this injection, you need to change your app
Setup
so that it overrides
CreateIocOptions()
There are currently two ways you inject into properties:
- inject only into marked interface properties -
MvxInjectInterfaceProperties
- inject into all interface properties -
AllInterfaceProperties
In both cases, MvvmCross will perform the property injection as soon as construction is completed.
One further option is that the MvvmCross
IMvxPropertyInjector
can be used independently - you can choose to use this on your options if you want to.
MvxInjectInterfaceProperties
If you override options as:
protected override IMvxIoCOptions CreateIocOptions()
{
return new MvxIocOptions()
{
PropertyInjectorOptions = new MvxPropertyInjectorOptions.MvxInject
};
}
then this will enable injection into
public
writeable properties which are declared as interfaces and which have an
MvxInject
attribute - e.g.
Foo
below:
public class MyViewModel : MvxViewModel
{
[MvxInject]
public IFooService Foo { get; set; }
}
AllInterfaceProperties
If you override options as:
protected override IMvxIoCOptions CreateIocOptions()
{
return new MvxIocOptions()
{
PropertyInjectorOptions = MvxPropertyInjectorOptions.All
};
}
then this will enable injection into
public
writeable properties which are declared as interfaces - e.g.
Bar
below:
public class MyViewModel : MvxViewModel
{
public IBarService Bar { get; set; }
}
Using IMvxPropertyInjector
directly
You can inject into your own objects independently of the MvvmCross IoC Container.
To do this, you can use:
var injector = new MvxPropertyInjector()
or
var injector = Mvx.Resolve();
and then:
var foo = new Foo();
injector.Inject(foo, MvxPropertyInjectorOptions.MvxInject);
or:
var bar = new Bar();
injector.Inject(bar, MvxPropertyInjectorOptions.All);