I am a new happy Xamarin developer and unfortunately I got stuck on my first project problem. When I was working with MVC I used to work with Ninject. So, I decided to use this tool as my IoC and DI in Xamarin project as well. My solution contains IOS project, Android project and PCL for shared datas. In my PCL project I created my NinjectModule (very simple implementation so far..:) )
public class NinjectModuleImplementation : NinjectModule
{
public override void Load()
{
this.Bind<IMapPoint>().To<MapPoint>();
}
}
And the other static class where I create my container:
public static class Startup
{
public static StandardKernel Container { get; set; }
public static void BuildContainer()
{
var kernel = new Ninject.StandardKernel(new NinjectModuleImplementation());
Startup.Container = kernel;
}
}
In my native project I call Startup.BuildContainer();
Android:
[Application]
public class App : Application
{
public App(IntPtr h, JniHandleOwnership jho) : base(h, jho)
{
}
public override void OnCreate()
{
Startup.BuildContainer();
}
}
And iOS
[Register ("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
public override UIWindow Window {
get;
set;
}
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
Startup.BuildContainer();
return true;
}
Now, when I was trying resolve my dependencies "explicitly", there is no problem - it works.
IMapPoint point = Startup.Container.Get<IMapPoint>();
However, when I try inject my dependence by constructor - like that :
public class SomeClass
{
public static SomeClass Instance { get; private set; }
public IMapPoint point;
public SomeClass(IMapPoint _point)
{
Instance = this;
point = _point;
}
}
NullReferenceException is thrown... What am I doing wrong ? I will be grateful for any suggestions :)
Regards,
Mariusz