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 aSystem.MissingMethodException
with the messageNo parameterless constructor defined for this object.
e.Uri
is/MainPage.xaml
as declared inWMAppManifest.xml
file asNavigationPage
.
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?