Windows Phone How to install LocalSettings for the first time?

In the Desktop Application or Web Projects projects, App.configs and Web.configs files were for storing settings. These settings were set during development (or sometime later), but if this happens, it was ALWAYS after the action.

Windows Phone 8.1 XAML does not have an App.config file, so developers can use Windows.Storage.ApplicationData.Current.LocalSettings . Nice.

How can I set these parameters for the first time (this means that the first time I start the application always, so I can only read them later and sometimes update existing values)? Of course, I can set the settings every time I launch the application, but this time is wasting time. How do you install LocalSettings in your applications for the first time? I saw this solution. Is there a first run β€œflag in WP7 , but I don’t think this is the only possibility.

+5
source share
2 answers
 var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; // Create a simple setting localSettings.Values["exampleSetting"] = "Hello Windows"; // Read data from a simple setting Object value = localSettings.Values["exampleSetting"]; if (value == null) { // No data } else { // Access data in value } // Delete a simple setting localSettings.Values.Remove("exampleSetting"); 

Link Msdn

Data resilience

+11
source

I wrote the code:

  public void Initialize() { var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; if (!localSettings.Values.ContainsKey(FirstRunSettingName)) { localSettings.Values.Add(FirstRunSettingName, false); } localSettings.Values.Add(SettingNames.DataFilename, "todo.data.xml"); } public bool IsFirstRun() { var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; if (localSettings.Values.ContainsKey(FirstRunSettingName)) { return (bool)localSettings.Values[FirstRunSettingName]; } else { return true; } } 

In the App.xaml.cs file:

  public App() { this.InitializeComponent(); this.Suspending += this.OnSuspending; var storageService = Container.Get<ISettingsService>(); if (storageService.IsFirstRun()) { storageService.Initialize(); } } 

I'm not sure if this is the right way to set the settings for the first time, but it's some kind of soul.

+4
source

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


All Articles