How can I wait until view is initialized in ViewModel-first approach?

Feb 9, 2012 at 6:02 PM

I have a ViewModel marked with ExportViewModel with true as second parameter, and a View as a DataTemplate, that references ViewModel via DataType. Right after constructing an instance of ViewModel, IContextAware services are not imported yet, since no actual View is created by this moment. I need to access one of services at this moment (or maybe some time later).

Currently, I do this via continuation passing style, putting an action that uses services into Dispatcher queue with Render priority (which is just right after DataBinding, where actual service injection seems to happen). But this trick won't work in Silverlight, since its Dispatcher does not support priorities.

Is there any event provided that is fired on ViewModel once all its imports are satisfied?



        
    
Coordinator
Feb 9, 2012 at 6:20 PM

You can have a property with an [Import] and MEFed would inject this property when the view is resolved. You can then use the events as usual. Don't forget to have the [ExportViewModel("name", true)] the true tells mefed that this is a ViewModelFirst

Feb 9, 2012 at 6:25 PM

Thanks for quick answer.

But I think: does this actually solves my problem? How can I be guaranteed, that this new property would be injected last? I can introduce this technique for each service I must wait injection for, but this would be enormous code duplication. I need an event, that is raised after all service imports are resolved.

Coordinator
Feb 9, 2012 at 6:42 PM
I see... unfortunatly I did not do that .... I can implement it should be pretty straight forward... but in short I do not have it right now :(

If you want to change it yourself, look where the DataContextAware resolver resolves the ViewModel dependencies, then have an interface that a ViewModel can implement and call a method on it.


Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
MEFedMVVM - http://mefedmvvm.codeplex.com


On Thu, Feb 9, 2012 at 7:25 PM, LostTheBlack <notifications@codeplex.com> wrote:

From: LostTheBlack

Thanks for quick answer.

But I think: does this actually solves my problem? How can I be guaranteed, that this new property would be injected last? I can introduce this technique for each service I must wait injection for, but this would be enormous code duplication. I need an event, that is raised after all service imports are resolved.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com