question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SubViewModel Constructor with runtime data as parameter using factory failed to create

See original GitHub issue

I got stuck to display a subview. It may be something simple.

My main viewmodel looks like this:

public class MainViewModel : Screen
    {
        public PerClassViewModel perclassViewModel { get; set; }

        public MainViewModel(PerClassViewModelFactory perClassViewModelFactory)
        {
            int appid = Properties.Settings.Default.DefaultAppId;
            perclassViewModel = perClassViewModelFactory.GetPerClassViewModel(appid);
        }
}

the perClassViewModel is a subviewmodel. It has an integer as a parameter plus a service. So I used a factory for the injection.

I stepped the code. The mainviewmodel subviewmodel property is properly initiated in the constructor. But the following error shows when the code reaches the mainview.

StyletIoC.StyletIoCFindConstructorException: 'Unable to find a constructor for type PerClassViewModel which we can call: Constructor: IDataAccessServices: Success int: Failure

It looks like it dis-regarded the subviewmodel that has already been created in the constructor, and trying to create a new subviewmodel from the container and fail to find the runtime data to inject.

I do not know how to fix this.

My subviewmodel constructor looks like: public PerClassViewModel(IDataAccessServices dataAccessServices, int defaultAppId)

My bootstrapper looks like this:

public class BingBingFrontBootstrapper : Bootstrapper<MainViewModel>
    {
        protected override void ConfigureIoC(StyletIoC.IStyletIoCBuilder builder)
        {
            base.ConfigureIoC(builder);

            builder.Bind<IDataAccessServices>().To<BBFDataAccessServices>().InSingletonScope();
            builder.Bind<PerClassViewModelFactory>().ToSelf();
        }       
        
    }

It seems should work. I appreciate your help. Thanks.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
fengfranklucommented, Feb 17, 2020

Thanks!!! Make sense! Oh. I forgot to clean the code behind for the subview. Thanks!!! I am trying to convert the app to your framework. Thanks a million for the great framework.

0reactions
canton7commented, Feb 17, 2020

Stylet works by:

  1. You create the VM (possibly using the IoC container)
  2. Stylet creates the View
  3. Stylet binds the View to the VM (by setting the VM as the View’s DataContext, and setting the View as the VM’s View property.

Stylet is already taking care of setting the DataContext. You don’t need to do it yourself.

Under VM-first MVVM, your views should not have any direct knowledge of your VMs. Your views should definitely not take VM’s as constructor parameters (they should only ever have parameterless constructors). Please see the documentation and samples.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot create an instance of class ViewModel
My constructor is public, but I received this error: Caused by java.lang.RuntimeException: Cannot create an instance of class com.randa.android.
Read more >
Create ViewModels with dependencies
If a ViewModel class receives dependencies in its constructor, provide a factory that implements the ViewModelProvider.Factory interface.
Read more >
Is it bad to use DI to inject constructor parameters at runtime?
If a property meets the following criteria then I make it a constructor parameter: The class is dependent on it for operation; It...
Read more >
Replace Constructor with Factory Method
Problem: You have a complex constructor that does something more than just setting parameter values in object fields. Solution: Create a factory method...
Read more >
[Solved]-Handling lots of commands in WPF MVVM-wpf
This way you simply create and add all the commands to the Commands collection in the ViewModel constructor or even from a factory...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found