AppSettings.cs (2775B)
1 using System; 2 using System.IO.IsolatedStorage; 3 using System.Diagnostics; 4 using System.Collections.Generic; 5 6 namespace File360 7 { 8 public class AppSettings 9 { 10 // Our settings 11 IsolatedStorageSettings settings; 12 13 //Key Names 14 const string SideBarColorKeyName = "SideBarColor"; 15 const string PageColorKeyName = "PageColor"; 16 17 // The default value of our settings 18 const string SideBarColorDefault = "{StaticResource PhoneChromeBrush}"; 19 const string PageColorDefault = "{StaticResource PhoneChromeBrush}"; 20 21 public AppSettings() 22 { 23 settings = IsolatedStorageSettings.ApplicationSettings; 24 } 25 26 public bool AddOrUpdateValue(string Key, Object value) 27 { 28 bool valueChanged = false; 29 30 // If the key exists 31 if (settings.Contains(Key)) 32 { 33 // If the value has changed 34 if (settings[Key] != value) 35 { 36 // Store the new value 37 settings[Key] = value; 38 valueChanged = true; 39 } 40 } 41 // Otherwise create the key. 42 else 43 { 44 settings.Add(Key, value); 45 valueChanged = true; 46 } 47 return valueChanged; 48 } 49 50 public T GetValueOrDefault<T>(string Key, T defaultValue) 51 { 52 T value; 53 54 // If the key exists, retrieve the value. 55 if (settings.Contains(Key)) 56 { 57 value = (T)settings[Key]; 58 } 59 // Otherwise, use the default value. 60 else 61 { 62 value = defaultValue; 63 } 64 return value; 65 } 66 67 /// <summary> 68 /// Save the settings. 69 /// </summary> 70 public void Save() 71 { 72 settings.Save(); 73 } 74 75 76 /// <summary> 77 /// Property to get and set a ListBox Setting Key. 78 /// </summary> 79 public string SideBarColor 80 { 81 get 82 { 83 return GetValueOrDefault<string>(SideBarColorKeyName, SideBarColorDefault); 84 } 85 set 86 { 87 if (AddOrUpdateValue(SideBarColorKeyName, value)) 88 { 89 Save(); 90 } 91 } 92 } 93 public string PageColor 94 { 95 get 96 { 97 return GetValueOrDefault<string>(PageColorKeyName, PageColorDefault); 98 } 99 set 100 { 101 if (AddOrUpdateValue(PageColorKeyName, value)) 102 { 103 Save(); 104 } 105 } 106 } 107 } 108 }