Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

windows phone 7 - Easy way to save game in WP7 Silverlight?

What is the best way to save game state in WP7 Silverlight? I prefer to save it in text file. I want save game when application goes to background (for example someone is calling, user clicks 'back' etc.) or closing. How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

update, this code is from the following articles:

Simple WP7 Mango App for Background Tasks, Toast, and Tiles Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation

IsolatedStorage works well.

This is a class I use to serialize and deserialize an instance to json (or xml) in IsolatedStorage. This example is use ServiceStack.Text but it can be switched out.

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();
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...