I am trying to instance a view model from my catalog
when I use Container.GetExportedValue and then initialise the properties all instances have their properties set to the value of the final value of ‘p’. but when I use a standard initialiser they are fine.
so in my example FormViewModel’s Name property in the MEF instancing example has these values
C
C
C
but in the normal instance example has these values
A
B
C
It is behaving like there is some shared reference between all instances coming from the MEF container.
var worker = new BackgroundWorker();
worker.DoWork += (o, ea) =>
{
_forms = new ObservableCollection<FormViewModel>(
FormsExplorerRepository.GetForms()
.Select(p =>
{
// This way of instancing does strange stuff
var fvm = Container.GetExportedValue<FormViewModel>();
// This is fine but of course I'm not getting the importing constructor called
var fvm = new FormViewModel();
fvm.Workspace = this;
fvm.FormId = p.FormId;
fvm.Label = p.Label;
fvm.Name = p.Name;
fvm.Disclaimer = p.Disclaimer;
fvm.CertificationText = p.CertificationText;
fvm.Schemes = FormViewModelExtensions.InitialiseSchemes(p);
return fvm;
})
.ToList());
};
here are the view model’s constructors
public FormViewModel()
: base(null, true)
{
}
[ImportingConstructor]
public FormViewModel(
IDialogManager dialogs,
IEventAggregator events)
: base(null, true)
{
_events = events;
_events.Subscribe(this);
_dialogs = dialogs;
}
and I have an export attribute on the class definition
[Export(typeof(FormViewModel)), PartCreationPolicy(CreationPolicy.NonShared)]
public class FormViewModel
I hope there is enough info here for someone to help
I found my error
I wasn’t using the right syntax in my AddExportedValue (the one I commented out here was the wrong way)