WP7墓碑,将精灵保存到当前状态不起作用

时间:2012-03-25 20:30:25

标签: windows-phone-7 tombstoning

我正在开发一款WP7游戏。我正在使用游戏状态管理(http://create.msdn.com/en-US/education/catalog/sample/game_state_management,但我认为它并不重要)我有将数据保存到的问题    Microsoft.Phone.Shell.PhoneApplicationService.Current.State

如果我在这个方法中放入sprite

   public override void Deactivate()
    {
        #if WINDOWS_PHONE            
        Microsoft.Phone.Shell.PhoneApplicationService.Current.State["Score"] = Score;
        Microsoft.Phone.Shell.PhoneApplicationService.Current.State["cloudSprite"] = cloudSprite;
        #endif
        base.Deactivate();
    }

中没有任何内容
    Microsoft.Phone.Shell.PhoneApplicationService.Current.State 

在激活方法中。但是,如果我删除了cloudSprite并将只有得分为int,那么它可以正常工作。我不知道什么是错的,也许它无法处理更复杂的对象。我试过浮动双倍,一切正常。但如果我把那些更复杂的东西放在那里它就行不通。你觉得怎么样?

修改

的 这是我的精灵课。我不知道如何使它可序列化。我添加了[DataContractAttribute()]和[DataMember],但它无法正常工作

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Graphics;
 using Microsoft.Xna.Framework.Content;
 using System.Runtime.Serialization;
 using System.IO;

 namespace GameStateManagementSample.GameObjects
 {
 [DataContractAttribute()]
public class Sprite
{
    [DataMember]
    public Vector2 Position;
     [DataMember]
    public Vector2 Size;
     [DataMember]
    public Texture2D Texture;

    [DataMember]
    public Rectangle Rect
    {
        get
        {
            return new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y);
        }
    }


    public Sprite(Vector2 position)
    {
        Position = position;
    }


    public Sprite(Vector2 position, Vector2 size)
    {
        Position = position;
        Size = size;
    }


    public Sprite(Vector2 position, Texture2D texture)
    {
        Position = position;
        Texture = texture;
        Size = new Vector2(Texture.Width, Texture.Height);
    }



    public void LoadContent(string assetName, ContentManager content)
    {
        Texture = content.Load<Texture2D>(assetName);

        if (Size == Vector2.Zero)
            Size = new Vector2(Texture.Width, Texture.Height);
    }

    public virtual void Draw(SpriteBatch spriteBatch)
    {
        //spriteBatch.Draw(Texture, Rect, Color.White);
        spriteBatch.Draw(Texture, Position, Color.White);

    }

    public virtual void Draw(SpriteBatch spriteBatch,  Rectangle TexturePositionInSpriteSheet, Color color)
    {
        spriteBatch.Draw(Texture, Position, TexturePositionInSpriteSheet, color);
    }

    public bool Intersects(Vector2 point)
    {
        if (point.X >= Position.X && point.Y >= Position.Y && point.X <= Position.X + Size.X && point.Y <= Position.Y + Size.Y)
            return true;
        else return false;
    }

    public bool Intersects(Rectangle rect)
    {
        return Rect.Intersects(rect);
    }



    public static void Serialize(Stream streamObject, object objForSerialization)
    {
        if (objForSerialization == null || streamObject == null)
            return;

        DataContractSerializer ser = new DataContractSerializer(objForSerialization.GetType());
        ser.WriteObject(streamObject, objForSerialization);
    }

    public static object Deserialize(Stream streamObject, Type serializedObjectType)
    {
        if (serializedObjectType == null || streamObject == null)
            return null;

        DataContractSerializer ser = new DataContractSerializer(serializedObjectType);
        return ser.ReadObject(streamObject);
    } 


}
 }

2 个答案:

答案 0 :(得分:1)

添加到State集合的对象使用DataContractSerializer序列化。确保您在那里保存的任何内容都可以通过这种方式进行序列化 任何序列化错误都会被忽略。

<强>更新

以下是Sprite对象的简化版本:

[DataContract]
public class Sprite
{
    [DataMember]
    public Vector2 Position;

    [DataMember]
    public Vector2 Size;

    [DataMember]
    public Texture2D Texture;

    public Sprite()
    {
    }

    public Sprite(Vector2 position)
    {
        Position = position;
    }

    public Sprite(Vector2 position, Vector2 size)
    {
        Position = position;
        Size = size;
    }

    public Sprite(Vector2 position, Texture2D texture)
    {
        Position = position;
        Texture = texture;
        Size = new Vector2(Texture.Width, Texture.Height);
    }
}

以下是序列化和反序列化的示例:

// Sprite序列化测试     var sprite1 = new Sprite(new Vector2(12.34f,56.78f));

Sprite sprite2;

using (var memStr = new MemoryStream())
{
    var serializer = new DataContractSerializer(typeof(Sprite));
    serializer.WriteObject(memStr, sprite1);

    memStr.Position = 0;
    var sr = new StreamReader(memStr);
    var serialized = sr.ReadToEnd();

    // serialized now looks like
    // <Sprite xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MiscExperiments"><Position xmlns:d2p1="http://schemas.datacontract.org/2004/07/Microsoft.Xna.Framework"><d2p1:X>12.34</d2p1:X><d2p1:Y>56.78</d2p1:Y></Position><Size xmlns:d2p1="http://schemas.datacontract.org/2004/07/Microsoft.Xna.Framework"><d2p1:X>0</d2p1:X><d2p1:Y>0</d2p1:Y></Size><Texture xmlns:d2p1="http://schemas.datacontract.org/2004/07/Microsoft.Xna.Framework.Graphics" i:nil="true" /></Sprite>

    memStr.Position = 0;

    sprite2 = (Sprite)serializer.ReadObject(memStr);

    // sprite2 now contains the same as 
    // sprite2.Position = { X:12.34, Y:56.78 }
}

答案 1 :(得分:0)

我相信你会得到ArgumentOutOfRangeException,如果你没有调试它会被忽略(在这种情况下抛出异常)。存储在State字典中的项目需要是可序列化的。尝试以某种方式存储该精灵并仅保存链接(源)到状态字典中的精灵。