0

I'm trying out Ninject in my current Xamarin (iOS) project, which uses Storyboards to create the user interface.

The problem is that UIViewControllers are created by the framework, not by Ninject. So I'm injecting the dependencies with property-injection manually:

[Inject]
public IDependency Dependency { get; set; }

public ViewController(IntPtr handle) : base(handle)
{
    App.CurrentKernel.Inject(this);
}

But this way I'm breaking the object graph, aren't I? For example the ViewController is actually a child of a UITabBarController and I would like to use things like InCallScope ect. How can I fix that?

hendra
  • 1,403
  • 2
  • 12
  • 26
  • Who is creating `UIViewController` actually depends on your code. If you define everything in storyboard, then controllers are created by iOS runtime. Instead, you can create them manually and use for navigation. Also, if you are using MvvmCross framework, you can override process of creating controllers there. – Alex Sorokoletov Oct 05 '16 at 17:33
  • Okay, maybe it could use injection for `ViewControllers` on iOS. But in Xamarin Android it is definitely not possible to instantiate activities with `new`. So is there no workaround for that? I read something on factories but didn't get it... – hendra Oct 06 '16 at 10:47
  • Hold, on @hendra. Your question is about iOS. On Android side, have base activity class or classes and override `OnCreate(Bundle)` method and do whatever you need there. Now I feel like you really want to use just IoC with Xamarin iOS and Android. If so, take a look at [MvvmCross](https://github.com/MvvmCross/MvvmCross) – Alex Sorokoletov Oct 06 '16 at 21:06

0 Answers0