StorageHandler

Topics: Getting Started
Feb 15, 2012 at 7:00 AM

Hello,

Hopefully someone can point me in the right direction with a question that I have.

If I have a StorageHandler defined as:

public class AddAccountPageStorageWorker : StorageHandler<AddAccountViewModel>

And in there I do something like:

        public override void Configure()
        {
            this.Property(vm => vm.Username)
                .InAppSettings()
                .RestoreAfterActivation();

            this.Property(vm => vm.Password)
                .InAppSettings()
                .RestoreAfterActivation();
        }

How do I then access these properties within another ViewModel?  Do I just add the same properties to the other ViewModel, and create a StorageHandler for that, or what is the recommended best approach?  Basically, I want to provide the ability in one View for a user to enter their login details for a site, and then in another View, access the information from the website and display it.

Thanks in advance!

Gary

Feb 15, 2012 at 3:51 PM
Edited Feb 15, 2012 at 3:56 PM

Gary,

Now you can do a simple constructor injection into the ViewModel you want the data to be available to.

public class EditAccountViewModel : Screen
{

   private AddAccountViewModel addAccount;

   public EditAccountViewModel(INavigationService service, AddAccountViewModel addVM)
   {
         addAccount = addVM;
   }
}

since the viewmodels are all registered with the Container they should be called checked for storage and then injected where you wanted it.  I do something similar with SettingsViewModel for my WP7 app.  I store the settings InAppSettings and then call where ever I inject the ViewModel.  One thing I should mention is that my SettingsViewModel is a singleton in the container.

Feb 15, 2012 at 5:30 PM

Hey,

Ah ok, that makes sense.  And that will give me the re-hydrated version of the properties within that ViewModel, recovered from isolated storage?

Is this the best practice?  Or is there a better way to share properties, stored in isolated storage, between ViewModels.

Thanks

Gary

Feb 16, 2012 at 7:50 AM

as long as its in the container it will be re-hydrated when it's called upon.  As far as I know it is best practice, with CM. 

Feb 16, 2012 at 7:40 PM

Thanks mvermef,

This worked perfectly :)

Gary

Feb 17, 2012 at 2:57 PM

np glad to help