【问题标题】:Saving highscores on Windows Store app?在 Windows 应用商店应用中保存高分?
【发布时间】:2014-06-07 17:42:48
【问题描述】:

几个月前我创建了一个 Windows Phone 应用程序,现在我也希望将它用于 Windows 应用商店。 除了保存高分之外,我已经完成了所有工作。我尝试过通读 API,但由于某种原因似乎无法理解。

那么,谁能告诉我以下 Windows Phone 代码对于 Windows 应用商店应用程序应该是什么样的?考虑到其他一切,我预计它会几乎相同。谢谢。

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";
        }
    }

【问题讨论】:

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


    【解决方案1】:

    在 Windows Store App 中,您可以使用 LocalSettingsRoamingSettings 来保存和获取数据。 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 2016-03-31
      • 2014-01-06
      • 2013-04-16
      • 1970-01-01
      相关资源
      最近更新 更多