Suggestion: Activation interface for ViewModels

Aug 4, 2010 at 8:27 AM

I'd like to be able to detect when my ViewModel has been properly initialized so that I can set some defaults or start an asynchronous web service requests.

I currently use the solution explained here to detect the DataContext change, and consequently, the ViewModel availability. I then call some Initialize method on the VM.

Aug 4, 2010 at 10:15 AM

Are you using  model-first approach or view-first (with Model.Bind)?

In the first case if you can implement IActivate interface on VM to have the infrastructure manage this lifecycle phase; the lifecycle, however, is only enforced when the VM is opened inside a Conductor or if the VM is the root model.

In the second case, the infrastructure seems to ignore lifecycle interfaces, but you can leverage IViewAware interface in the VM, which is called when the VM-view binding occur.

Aug 4, 2010 at 10:18 AM

Hi Marco,

I use view-first since those pages are part of a SL frame. And I have looked at IViewAware, but it looked a bit dirty to me :-)

Would be nice if Caliburn would use the IActivate interface during a Bind.Model call.

Aug 4, 2010 at 11:38 AM

Yes, I agree on this, even though I see potential problems related to VM (for example: which view owns the lifecycle responsibility of a singleton VM?)
That's why I feel very uncomfortable with view-first...

@Rob: any thoughts?

Aug 4, 2010 at 2:33 PM

Have a look at the new navigation stuff I added for WP7. You should be able to adapt this to work with an SL Frame. Even if it doesn't meet your needs, it might give you some ideas. I have a blog post coming soon that will discuss it.

Aug 7, 2010 at 4:58 PM
Edited Aug 7, 2010 at 5:57 PM

That navigation stuff works quite nicely and honors the IActivate interface as well. Check it out here.