How to do isolated storage in Wp7? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this questionI want to store data in dropdown list a开发者_Go百科nd between 2 radio button I have to store one value using isolated storage in Window phone 7 a How to do isolated storage in Wp7?
The easiest way to do it, is to use the IsolatedStorageSettings.ApplicationSettings class/property - it is a great dumpbin for all your small temporary data that must survive whatever happens. Generally, that object is automatically saved/restored from the ISO store, but be careful in believing that - it works if your app is closed gracefully. If you want to guard that against for example, an application crash/etc - you still should periodically manually call SAVE on this object.
Some links/tuts: MSDN: quite nice explanation an example: http://msdn.microsoft.com/en-us/library/cc221360(v=vs.95).aspx first-whetever from google http://dotnet.dzone.com/articles/using-application-settings
This is the code snippet I use for loading and saving High scores in a WP7 app, tweak it to suit your needs. It could save millions of lives :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)
{
}
}
The same as in normal Silverlight (except for the non existing site storage). See any tutorials/books for further information.
Use this class for Isolated storage public class ApplicationSettings { /// /// method to get value of given key /// /// /// /// public static T GetKeyValue(string key) { try { if (IsolatedStorageSettings.ApplicationSettings.Contains(key)) return (T)IsolatedStorageSettings.ApplicationSettings[key]; else return default(T); } catch (Exception) { return default(T); } } /// /// method to set value of key /// /// /// /// public static void SetKeyValue(string key, T value) { if (IsolatedStorageSettings.ApplicationSettings.Contains(key)) IsolatedStorageSettings.ApplicationSettings[key] = value; else 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; }
}
}
then store data as ApplicationSettings.SetKeyValue("key", value); retrieve var aaa=ApplicationSettings.GetKeyValue("key");
精彩评论