Advertisement
tomasslavicek

IsolatedStorageSettings on Xamarin.Android / MonoDroid

Oct 10th, 2013
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. #if ANDROID
  2. using System;
  3. using System.Net;
  4. using System.Windows;
  5. using Android.App;
  6. using Android.Content;
  7.  
  8. namespace System.IO.IsolatedStorage
  9. {
  10.     /// <summary>
  11.     /// TODO na Androidu to umí serializovat pouze string hodnoty!! (příp. int převedené na string)
  12.     /// </summary>
  13.     public class IsolatedStorageSettings
  14.     {
  15.         static IsolatedStorageSettings appSettings = null;
  16.         public static IsolatedStorageSettings ApplicationSettings
  17.         {
  18.             get
  19.             {
  20.                 if (appSettings == null)
  21.                     appSettings = new IsolatedStorageSettings();
  22.                 return appSettings;
  23.             }
  24.         }
  25.  
  26.         // Returns:
  27.         //     The value associated with the specified key. If the specified key is not
  28.         //     found, a get operation throws a System.Collections.Generic.KeyNotFoundException,
  29.         //     and a set operation creates a new element that has the specified key.
  30.         public object this[string key]
  31.         {
  32.             get
  33.             {
  34.                 // Load
  35.                 var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
  36.                 return prefs.GetString(key, null);
  37.             }
  38.             set
  39.             {
  40.                 Add(key, value);
  41.             }
  42.         }
  43.  
  44.         public void Add(string key, object value)
  45.         {
  46.             var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
  47.             var prefEditor = prefs.Edit();
  48.             prefEditor.PutString(key, Convert.ToString(value));
  49.             prefEditor.Commit();
  50.         }
  51.  
  52.         public bool Contains(string key)
  53.         {
  54.             var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
  55.             return prefs.Contains(key);
  56.         }
  57.  
  58.         public void Save()
  59.         {
  60.         }
  61.  
  62.     }
  63. }
  64. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement