MonoGame的价值在于可以将原XNA游戏移植到iOS和Android平台。移植基础是Xamarin。在Xamarin.iOS缺少System.IO.IsolatedStorage.IsolatedStorageSettings类。这个类通常用来存储游戏数据状态,非常重要。下面提供IsolatedStorageSettings在iOS平台的实现代码:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.Serialization;
  6. using System.Text;
  7. namespace System.IO.IsolatedStorage {
  8. public sealed class IsolatedStorageSettings : IDictionary<string, object>, IDictionary,
  9. ICollection<KeyValuePair<string, object>>, ICollection,
  10. IEnumerable<KeyValuePair<string, object>>, IEnumerable {
  11. static private IsolatedStorageSettings application_settings;
  12. static private IsolatedStorageSettings site_settings;
  13. private IsolatedStorageFile container;
  14. private Dictionary<string, object> settings;
  15. // SL2 use a "well known" name and it's readable (and delete-able) directly by isolated storage
  16. private const string LocalSettings = "__LocalSettings";
  17. internal IsolatedStorageSettings (IsolatedStorageFile isf)
  18. {
  19. container = isf;
  20. if (!isf.FileExists (LocalSettings)) {
  21. settings = new Dictionary<string, object> ();
  22. return;
  23. }
  24. using (IsolatedStorageFileStream fs = isf.OpenFile (LocalSettings, FileMode.Open)) {
  25. using (StreamReader sr = new StreamReader (fs)) {
  26. DataContractSerializer reader = new DataContractSerializer (typeof (Dictionary<string, object>));
  27. try {
  28. settings = (Dictionary<string, object>) reader.ReadObject (fs);
  29. } catch (Xml.XmlException) {
  30. settings = new Dictionary<string, object> ();
  31. }
  32. }
  33. }
  34. }
  35. ~IsolatedStorageSettings ()
  36. {
  37. // settings are automatically saved if the application close normally
  38. Save ();
  39. }
  40. // static properties
  41. // per application, per-computer, per-user
  42. public static IsolatedStorageSettings ApplicationSettings {
  43. get {
  44. if (application_settings == null) {
  45. application_settings = new IsolatedStorageSettings (
  46. IsolatedStorageFile.GetUserStoreForApplication ());
  47. }
  48. return application_settings;
  49. }
  50. }
  51. // per domain, per-computer, per-user
  52. public static IsolatedStorageSettings SiteSettings {
  53. get {
  54. if (site_settings == null) {
  55. site_settings = new IsolatedStorageSettings (
  56. IsolatedStorageFile.GetUserStoreForSite ());
  57. }
  58. return site_settings;
  59. }
  60. }
  61. // properties
  62. public int Count {
  63. get { return settings.Count; }
  64. }
  65. public ICollection Keys {
  66. get { return settings.Keys; }
  67. }
  68. public ICollection Values {
  69. get { return settings.Values; }
  70. }
  71. public object this [string key] {
  72. get {
  73. return settings [key];
  74. }
  75. set {
  76. settings [key] = value;
  77. }
  78. }
  79. // methods
  80. public void Add (string key, object value)
  81. {
  82. settings.Add (key, value);
  83. }
  84. // This method is emitted as virtual due to: https://bugzilla.novell.com/show_bug.cgi?id=446507
  85. public void Clear ()
  86. {
  87. settings.Clear ();
  88. }
  89. public bool Contains (string key)
  90. {
  91. if (key == null)
  92. throw new ArgumentNullException ("key");
  93. return settings.ContainsKey (key);
  94. }
  95. public bool Remove (string key)
  96. {
  97. return settings.Remove (key);
  98. }
  99. public void Save ()
  100. {
  101. using (IsolatedStorageFileStream fs = container.CreateFile (LocalSettings)) {
  102. DataContractSerializer ser = new DataContractSerializer (settings.GetType ());
  103. ser.WriteObject (fs, settings);
  104. }
  105. }
  106. public bool TryGetValue<T> (string key, out T value)
  107. {
  108. object v;
  109. if (!settings.TryGetValue (key, out v)) {
  110. value = default (T);
  111. return false;
  112. }
  113. value = (T) v;
  114. return true;
  115. }
  116. // explicit interface implementations
  117. int ICollection<KeyValuePair<string, object>>.Count {
  118. get { return settings.Count; }
  119. }
  120. bool ICollection<KeyValuePair<string, object>>.IsReadOnly {
  121. get { return false; }
  122. }
  123. void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> item)
  124. {
  125. settings.Add (item.Key, item.Value);
  126. }
  127. void ICollection<KeyValuePair<string, object>>.Clear ()
  128. {
  129. settings.Clear ();
  130. }
  131. bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> item)
  132. {
  133. return settings.ContainsKey (item.Key);
  134. }
  135. void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int arrayIndex)
  136. {
  137. (settings as ICollection<KeyValuePair<string, object>>).CopyTo (array, arrayIndex);
  138. }
  139. bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> item)
  140. {
  141. return settings.Remove (item.Key);
  142. }
  143. ICollection<string> IDictionary<string, object>.Keys {
  144. get { return settings.Keys; }
  145. }
  146. ICollection<object> IDictionary<string, object>.Values {
  147. get { return settings.Values; }
  148. }
  149. bool IDictionary<string, object>.ContainsKey (string key)
  150. {
  151. return settings.ContainsKey (key);
  152. }
  153. bool IDictionary<string, object>.TryGetValue (string key, out object value)
  154. {
  155. return settings.TryGetValue (key, out value);
  156. }
  157. private string ExtractKey (object key)
  158. {
  159. if (key == null)
  160. throw new ArgumentNullException ("key");
  161. return (key as string);
  162. }
  163. void IDictionary.Add (object key, object value)
  164. {
  165. string s = ExtractKey (key);
  166. if (s == null)
  167. throw new ArgumentException ("key");
  168. settings.Add (s, value);
  169. }
  170. void IDictionary.Clear ()
  171. {
  172. settings.Clear ();
  173. }
  174. bool IDictionary.Contains (object key)
  175. {
  176. string skey = ExtractKey (key);
  177. if (skey == null)
  178. return false;
  179. return settings.ContainsKey (skey);
  180. }
  181. object IDictionary.this [object key] {
  182. get {
  183. string s = ExtractKey (key);
  184. return (s == null) ? null : settings [s];
  185. }
  186. set {
  187. string s = ExtractKey (key);
  188. if (s == null)
  189. throw new ArgumentException ("key");
  190. settings [s] = value;
  191. }
  192. }
  193. bool IDictionary.IsFixedSize {
  194. get { return false; }
  195. }
  196. bool IDictionary.IsReadOnly {
  197. get { return false; }
  198. }
  199. void IDictionary.Remove (object key)
  200. {
  201. string s = ExtractKey (key);
  202. if (s != null)
  203. settings.Remove (s);
  204. }
  205. void ICollection.CopyTo (Array array, int index)
  206. {
  207. (settings as ICollection).CopyTo (array, index);
  208. }
  209. bool ICollection.IsSynchronized {
  210. get { return (settings as ICollection).IsSynchronized; }
  211. }
  212. object ICollection.SyncRoot {
  213. get { return (settings as ICollection).SyncRoot; }
  214. }
  215. IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator ()
  216. {
  217. return settings.GetEnumerator ();
  218. }
  219. IEnumerator IEnumerable.GetEnumerator ()
  220. {
  221. return settings.GetEnumerator ();
  222. }
  223. IDictionaryEnumerator IDictionary.GetEnumerator ()
  224. {
  225. return settings.GetEnumerator ();
  226. }
  227. }
  228. }
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