Easy way to save a game in WP7 Silverlight?

What is the best way to save game state in WP7 Silverlight? I prefer to save it in a text file. I want to save the game when the application goes into the background (for example, someone is calling, the user presses "back", etc.) Or it closes. How can i do this?

+1
source share
1 answer

update , this code is from the following articles:

Simple WP7 Mango app for background tasks, toasts and slabs Simple WP7 Mango app for background tasks, toasts and snippets: code explanation

Isolated storage works well.

This is the class that I use to serialize and deserialize an instance in json (or xml) in IsolStorage. This example uses ServiceStack.Text , but you can disable it.

To use, read and write:

public class MyClass { public void Save() { MutexedIsoStorageFile.Write(this, "MyClass.json", "MYCLASSJSON"); } public static MyClass Load() { return MutexedIsoStorageFile.Read<MyClass>("MyClass.json", "MYCLASSJSON"); } } public static class MutexedIsoStorageFile { public static T Read<T>(string fileName, string mutexName) where T : new() { var mutexFile = new Mutex(false, mutexName); var model = new T(); mutexFile.WaitOne(); try { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) using (var stream = new IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read, store)) using (var reader = new StreamReader(stream)) if (!reader.EndOfStream) { //var serializer = new XmlSerializer(typeof (T)); //model = (T) serializer.Deserialize(reader); model = JsonSerializer.DeserializeFromReader<T>(reader); } } finally { mutexFile.ReleaseMutex(); } return model; } public static void Write<T>(T data, string fileName, string mutexName) { var mutexFile = new Mutex(false, mutexName); mutexFile.WaitOne(); try { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, FileAccess.Write, store)) { //var serializer = new XmlSerializer(typeof (T)); //serializer.Serialize(stream, data); JsonSerializer.SerializeToStream(data, stream); } } finally { mutexFile.ReleaseMutex(); } } 
+6
source

All Articles