Storing data on Windows phone

血红的双手。 提交于 2019-12-24 13:35:02

问题


I'm working on an app that is going to store some favourites information that the user has added. I want to store two pieces of information for each favourite - a number and a piece of text. So it will be something like this:

123 Some text
1234 Some more text
1233 More text

And so on.

The number will be unique so it should be fine to use as a key and I will need to have this number stored separately anyway in order to use it to query some data.

What is the best way to store this data on Windows Phone? I've been looking at IsolatedStorage and specifically, ApplicationSettings however I think it only stores one piece of information at a time? At least when I added some favourites information, the original value got overwritten by the new value.

Do I need to use some sort of database to store this information in IsolatedStorage? I can't imagine the amount of data will be huge. I would expect the users may only add a handful of favourites at most.

What's the best way to go about storing some data that takes the form of a key and a value on Windows Phone? Once the user has added their favourites information, it will need to be stored and loaded automatically when the app is loaded.


回答1:


A very simple solution is to use the IsolatedStorageSettings. The settings is a dictionary of values. You access settings like such

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

bool useLocation;
if (settings.TryGetValue("UseLocation", out useLocation) == false)
{
    // provide a default value if the key does not exist
    useLocation = true;
}

You then can save settings like such

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

settings["UseLocation"] = value;
settings.Save();

Even better is to create a nice Settings class to take care of all of that for you. Here is a nice blog post detailing how to do that.



来源:https://stackoverflow.com/questions/21101395/storing-data-on-windows-phone

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