在Windows应用商店应用中保存高分?

时间:2014-06-07 17:42:49

标签: c# windows-store-apps storage windows-store

几个月前我创建了一个Windows Phone应用程序,我现在也想在Windows Store中创建它。 除了保存高分之外,我已经完成了所有工作。我试过阅读API,但似乎无法出于某种原因。

那么,有人能告诉我Windows Store应用程序的以下Windows Phone代码应该是什么样的吗?考虑到其他一切,我预计它几乎是一样的。感谢。

private IsolatedStorageSettings appsettings = IsolatedStorageSettings.ApplicationSettings;

private void SetHighScore()
    {
        if (appsettings.Contains("highscore"))
        {
            int high = Convert.ToInt32(appsettings["highscore"]);

            if (score > high) //if the current score is greater than the high score, make it the new high score
            {
                appsettings.Remove("highscore");
                appsettings.Add("highscore", score);

                GoogleAnalytics.EasyTracker.GetTracker().SendEvent("mainpage", "highscore " + score.ToString(), null, score);
            }

            else //if the current score if less than or equal to the high score, do nothing
            {
                //do nothing
            }
        }

        else
        {
            appsettings.Add("highscore", score); //if there is no high score already set, set it to the current score
        }
    }

    private void GetHighScore()
    {
        if (appsettings.Contains("highscore")) //if there is a highscore display it
        {
            int high = Convert.ToInt32(appsettings["highscore"]);
            txbHighScore.Text = high.ToString();
        }

        else //if there is no highscore display zero
        {
            txbHighScore.Text = "0";
        }
    }

1 个答案:

答案 0 :(得分:2)

在Windows应用商店应用中,您可以使用 LocalSettings RoamingSettings 来保存和获取数据。 LocalSettings会将您的数据保存在一台设备中,RoamingSettings会在具有相同帐户的设备上同步您的数据。 这是LocalSettings的示例代码,RoamingSettings类似:

public class LocalSettingsHelper
{
    public static void Save<T>(string key, T value)
    {
        ApplicationData.Current.LocalSettings.Values[key] = value;
    }

    public static T Read<T>(string key)
    {
        if (ApplicationData.Current.LocalSettings.Values.ContainsKey(key))
        {
            return (T)ApplicationData.Current.LocalSettings.Values[key];
        }
        else
        {
            return default(T);
        }
    }

    public static bool Remove(string key)
    {
        return ApplicationData.Current.LocalSettings.Values.Remove(key);
    }
}