How to add LightInject to your Xamarin project

Hello everyone, I thought of writing a short and sweet, quick post of using LightInject NuGet in your Xamarin project for Dependency Injection mechanism. In this post, I will cover how LightInject include to your project and what are the codes that you can remove from your ordinary development which I have explained in one of my previous post.

Let’s begin straight forward. At the end of this post, what you can see is a simple application with two views that can navigate forward and backwards. You have to create the project the same way that describe in the post that I have linked in the first paragraph, but do not add any NuGet. The reason is, in that post we have added the MvvmLight first and then MvvmLightLibs NuGet. But here, what we need is only MvvmLightLibs NuGet from those two. Additionally we need to include the LightInject NeGet as well.

Adding LightInject NuGet
Adding LightInject NuGet

If you compare the two projects carefully, you will be able to see that ViewModelLocator class is no longer available. Because of that, we have no modification inside Main.cs file. Following is the way that my Main.storyboard looks like.


Our important code reside inside AppDelegate.cs file. Below is that code snippet.

        private static ServiceContainer serviceContainer;
        public static NavigationService navigationService;

        public override UIWindow Window

        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
            serviceContainer = new ServiceContainer();

            serviceContainer.Register<INavigationService, NavigationService>();
            navigationService = serviceContainer.GetInstance<INavigationService>() as NavigationService;

            return true;

Once you write that code, what you have to do is, write the touch up inside event to the button in the first view controller. I’ve added inside the ViewDidLoad method in that particular ViewController like below.

        public override void ViewDidLoad()

            NavigateButton.TouchUpInside += (object sender, EventArgs e) => 

Yes, that’s all you wanted to do to run a small application with the help of LightInject. But LightInject has several features that we can get use. You better read the original document for that. In any case, if you need the source, it will be uploaded to GitLab.

Before ending the post, I would like to give you brief explanation of how I understand the Dependency Injection. As the basic understand, just twist the words and keep remember. Which means Injecting Dependencies. Now we have to understand, what are Dependencies. Dependency is another class that your class needs to functional. As an example, if your model class fetches data from a database object, which means your model class is depend on that database class. Next we will understand, what the meaning or Inject Dependencies. That means, your dependency is pushed in to your class from the outside. In another words, you do not need to create new instances from new operator of the dependent class from your class. Instead you can reference it using the constructor parameter.

See you in another short post 🙂


About AnujAroshA

Working as a Technical Lead. Specialized in iOS application development. A simple person :)
This entry was posted in iOS, LightInject, Xamarin and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s