使用本地数据库在Windows Phone 7中保存变量

Ngh*_*yen 3 local-database windows-phone-7

如何在Windows Phone 7中使用本地数据库只保存一个值,然后在每次加载(打开)应用程序时检索该值?

Ala*_*lan 7

如果您只想存储一个值进行检索,那么我建议使用IsolatedStorage,尤其是ApplicationSettings类.

它的用法示例:

using System.IO.IsolatedStorage;

//storing value
int someValue = 10;
IsolatedStorageSettings.ApplicationSettings.Add("MyKey",someValue);

//write or update value
IsolatedStorageSettings.ApplicationSettings["MyKey"] = someValue;

//write to disk
IsolatedStorageSettings.ApplicationSettings.Save();


//reading value
if(IsolatedStorageSettings.ApplicationSettings.Contains("MyKey"))
{
   int readValue = (int) IsolatedStorageSettings.ApplicationSettings["MyKey"];
}   
Run Code Online (Sandbox Code Playgroud)

芒果现在提供MSSqlCE支持,但对于一组值,它是过度的.如果需要存储关系数据,则数据库更合适,而不是持久化用户/应用程序设置.

虽然IsolatedStorage很好,但读取和写入的成本可能很高.避免从UI线程中读取IsolatedStorage,这会导致您的应用看起来没有响应.