ViewModelFactory

Topics: Questions
Sep 10, 2014 at 9:53 AM
Catel 4 :-)

Hope you don't kick me out

I use Catel:usercontrol with viewmodel injection:
<views:UnitView DataContext="{Binding SelectedUnit}"
public UnitViewModel(Unit unit)
{
  Unit = unit;
}
Viewmodel is not constructed:

Cateldebug message:
10:40:37:815 => [DEBUG] [Catel.MVVM.Providers.LogicBase] Using IViewModelFactory 'ConfiguratorControl.Services.CustomViewModelFactory' to instantiate the view model
10:40:37:815 => [DEBUG] [Catel.IoC.TypeFactory] Creating instance of type 'ConfiguratorControl.ViewModels.UnitViewModel' using specific parameters. No constructor found in the cache, so searching for the right one
10:40:37:816 => [DEBUG] [Catel.IoC.TypeFactory] Checking if constructor 'public ctor(Unit unit)' can be used
10:40:37:816 => [DEBUG] [Catel.IoC.TypeFactory] Constructor is not valid because parameter 'unit' cannot be resolved from the dependency resolver
10:40:37:816 => [DEBUG] [Catel.IoC.TypeFactory] The constructor is valid and can be used
10:40:37:817 => [DEBUG] [Catel.IoC.TypeFactory] No constructor could be used, cannot construct type 'ConfiguratorControl.ViewModels.UnitViewModel' with the specified parameters
10:40:37:817 => [DEBUG] [Catel.MVVM.ViewModelFactory] Could not construct view model 'ConfiguratorControl.ViewModels.UnitViewModel' using injection of data context 'null'
->
Constructor is not valid because parameter 'unit' cannot be resolved from the dependency resolver

Unit is a class generated from Entityframework. Do I have to Register a model?
Sep 16, 2014 at 11:16 PM
No need to register the model. I think this is a screw-up because you are using a custom view model factory that does not use the CreateWithPArametersAndAutoComplete as you can see in the original source.
Sep 17, 2014 at 6:51 AM
I don't create my viewmodels self. That's everything I do:
public override IViewModel CreateViewModel(Type viewModelType, object dataContext)
{
    var manager = ServiceLocator.Default.ResolveType<IViewModelManager>();
    IViewModel vm = null;

    if (viewModelType == typeof(BlockViewModel))
    {
        var model = dataContext as BlockInstance;
        vm = manager.GetViewModelsOfModel(model).SingleOrDefault();
    }

    if (viewModelType == typeof(PlaceViewModel))
    {
        var model = dataContext as PlaceInstance;
        vm = manager.GetViewModelsOfModel(model).SingleOrDefault();
    }

     ....

     if (vm != null)
        return vm;

    // Fall back to default behavior
    return base.CreateViewModel(viewModelType, dataContext);
 }