Setting up IoC with Android
Now for the IoC implementation. It works exactly the same as iOS; let's add the Android module:
public class DroidModule : IModule { public void Register(ContainerBuilder builer) { builer.RegisterType<TextToSpeechDroid> ().As<ITextToSpeech> ().SingleInstance (); } }
Easy, right?
Now we have to set up the IoC container in our MainActivity.cs
class; simply copy the iOS function in the AppDelegate
file called initIoC
and paste this into the MainActivity
class, replace the instantiation of the iOSModule
with your DroidModule
, then simply add the function call after the initialization of Xamarin.Forms
:
protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); global::Xamarin.Forms.Forms.Init (this, bundle); InitIoC (); LoadApplication (new App (...