1

I tried constructor injection this way:

public partial class MainPage : PhoneApplicationPage
{
    private readonly ISomeService service;

    public MainPage(ISomeService service)
    {
        InitializeComponent();

        this.service = service;
    }
}

In the App.xaml.cs constructor:

public App()
{
    // Global handler for uncaught exceptions.
    UnhandledException += Application_UnhandledException;

    DependencyService.Initialize(GetAssemblyFilePathCollection());

    DependencyService
        .Container
        .Register(typeof(MainPage), made: Made.Of(() => new MainPage(Arg.Of<ISomeService>())));

    // The remaining part is omitted.
}

DependencyService is my custom static class that creates the Container and loads the bindings from all the assemblies.

In this case App.RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e) method is always executed:

  • e.Exception is a System.MissingMethodException with the message No parameterless constructor defined for this object.
  • e.Uri is /MainPage.xaml as declared in WMAppManifest.xml file as NavigationPage.

Inside the MainPage constructor I can resolve the service successfully using the container directly but I want to use constructor injection.

I have not found any specific example for this. Anybody any idea how does it work?

Gabor
  • 3,021
  • 1
  • 11
  • 20
  • Did you register `ISomeService` dependency? – dadhi Dec 04 '16 at 19:09
  • Yes and inside `MainPage` ctor I can resolve the `ISomeService` dependency successfully using the `Container` directly: `ISomeService service = DependencyService.Container.Resolve();` works well. – Gabor Dec 04 '16 at 20:04
  • How can I tell the WP navigation framework to use the container to resolve dependencies automatically? Maybe I missed some configuration. – Gabor Dec 04 '16 at 20:08
  • The problem is that original exception is masked by infrastructure callback. Dumb question then, does MainPage have a paramerless ctor? Also did you try `made: FactoryMethod.ConstructorWithResolvableArguments`? – dadhi Dec 04 '16 at 20:09
  • Sorry, I won't be able to help with WinPhone specifics :(. Btw, are you using some Mvvm integration package, like Prism.DryIoc.Forms? – dadhi Dec 04 '16 at 20:12
  • I tried two scenarios: 1) one and only parameterless ctor for MainPage + resolve dependency inside the ctor; 2) one and only ctor with the resolvable dependency; Only the 1st scenario worked. – Gabor Dec 04 '16 at 20:27
  • Currently I don't use any mvvm integration package. Earlier I used `GalaSoft.MvvmLight` package but I could not use ctor injection with that package. My app is not a Xamarin.Forms app but a standard WP8 Silverlight app. – Gabor Dec 04 '16 at 20:30
  • This is not related to dependency resolution at all. The XAML infrastructure wants the object to have a parameterless constructor, and there is no way to tell it to use a custom instantiation logic. As a hack, you could use property injection and then `InjectPropertiesAndFields` in your `Loaded` event, but I wouldn't recommend it: you shouldn't mix UI and application logic. Your UI components shouldn't have dependencies. – Fyodor Soikin Dec 05 '16 at 13:08

0 Answers0