使用 IsloatedStorageSettings 保存自定义对象

发布于 2024-11-06 08:42:58 字数 1152 浏览 0 评论 0原文

我试图在isolatedStorageSettings 中保存一个对象,以保存我的游戏的高分,但每当我尝试保存该对象的更新副本时,C# 似乎认为该对象没有更改。我尝试为 HighScores 类创建自定义 Equals 函数,但这似乎没有帮助。

知道我做错了什么吗?

谢谢

public bool AddOrUpdateValue(string Key, Object value)
{
    bool valueChanged = false;

    // If the key exists
    if (isolatedStore.Contains(Key))
    {
        // If the value has changed
        if (isolatedStore[Key] != value) //This keeps returning false
        {
            // Store the new value
            isolatedStore[Key] = value;
            valueChanged = true;
        }
    }
    // Otherwise create the key.
    else
    {
        isolatedStore.Add(Key, value);
        valueChanged = true;
    }

    return valueChanged;
}



//This is located inside the HighScores class    
public bool Equals(HighScores newHighScores)
{
    for (int i = 0; i < highScores.Length; i++)
    {
        if (!highScores[i].Name.Equals(newHighScores.GetIndex(i).Name))
        {
            return false;
        }

        if (!highScores[i].Time.Equals(newHighScores.GetIndex(i).Time))
        {
            return false;
        }
    }

    return true;
}

I'm trying to save an object in IsolatedStorageSettings to save the high scores for my game, but whenever I try to save an updated copy of the object C# seems to think the object hasn't changed. I tried creating a custom Equals function for the HighScores class but that doesn't seem to help.

Any idea what I'm doing wrong?

Thanks

public bool AddOrUpdateValue(string Key, Object value)
{
    bool valueChanged = false;

    // If the key exists
    if (isolatedStore.Contains(Key))
    {
        // If the value has changed
        if (isolatedStore[Key] != value) //This keeps returning false
        {
            // Store the new value
            isolatedStore[Key] = value;
            valueChanged = true;
        }
    }
    // Otherwise create the key.
    else
    {
        isolatedStore.Add(Key, value);
        valueChanged = true;
    }

    return valueChanged;
}



//This is located inside the HighScores class    
public bool Equals(HighScores newHighScores)
{
    for (int i = 0; i < highScores.Length; i++)
    {
        if (!highScores[i].Name.Equals(newHighScores.GetIndex(i).Name))
        {
            return false;
        }

        if (!highScores[i].Time.Equals(newHighScores.GetIndex(i).Time))
        {
            return false;
        }
    }

    return true;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

困倦 2024-11-13 08:42:58

您还没有实现相等运算符 '==' 和 '!=' 并且这些比较引用相等,您将提供映射到您的 'Equals' 方法的实现

http://msdn.microsoft.com/en-us/library/ms173147%28v=vs .80%29.aspx

You haven't implemented the equality operators '==' and '!=' and these compare reference equality, you are going to have provide the implementation which maps on to your 'Equals' method

http://msdn.microsoft.com/en-us/library/ms173147%28v=vs.80%29.aspx

客…行舟 2024-11-13 08:42:58

您应该执行isolatedStore.Save()来提交更改

You should do isolatedStore.Save() to commit the changes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文