如何在 Win10 UWP App 中跨多个设备共享数据
为了使应用程序更具凝聚力,我们经常需要在使用一个 Microsoft 帐户登录的多个设备上保持用户的个人设置和首选项一致。在此示例中,我们使用漫游数据来存储和加载 UI 设置,游戏过程和用户信息。但漫游数据有其自身的限制:我们无法在漫游文件夹中存储大文件。系统会暂停程序包中所有应用程序的数据复制到云端,直到当前大小不再超过最大大小。因此,在此示例中,我们尚未将用户图像存储在漫游文件夹中。相反,它存储在本地文件夹中。
private async void LoadRoamingData()
{
//Get background color
object color = roamingSettings.Values["BackgroundColor"];
if (color != null)
{
if (ViewModel.ColorList.Keys.Contains(color.ToString()))
{
Color backgroundColor = ViewModel.ColorList[color.ToString()];
ViewModel.BackgroundColor = new SolidColorBrush(backgroundColor);
comboBackgroundColor.SelectedValue = color.ToString();
}
}
//Get game process stored in the roaming file
try
{
StorageFile processFile = await roamingFolder.GetFileAsync(processFileName);
string process = await FileIO.ReadTextAsync(processFile);
int gameProcess;
if (process != null && int.TryParse(process.ToString(), out gameProcess) && gameProcess > 0)
{
ViewModel.GameProcess = gameProcess;
}
}
catch { }
//Get user name
object userName = roamingSettings.Values["UserName"];
if (userName != null && !string.IsNullOrWhiteSpace(userName.ToString()))
{
ViewModel.UserName = userName.ToString();
}
}
有关更多信息,请参阅 https://code.msdn.microsoft.com/How-to-share-data-across-d492cc0b 。