Advertisement
benhurrus

flash(light) Settings.xaml.cs

Dec 12th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1. using System;
  2. using System.IO.IsolatedStorage;
  3. using System.Diagnostics;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Navigation;
  10. using Microsoft.Phone.Controls;
  11. using Microsoft.Phone.Shell;
  12.  
  13. namespace flashlightSettings
  14. {
  15.     public class AppSettings
  16.     {
  17.         IsolatedStorageSettings settings;
  18.  
  19.         const string CheckBoxSettingKeyName = "CheckBoxSetting";
  20.  
  21.         const bool CheckBoxSettingDefault = true;
  22.  
  23.         public AppSettings()
  24.         {
  25.             settings = IsolatedStorageSettings.ApplicationSettings;
  26.         }
  27.  
  28.         public bool AddOrUpdateValue(string Key, Object value)
  29.         {
  30.             bool valueChanged = false;
  31.  
  32.             if (settings.Contains(Key))
  33.             {
  34.                 if (settings[Key] != value)
  35.                 {
  36.                     settings[Key] = value;
  37.                     valueChanged = true;
  38.                 }
  39.             }
  40.             else
  41.             {
  42.                 settings.Add(Key, value);
  43.                 valueChanged = true;
  44.             }
  45.             return valueChanged;
  46.         }
  47.  
  48.         public T GetValueOrDefault<T>(string Key, T defaultValue)
  49.         {
  50.             T value;
  51.  
  52.             // If the key exists, retrieve the value.
  53.             if (settings.Contains(Key))
  54.             {
  55.                 value = (T)settings[Key];
  56.             }
  57.             // Otherwise, use the default value.
  58.             else
  59.             {
  60.                 value = defaultValue;
  61.             }
  62.             return value;
  63.         }
  64.  
  65.         public void Save()
  66.         {
  67.             settings.Save();
  68.         }
  69.  
  70.         public bool CheckBoxSetting
  71.         {
  72.             get
  73.             {
  74.                 return GetValueOrDefault<bool>(CheckBoxSettingKeyName, CheckBoxSettingDefault);
  75.             }
  76.             set
  77.             {
  78.                 if (AddOrUpdateValue(CheckBoxSettingKeyName, value))
  79.                 {
  80.                     Save();
  81.                 }
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement