问题
I have a question regarding Local Storage on Windows 8.
I want to store a string array of stock symbols. What is the best way to do something like this on Windows 8?
Browsing around, some have suggested store each string in the array into a new line in a text file, saved and retrieved from local storage.
If so, how would I achieve this? Are there better alternatives? Should I go down the path of sqlite?
回答1:
Here's a sample demonstrates how to save a collection to local storage, please check it: http://code.msdn.microsoft.com/windowsapps/CSWinStoreAppSaveCollection-bed5d6e6
回答2:
I'd recommend using the Isolated storage to store application values, possibly as serialised JSON, an object, etc. Here is a code example:
-- Set data
var localStorage = ApplicationData.Current.LocalSettings;
localStorage.Values[key] = value;
-- Get data
var localStorage = ApplicationData.Current.LocalSettings;
if(localStorage.Values.ContainsKey(key)
return (T)localStorage.Values[key];
Although it's usually more practical to wrap the above in a helper class.
Other options would be writing a file to disk such as SQLite or an XML/JSON file but the above should work just fine for a small amount of data.
回答3:
To save an array into local storage, you need to convert it to a string.
var localSettings = Windows.Storage.ApplicationData.current.localSettings;
var someSetting= new Array();
//Add values you want to store
someSetting.push("someValue");
//Change to string and save to local Storage
//toString will convert the array to a string with values separated by a comma
localSettings.values["someSettingInStorage"] = someSetting.toString();
//To retrieve the stored string value as a usable array
if (localSettings.values["someSetting"] != null)
someSetting = (localSettings.values["someSetting"]).split(",");
来源:https://stackoverflow.com/questions/14597478/best-way-to-store-string-array-in-local-storage-windows-8