我想将数据存储在下拉列表中以及2个单选按钮之间我必须使用Window Phone 7中的隔离存储来存储一个值 如何在Wp7中进行隔离存储?
答案 0 :(得分:6)
最简单的方法是使用IsolatedStorageSettings.ApplicationSettings类/属性 - 对于所有小的临时数据来说,它是一个很棒的dumpbin,无论发生什么都必须存活。通常,该对象会自动从ISO商店保存/恢复,但要小心相信 - 如果您的应用程序正常关闭,它就可以正常工作。如果你想防范这种情况,例如,应用程序崩溃/等等 - 你仍然应该定期手动调用此对象的SAVE。
一些链接/图片: MSDN:非常好的解释一个例子:http://msdn.microsoft.com/en-us/library/cc221360(v=vs.95).aspx 来自谷歌的http://dotnet.dzone.com/articles/using-application-settings
的第一名答案 1 :(得分:5)
这是我用于在WP7应用中加载和保存高分的代码片段,根据您的需要进行调整。它可以挽救数百万人的生命:D
private void LoadHighScore()
{
// open isolated storage, and load data from the savefile if it exists.
#if WINDOWS_PHONE
using (IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication())
#else
using (IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForDomain())
#endif
{
if (savegameStorage.FileExists("guessthecard.txt"))
{
using (IsolatedStorageFileStream fs = savegameStorage.OpenFile("guessthecard.txt", System.IO.FileMode.Open))
{
if (fs != null)
{
// Reload the saved high-score data.
byte[] saveBytes = new byte[4];
int count = fs.Read(saveBytes, 0, 4);
if (count > 0)
{
highScore = System.BitConverter.ToInt32(saveBytes, 0);
}
}
}
}
}
}
// Save highscore
public async void UnloadContent()
{
// SAVE HIGHSCORE
// Save the game state (in this case, the high score).
#if WINDOWS_PHONE
IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication();
#else
IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForDomain();
#endif
// open isolated storage, and write the savefile.
IsolatedStorageFileStream fs = null;
using (fs = savegameStorage.CreateFile("guessthecard.txt"))
{
if (fs != null)
{
// just overwrite the existing info for this example.
byte[] bytes = System.BitConverter.GetBytes(highScore);
fs.Write(bytes, 0, bytes.Length);
}
}
try
{
CardGuess item = new CardGuess { Text = highScore.ToString() };
await App.MobileService.GetTable<CardGuess>().InsertAsync(item);
}
catch(Exception e)
{
}
}
答案 2 :(得分:0)
与普通Silverlight相同(非现有站点存储除外)。有关详细信息,请参阅任何教程/书籍。
答案 3 :(得分:0)
观看此视频..它的过程非常清楚 http://msdn.microsoft.com/en-us/gg241265
答案 4 :(得分:0)
将此类用于隔离存储 公共类ApplicationSettings { /// ///获取给定键值的方法 /// /// /// /// public static T GetKeyValue(string key) { 尝试 { if(IsolatedStorageSettings.ApplicationSettings.Contains(key)) return(T)IsolatedStorageSettings.ApplicationSettings [key]; 其他 return default(T); } catch(异常){return default(T); } } /// ///设置键值的方法 /// /// /// /// public static void SetKeyValue(string key,T value) { if(IsolatedStorageSettings.ApplicationSettings.Contains(key)) IsolatedStorageSettings.ApplicationSettings [key] = value; 其他 IsolatedStorageSettings.ApplicationSettings.Add(key,value);
IsolatedStorageSettings.ApplicationSettings.Save();
}
/// <summary>
/// method to remove key from isolated storage
/// </summary>
/// <param name="key"></param>
public static void RemoveKey(string key)
{
try
{
IsolatedStorageSettings.ApplicationSettings.Remove(key);
}
catch
{
}
}
/// <summary>
/// method to check when a key exists in isolated storage
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool HasKey(string key)
{
try
{
if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
return true;
else
return false;
}
catch (Exception) { return false; }
}
}
然后将数据存储为 ApplicationSettings.SetKeyValue(“key”,value); 取回 var aaa = ApplicationSettings.GetKeyValue(“key”);