The Mvx IoC container
MVVMCross comes with its very own IoC container. It works exactly like our previous example with Autofac, but we are not going to be using modules. Let's begin by registering our sound handler implementation; open our AppDelegate.cs
file and create a new private function called setupIoC
:
private void SetupIoC() { Mvx.RegisterType<ISoundHandler, SoundHandler>(); }
We must also register our view-models so we can retrieve registered interfaces within our view-model's constructor. Let's add a new folder called IoC
inside our AudioPlayer.Portable
project. Add a new file called PortableMvxIoCRegistrations.cs
and implement the following:
public static class PortableMvxIoCRegistrations { public static void InitIoC() { Mvx.IocConstruct<MainPageViewModel>(); Mvx.IocConstruct<AudioPlayerPageViewModel>(); } }
Now we must call the static function InitIoC
from the AppDelegate
function SetupIoC...