Thank you for your answer!
>>>The first thing is that MEF does support passing data via constructor.
>>>You can use ImportingConstrcutor attribute on the constructor on your ViewModel.
Correct me if I'm wrong, but when you define ImportingConstructor with parameters, it's a job of constructor to resolve this parameters. For shared parts it will get the instance of singleton for non-shared it will try to create a new instance. And that
is not what I need.
I tried the route you suggested. If I understand you right my ViewModelFactory should look something like:
public class ViewModelFactory
public static ViewModel CreateViewModel(object data1, object data2)
var result = new ViewModel(data1, data2);
The problem I'm having with this approach is that it works first time but for some reason when my application handles the second group of viewModels the ComposeParts line throws composition exception:
1) Cannot create an instance of type '[MyViewModelType]' because a constructor could not be selected for construction. Ensure that the type either has a default constructor, or a single constructor marked with the 'System.ComponentModel.Composition.ImportingConstructorAttribute'.
I don't understand why MEF is looking for constructor - I'm trying to Compose already existent instance.
I guess MEF cannot satisfy one of the imports required by my ViewModel. But again this only happened second time I create the instance of my ViewModel.
Therefore my questions are:
- Is my code correct?
- Do I need to clean up after the call to ComposeParts so next time it will work with the fresh instance?
- Yes please, some code sample would be very helpful.