Page/View State Management & Isolated Storage with Silverlight

Mar 4, 2011 at 9:33 PM

What is the recommended method, within the Caliburn.Micro framework, for managing Silverlight page state with the use of Isolated Storage. In reality, I believe we want to persist the state of the ViewModel that is associated with a View (Page) when the user navigates away from the view, and then restore the state of the ViewModel when the user navigates to the view. It seems that the key challenge is to get a reference to the ViewModel associated wih the View.

Thanks!

Coordinator
Mar 4, 2011 at 9:46 PM

It's hard to answer this question without knowing a little more about your architecture. I typically use screens and conductors for navigation. In this case, I just override the screens OnActivate/OnDeactivate to load/save any important data. You could also wire up your IoC container to pull certain view models out of IsolatedStorage. That technique should be used very carefully though and only on simple view models. If you are using the SL Navigation framework, then you should have a look at the WP7 version of Caliburn.Micro which has some integration with the navigation system. That may give you some ideas as well.

Mar 4, 2011 at 10:48 PM

Thanks, and yes I am using the SL Navigation framework.  (Note: I have also adopted an additional component, MEFNavigation, which helps to bind View & ViewModel via attributes.)  My ViewModel classes derive from Screen and my 'View' classes derive from Page.  While the ViewModel can override the OnActivate/OnDeactivate methods, those methods are not being called.  The View's OnNavigatedTo/OnNavigatedFrom methods are being called, and I don't mind triggering the interaction with Iso Storage from those methods, but I haven't found a way to access the ViewModel that is associated with the View.  One approach would be to specifically identify the desired ViewModel class from the View, but that sets up coupling that Caliburn.Micro helps to avoid. 

Coordinator
Mar 4, 2011 at 11:43 PM

I think that the MEFNavigation framework is a bit of a work in progress, so it may not handle everything. You should try and get in touch with Greg Gum (http://silverlightdev.net/) and follow up on that. I would bet that he needs to add code to check for IActivate, IDeactivate and IGuardClose. 

Mar 4, 2011 at 11:48 PM

Good suggestion -- I'll follow up with Greg.  In the meantime, I found that I am able to obtain a reference to the ViewModel associated with a View by calling Caliburn.Micro.ViewModelLocator.LocateForView().  With that, I can call to the associated ViewModel to request that it persist or retrieve state information via Iso Storage.