MonoGame在iOS上缺少数据存储IsolatedStorageSettings类的解决办法
MonoGame的价值在于可以将原XNA游戏移植到iOS和Android平台。移植基础是Xamarin。在Xamarin.iOS缺少System.IO.IsolatedStorage.IsolatedStorageSettings类。这个类通常用来存储游戏数据状态,非常重要。下面提供IsolatedStorageSettings在iOS平台的实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization; using System.Text; namespace System.IO.IsolatedStorage { public sealed class IsolatedStorageSettings : IDictionary<string, object>, IDictionary, ICollection<KeyValuePair<string, object>>, ICollection, IEnumerable<KeyValuePair<string, object>>, IEnumerable { static private IsolatedStorageSettings application_settings; static private IsolatedStorageSettings site_settings; private IsolatedStorageFile container; private Dictionary<string, object> settings; // SL2 use a "well known" name and it's readable (and delete-able) directly by isolated storage private const string LocalSettings = "__LocalSettings"; internal IsolatedStorageSettings (IsolatedStorageFile isf) { container = isf; if (!isf.FileExists (LocalSettings)) { settings = new Dictionary<string, object> (); return; } using (IsolatedStorageFileStream fs = isf.OpenFile (LocalSettings, FileMode.Open)) { using (StreamReader sr = new StreamReader (fs)) { DataContractSerializer reader = new DataContractSerializer (typeof (Dictionary<string, object>)); try { settings = (Dictionary<string, object>) reader.ReadObject (fs); } catch (Xml.XmlException) { settings = new Dictionary<string, object> (); } } } } ~IsolatedStorageSettings () { // settings are automatically saved if the application close normally Save (); } // static properties // per application, per-computer, per-user public static IsolatedStorageSettings ApplicationSettings { get { if (application_settings == null) { application_settings = new IsolatedStorageSettings ( IsolatedStorageFile.GetUserStoreForApplication ()); } return application_settings; } } // per domain, per-computer, per-user public static IsolatedStorageSettings SiteSettings { get { if (site_settings == null) { site_settings = new IsolatedStorageSettings ( IsolatedStorageFile.GetUserStoreForSite ()); } return site_settings; } } // properties public int Count { get { return settings.Count; } } public ICollection Keys { get { return settings.Keys; } } public ICollection Values { get { return settings.Values; } } public object this [string key] { get { return settings [key]; } set { settings [key] = value; } } // methods public void Add (string key, object value) { settings.Add (key, value); } // This method is emitted as virtual due to: https://bugzilla.novell.com/show_bug.cgi?id=446507 public void Clear () { settings.Clear (); } public bool Contains (string key) { if (key == null) throw new ArgumentNullException ("key"); return settings.ContainsKey (key); } public bool Remove (string key) { return settings.Remove (key); } public void Save () { using (IsolatedStorageFileStream fs = container.CreateFile (LocalSettings)) { DataContractSerializer ser = new DataContractSerializer (settings.GetType ()); ser.WriteObject (fs, settings); } } public bool TryGetValue<T> (string key, out T value) { object v; if (!settings.TryGetValue (key, out v)) { value = default (T); return false; } value = (T) v; return true; } // explicit interface implementations int ICollection<KeyValuePair<string, object>>.Count { get { return settings.Count; } } bool ICollection<KeyValuePair<string, object>>.IsReadOnly { get { return false; } } void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> item) { settings.Add (item.Key, item.Value); } void ICollection<KeyValuePair<string, object>>.Clear () { settings.Clear (); } bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> item) { return settings.ContainsKey (item.Key); } void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int arrayIndex) { (settings as ICollection<KeyValuePair<string, object>>).CopyTo (array, arrayIndex); } bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> item) { return settings.Remove (item.Key); } ICollection<string> IDictionary<string, object>.Keys { get { return settings.Keys; } } ICollection<object> IDictionary<string, object>.Values { get { return settings.Values; } } bool IDictionary<string, object>.ContainsKey (string key) { return settings.ContainsKey (key); } bool IDictionary<string, object>.TryGetValue (string key, out object value) { return settings.TryGetValue (key, out value); } private string ExtractKey (object key) { if (key == null) throw new ArgumentNullException ("key"); return (key as string); } void IDictionary.Add (object key, object value) { string s = ExtractKey (key); if (s == null) throw new ArgumentException ("key"); settings.Add (s, value); } void IDictionary.Clear () { settings.Clear (); } bool IDictionary.Contains (object key) { string skey = ExtractKey (key); if (skey == null) return false; return settings.ContainsKey (skey); } object IDictionary.this [object key] { get { string s = ExtractKey (key); return (s == null) ? null : settings [s]; } set { string s = ExtractKey (key); if (s == null) throw new ArgumentException ("key"); settings [s] = value; } } bool IDictionary.IsFixedSize { get { return false; } } bool IDictionary.IsReadOnly { get { return false; } } void IDictionary.Remove (object key) { string s = ExtractKey (key); if (s != null) settings.Remove (s); } void ICollection.CopyTo (Array array, int index) { (settings as ICollection).CopyTo (array, index); } bool ICollection.IsSynchronized { get { return (settings as ICollection).IsSynchronized; } } object ICollection.SyncRoot { get { return (settings as ICollection).SyncRoot; } } IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator () { return settings.GetEnumerator (); } IEnumerator IEnumerable.GetEnumerator () { return settings.GetEnumerator (); } IDictionaryEnumerator IDictionary.GetEnumerator () { return settings.GetEnumerator (); } } } |
还需要引用System.Runtime.Serialization.dll
如果您对C#游戏开发感兴趣,可以扫下面二维码加入我们的QQ群来一起学习交流
原创文章,转载请注明本文链接地址(违者必究):MonoGame在iOS上缺少数据存储IsolatedStorageSettings类的解决办法