SubViewModel Constructor with runtime data as parameter using factory failed to create
See original GitHub issueI 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:
- Created 4 years ago
- Comments:14 (7 by maintainers)
Top GitHub Comments
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.
Stylet works by:
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.