Best way to store string array in local storage, Windows 8?

做~自己de王妃 提交于 2019-12-12 01:34:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!