FreshMVVM: No default constructor exception on PageModel constructor injection

BardiaNoohi.8900BardiaNoohi.8900 USMember, University
edited October 2015 in Xamarin.Forms

Hey there, getting a missing method exception when trying to create a new page using FreshMVVM.

System.MissingMethodException: Default constructor not found for type InContext.Kemosabe.UI.ViewModels.ProjectSelectionPageModel

This is the PageModel:

public class ProjectSelectionPageModel : FreshBasePageModel
{
    private readonly IProjectService _projectService;
    public ProjectSelectionPageModel(IProjectService projectService)
    {
        _projectService = projectService;
    }
}

The IProjectService is registered on App start and the code works if I add this constructor to ProjectSelectionPageModel:
public ProjectSelectionPageModel() : this(FreshIOC.Container.Resolve<IProjectService>()) { }

So that is does the resolution in the construction. However, I want to do constructor injection and FreshMVVM claims to handle this.

As a note, the page is being created by this call: FreshPageModelResolver.ResolvePageModel<ProjectSelectionPageModel>(clientId);
Though the same issue is seen with the PushPageModel call.`

What am I missing?

Answers

  • CodeRinseCodeRinse USMember

    What does the code look like in the App start? Like this?

    FreshIOC.Container.Register<IProjectService, ProjectService> ();

    Samples from Michael Ridland that may help:

    https://github.com/rid00z/FreshMvvm/blob/master/samples/FreshMvvmSampleApp/FreshMvvmSampleApp/FreshMvvmSampleApp.cs

    https://github.com/rid00z/FreshMvvm/blob/master/samples/FreshMvvmSampleApp/FreshMvvmSampleApp/PageModels/ContactListPageModel.cs

  • BardiaNoohi.8900BardiaNoohi.8900 USMember, University

    Just went through the samples again. My project follows the style of the sample. Not sure why it's not working.

  • BardiaNoohi.8900BardiaNoohi.8900 USMember, University

    It looks like the resolution might be happening on the FreshMVVM side, however in the InitializeComponent call on the Page it calls this.LoadFromXaml(typeof(ProjectPage));

    The call attempts to create the type and call the default constructor, which doesn't exist.

  • MichaelRidlandMichaelRidland AUInsider, University ✭✭✭

    Hi @BardiaNoohi.8900

    Constructor injection definately works, I'm not sure what's happened in this case.

    Is it something to do with the Xamarin.Forms Page creation?

    Thanks

  • BardiaNoohi.8900BardiaNoohi.8900 USMember, University
    edited November 2015

    I think it does have to do with the page creation. It's failing when the page's InitializeComponent() call is made and that tries to instantiate the viewmodel itself with a default constructor.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    InitializeComponent does not by itself create view models. It loads XAML and hooks up backing fields. It doesn't create the Page instance either because the Page instance already exists by then (that's the object that has InitializeComponent in its constructor). So this isn't making sense. What does your XAML look like, and what is your call stack?

  • ArieKanarieArieKanarie USMember

    I'm also having this issue.
    The registering and resolving of the service works because I see the constructor of the (empty) service being called, and the field in the pagemodel get's set with the instance of the service.
    But after that I get an exception "Default constructor not found for type...". It doesn't matter if I run it on Android or iOS emulator.

    Nevermind, there was an error in the XAML, some leftovers from a test. Someone added
    <ContentPage.BindingContext> <ViewModels:LoginPageModel/> </ContentPage.BindingContext>
    That makes sense why it was trying to create another instance of the LoginPageModel

  • ArieKanarieArieKanarie USMember

    I'm also having this issue.
    The registering and resolving of the service works because I see the constructor of the (empty) service being called, and the field in the pagemodel get's set with the instance of the service.
    But after that I get an exception "Default constructor not found for type...". It doesn't matter if I run it on Android or iOS emulator. I've used FreshMVVM before and it worked great. What went wrong with this project?

    The full stacktrace:
    FreshTinyIoC.TinyIoCResolutionException: Resolve failed: LoginPage - Reason: Default constructor not found for type DV.F4RMobile.App.PageModels.LoginPageModel ---> System.MissingMethodException: Default constructor not found for type DV.F4RMobile.App.PageModels.LoginPageModel at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x00085] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:171 at System.RuntimeType.CreateInstanceSlow (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x0001a] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:153 at System.RuntimeType.CreateInstanceDefaultCtor (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x0002a] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/rttype.cs:5638 at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00040] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/activator.cs:201 at System.Activator.CreateInstance (System.Type type) [0x00000] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/activator.cs:134 at Xamarin.Forms.Xaml.CreateValuesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, Xamarin.Forms.Xaml.INode parentNode) [0x0021f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\CreateValuesVisitor.cs:97 at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x000b1] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:176 at Xamarin.Forms.Xaml.RootNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00054] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:224 at Xamarin.Forms.Xaml.XamlLoader.Visit (Xamarin.Forms.Xaml.RootNode rootnode, Xamarin.Forms.Xaml.HydratationContext visitorContext) [0x00053] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:122 at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.String xaml) [0x00046] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:76 at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Type callingType) [0x0002f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:56 at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (TXaml view, System.Type callingType) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ViewExtensions.cs:36 at DV.F4RMobile.App.Pages.LoginPage.InitializeComponent () [0x00001] in C:\Source\F4RMobileNet\F4RMobile App\DV.F4RMobile.App\DV.F4RMobile.App\obj\Debug\DV.F4RMobile.App.Pages.LoginPage.xaml.g.cs:21 at DV.F4RMobile.App.Pages.LoginPage..ctor () [0x00008] in C:\Source\F4RMobileNet\F4RMobile App\DV.F4RMobile.App\DV.F4RMobile.App\Pages\LoginPage.xaml.cs:9 at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&) at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:661 --- End of inner exception stack trace --- at FreshTinyIoC.FreshTinyIoCContainer.ConstructType (System.Type requestedType, System.Type implementationType, System.Reflection.ConstructorInfo constructor, FreshTinyIoC.NamedParameterOverloads parameters, FreshTinyIoC.ResolveOptions options) [0x0018e] in <2c9607ea566c42259e3dd2ee50f95a71>:0 at FreshTinyIoC.FreshTinyIoCContainer.ConstructType (System.Type requestedType, System.Type implementationType, FreshTinyIoC.NamedParameterOverloads parameters, FreshTinyIoC.ResolveOptions options) [0x00000] in <2c9607ea566c42259e3dd2ee50f95a71>:0 at FreshTinyIoC.FreshTinyIoCContainer.ResolveInternal (FreshTinyIoC.FreshTinyIoCContainer+TypeRegistration registration, FreshTinyIoC.NamedParameterOverloads parameters, FreshTinyIoC.ResolveOptions options) [0x001eb] in <2c9607ea566c42259e3dd2ee50f95a71>:0 at FreshTinyIoC.FreshTinyIoCContainer.Resolve (System.Type resolveType) [0x00011] in <2c9607ea566c42259e3dd2ee50f95a71>:0 at FreshMvvm.FreshTinyIOCBuiltIn.Resolve (System.Type resolveType) [0x00000] in /Users/michaelridland/Projects/FreshMvvm/src/FreshMvvm/FreshTinyIOCBuiltIn.cs:48 at FreshMvvm.FreshPageModelResolver.ResolvePageModel (System.Type type, System.Object data, FreshMvvm.FreshBasePageModel pageModel) [0x0002a] in /Users/michaelridland/Projects/FreshMvvm/src/FreshMvvm/FreshPageModelResolver.cs:41 at FreshMvvm.FreshPageModelResolver.ResolvePageModel[T] (System.Object data, T pageModel) [0x0000e] in /Users/michaelridland/Projects/FreshMvvm/src/FreshMvvm/FreshPageModelResolver.cs:25 at FreshMvvm.FreshPageModelResolver.ResolvePageModel[T] (System.Object initData) [0x0000b] in /Users/michaelridland/Projects/FreshMvvm/src/FreshMvvm/FreshPageModelResolver.cs:19 at FreshMvvm.FreshPageModelResolver.ResolvePageModel[T] () [0x00000] in /Users/michaelridland/Projects/FreshMvvm/src/FreshMvvm/FreshPageModelResolver.cs:12 at DV.F4RMobile.App.App..ctor () [0x00029] in C:\Source\F4RMobileNet\F4RMobile App\DV.F4RMobile.App\DV.F4RMobile.App\App.xaml.cs:29

  • ArieKanarieArieKanarie USMember

    I'm also having this issue.
    The registering and resolving of the service works because I see the constructor of the (empty) service being called, and the field in the pagemodel get's set with the instance of the service.
    But after that I get an exception "Default constructor not found for type...". It doesn't matter if I run it on Android or iOS emulator. I've used FreshMVVM before and it worked great. What went wrong with this project?

    The full stacktrace:
    FreshTinyIoC.TinyIoCResolutionException: Resolve failed: LoginPage - Reason: Default constructor not found for type DV.F4RMobile.App.PageModels.LoginPageModel ---> System.MissingMethodException: Default constructor not found for type DV.F4RMobile.App.PageModels.LoginPageModel at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x00085] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:171 at System.RuntimeType.CreateInstanceSlow (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x0001a] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:153 at System.RuntimeType.CreateInstanceDefaultCtor (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x0002a] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/rttype.cs:5638 at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00040] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/activator.cs:201 at System.Activator.CreateInstance (System.Type type) [0x00000] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/activator.cs:134 at Xamarin.Forms.Xaml.CreateValuesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, Xamarin.Forms.Xaml.INode parentNode) [0x0021f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\CreateValuesVisitor.cs:97 at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x000b1] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:176 at Xamarin.Forms.Xaml.RootNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00054] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:224 at Xamarin.Forms.Xaml.XamlLoader.Visit (Xamarin.Forms.Xaml.RootNode rootnode, Xamarin.Forms.Xaml.HydratationContext visitorContext) [0x00053] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:122 at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.String xaml) [0x00046] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:76 at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Type callingType) [0x0002f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:56 at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (TXaml view, System.Type callingType) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ViewExtensions.cs:36 at DV.F4RMobile.App.Pages.LoginPage.InitializeComponent () [0x00001] in C:\Source\F4RMobileNet\F4RMobile App\DV.F4RMobile.App\DV.F4RMobile.App\obj\Debug\DV.F4RMobile.App.Pages.LoginPage.xaml.g.cs:21 at DV.F4RMobile.App.Pages.LoginPage..ctor () [0x00008] in C:\Source\F4RMobileNet\F4RMobile App\DV.F4RMobile.App\DV.F4RMobile.App\Pages\LoginPage.xaml.cs:9 at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&) at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:661 --- End of inner exception stack trace --- at FreshTinyIoC.FreshTinyIoCContainer.ConstructType (System.Type requestedType, System.Type implementationType, System.Reflection.ConstructorInfo constructor, FreshTinyIoC.NamedParameterOverloads parameters, FreshTinyIoC.ResolveOptions options) [0x0018e] in <2c9607ea566c42259e3dd2ee50f95a71>:0 at FreshTinyIoC.FreshTinyIoCContainer.ConstructType (System.Type requestedType, System.Type implementationType, FreshTinyIoC.NamedParameterOverloads parameters, FreshTinyIoC.ResolveOptions options) [0x00000] in <2c9607ea566c42259e3dd2ee50f95a71>:0 at FreshTinyIoC.FreshTinyIoCContainer.ResolveInternal (FreshTinyIoC.FreshTinyIoCContainer+TypeRegistration registration, FreshTinyIoC.NamedParameterOverloads parameters, FreshTinyIoC.ResolveOptions options) [0x001eb] in <2c9607ea566c42259e3dd2ee50f95a71>:0 at FreshTinyIoC.FreshTinyIoCContainer.Resolve (System.Type resolveType) [0x00011] in <2c9607ea566c42259e3dd2ee50f95a71>:0 at FreshMvvm.FreshTinyIOCBuiltIn.Resolve (System.Type resolveType) [0x00000] in /Users/michaelridland/Projects/FreshMvvm/src/FreshMvvm/FreshTinyIOCBuiltIn.cs:48 at FreshMvvm.FreshPageModelResolver.ResolvePageModel (System.Type type, System.Object data, FreshMvvm.FreshBasePageModel pageModel) [0x0002a] in /Users/michaelridland/Projects/FreshMvvm/src/FreshMvvm/FreshPageModelResolver.cs:41 at FreshMvvm.FreshPageModelResolver.ResolvePageModel[T] (System.Object data, T pageModel) [0x0000e] in /Users/michaelridland/Projects/FreshMvvm/src/FreshMvvm/FreshPageModelResolver.cs:25 at FreshMvvm.FreshPageModelResolver.ResolvePageModel[T] (System.Object initData) [0x0000b] in /Users/michaelridland/Projects/FreshMvvm/src/FreshMvvm/FreshPageModelResolver.cs:19 at FreshMvvm.FreshPageModelResolver.ResolvePageModel[T] () [0x00000] in /Users/michaelridland/Projects/FreshMvvm/src/FreshMvvm/FreshPageModelResolver.cs:12 at DV.F4RMobile.App.App..ctor () [0x00029] in C:\Source\F4RMobileNet\F4RMobile App\DV.F4RMobile.App\DV.F4RMobile.App\App.xaml.cs:29

Sign In or Register to comment.