我正在制作一款教育游戏(Windows 10 UWP、C# + XAML),我需要存储用户信息(特别是他们当前的分数)并在他们再次启动应用程序时检索它。我找到了一种方法来做到这一点(请参阅下面的代码),但我不知道这是否是此问题的正常解决方案。我目前正在创建一个 txt 文件,并在其中存储和检索数据。有更常见或更简单的方法吗?
这是我目前正在做的事情:
创建文件:
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.CreateFileAsync("nameOfTextFile.txt", CreationCollisionOption.OpenIfExists); //other options are ReplaceExisting
打开文件:
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.GetFileAsync("nameOfTextFile.txt");
将文本写入文件:
await FileIO.WriteTextAsync(sampleFile, "Put the added text here");
从文件中读取文本:
string someVariableName = await FileIO.ReadTextAsync(sampleFile);
-预先感谢您的帮助!!
最佳答案
虽然基于文件的方法是有效的,但还有更简单的方法,至少对于简单数据而言:您可以使用漫游(或本地)设置。例如,漫游设置可以在设备之间漫游,只要它们的大小不超过 64K,并且会将分数从用户的桌面传送到用户的手机。本地设置保留在计算机上。
设置易于使用:
IPropertySet propertySet = ApplicationData.Current.RoamingSettings.Values;
// Get previous score (or 0 if none)
int score = (int)(propertySet["Score"] ?? 0);
// ...play game...
// Set updated score:
propertySet["Score"] = score;
关于c# - 在 Windows 10 UWP 应用程序中存储用户的分数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37092279/