I am currently developing a Silverlight/C# application with the use of MEF.
Currently I create a new ViewModel instance and ChildWindow through the GetExport method of MEF. Then I assign the ViewModel DataContext to the Context of ChildWindow. Then I open another ChildWindow and I want to get the instance of the previous ViewModel via GetExportedValue. I retreive an instance of the model but the properties have the default values or values assigned in the constructor.
It looks like Selection Change from the User Interface doesn't affect the property (of the ViewModel)
I think this is a problem of threading, because the Model is created in the main program thread and the UI part is running on another thread.
Export of ChildWindow and ViewModel has the Part Creation policy non-shared
How can I solve this problem? Do you have any ideas?
Edit:
Ok. GetExportedValue() everytime returns me new instance for non-shared export. I change to Shared and everything is fine.
But i have some logic in constructor so i would like to create everytime a new instance when I opening new childwindow, and have the reference in container when I open next childWindow. So I started using ExportFactory and ExportLifetimeContext:
[Export] public class MainVM { [Import] public ExportFactory<ViewModel> ViewModelFactory { get; set; } public ExportLifetimeContext<ChildWindow> ViewModelContext { get; set; } [Import] public ExportFactory<NextViewModel> NextViewModelFactory { get; set; } public ExportLifetimeContext<NextChildWindow> NextViewModelContext { get; set; } [Import] public ExportFactory<NextNextViewModel> NextNextViewModelFactory { get; set; } public ExportLifetimeContext<NextNextChildWindow> NextNextViewModelContext { get; set; } }
Im creating ChildWindow and instance of vm in MainVM following way:
ChildWindowInstance.DataContext = ViewModelFactory.CreateExport()).Value
In ViewModel Im creating new ChildWIndow and NextViewModel and assign this to DataContext
NextChildWindowInstance.DataContext = container.GetExportedValue<*MainVM*>().NextViewModelFactory.CreateExport()).Value
Im getting reference to previous viewmodel in following way:
container.GetExportedValue<*MainVM*>().ViewModelContext
When I repeat previous step and make NextNextChildwindow and next NextNextViewmodel by factory method the context of this viewmodel is null somehow ?
Thus, in some deep level of my child windows when I invoke CreateExport method the lifetimecontext is not initialized and pointing to null...
Could someone explain me what Im doing wrong ? I know this description is little complicated :)
Thanks in advance
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog