A practical guide. Saving data in Windows Phone

I am writing an application for Windows Phone. What I want to do when the application is launched, it receives some data (settings or something else), and I want this data to be stored throughout the application; those. I do not want to continue reading isolated storage or calling the server whenever I need this piece of data.

What is the best way to do this (upload and share)? Bearing in mind the following:

  • I want it to be compatible with MVVM
  • I am using Caliburn.Micro
  • Data is not read-only.
  • An application has more than one page / View and ViewModels that share data

Thanks in advance.

+4
source share
4 answers

A static class, as Phil mentions, is a great idea, but I would suggest exploring the use of dependency injection. You may not need a full IoC container, but this will probably help you in your scenario. Caliburn.Micro integrates such a container very easily.

Create the Settings class. (I would also create an ISettings interface ISettings that you can pass the stub settings to your view models for testing, but this is an added bonus.) Then make all your ViewModels the necessary instance of ISettings in your constructors.

When your application starts, you create one instance of Settings that reads from IsolStorage or wherever you are, then pass that instance to any CreateModel created.

This Settings class may be responsible for saving settings back to IsolStorage whenever necessary.

An example of this scenario:

In the AppBootstrapper class:

 PhoneContainer container; ISettings settings; protected override void Configure() { // Your usual stuff go here settings = new Settings(); settings.LoadSettings(); container.Instance(settings); } 

In your ViewModel class:

 ISettings settings; public MainPageViewModel(ISettings settings) { this.settings = settings; } 

At this point, you will have all your settings for your ViewModel.

+1
source

If I understood your question correctly, you could use a static class with static members in it. As long as you reference this class where you want to use members, they will exist until the application is launched.

I will update the answer with another solution if I misunderstood the "service life"

+1
source

With Caliburn micro, I like to create the SharedData class and register it in the container as a singleton. Then paste it into any ViewModels that should use it. The CM navigation service also makes it easier to traverse dta between pages using .WithParam.

Edit: I just realized that this is basically what Dennis said. I also mention that I also use SterlingDB to save some things between ViewModels.

+1
source

I misunderstood what Caliburn is - I thought for some reason it was a DI container. Since this is not the case, I would recommend using it. Personally, I used OpenNETCF IoC because I am familiar with it, but any supported container will work. When initializing the application, load your settings and any other services that you want for the life of the application and put them in the DI container (or create a container for them).

For IoC, which will look like this in application initialization:

 // assuming MySettings will initialize itself in the ctor RootWorkItem.Services.AddNew<MySettings, IMySettings>(); 

or

 var settings = new MySettings(); // init settings here RootWorkItem.Services.Add<IMySettings>(settings); 

Then, anywhere in the application you want, you will pull (enable) by the type of interface:

 var settings = RootWorkItem.Services.Get<IMySettings>(); 

And use the returned instance. You can use a specific type instead of an interface in all cases, but I prefer to use interfaces to make testing and mocking easier.

0
source

Source: https://habr.com/ru/post/1384338/


All Articles