免责声明:尽管在发布答案时,提出此问题的用例已得到解决,但 Microsoft Store 不再接受开发相关项目的开发范例。然而,由于 StackOverflow 对删除此类内容有严格的限制,因此我选择将其保留以用于历史目的。
原问题如下:
这是相当令人困惑的。我正在通过使用 WebView 托管游戏内容将 HTML5 游戏转换为 UWP,并且希望从 WebView 中的 localStorage 进行备份,并在 localStorage 中为整个应用程序创建一个副本(即从浏览器存储到包存储)。这当然是一个预期的安全网,以防玩家在硬件上崩溃(因为我显然希望将数据备份到玩家的云存储),但是有些事情不太对劲。具体来说,备份已正确写入,但当使用通过 WinRT 组件连接到主机的 Javascript 方法将它们从主机应用程序读回 HTML5 部分时,它们不会对 WebView 中的 localStorage 产生影响(反过来使它看起来像没有保存数据,因此继续选项被禁用)。
这是我正在做的代码示例:
var i;
for (i = -1; i <= 200; i++) {
var data = window.UWPConnect.getSaveFile(i);
var key = 'File'+i;
if (i === -1) key = 'Config';
if (i === 0) key = 'Global';
localStorage.setItem(key, data);
}
第一部分读取我的 WinRT 组件以从磁盘加载保存数据。
public void doSave(int key, string data) {
/*StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile saveWrite = await folder.CreateFileAsync("saveData" + key + ".json", CreationCollisionOption.ReplaceExisting);
try { await saveWrite.DeleteAsync(); } catch { }
String[] lines = { data };
await FileIO.WriteLinesAsync(saveWrite, lines);*/
if (key == -1) { System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\config.json", data); }
else if (key == 0) { System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\global.json", data); }
else
{
System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\saveData" + key + ".json", data);
}
}
public void doStartup()
{
StorageFolder folder = ApplicationData.Current.LocalFolder;
for (int i = -1; i <= 200; i++)
{
try {
if (i == -1)
{
saveData[i] = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\config.json");
} else if (i == 0)
{
saveData[i] = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\global.json");
}
else
{
saveData[i] = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\saveData" + i + ".json");
}
Debug.WriteLine(saveData[i]);
}
catch {}
/*Debug.WriteLine(File.Exists(SaveFile));
if (File.Exists(SaveFile)) {
try { saveData[i] = File.ReadAllText(SaveFile);
Debug.WriteLine(saveData[i]); }
catch { }
}*/
}
}
public string getSaveFile(int savefileId)
{
string data;
try
{
data = saveData[savefileId];
if (data == null) data = "";
Debug.WriteLine(data);
}
catch { data = ""; }
return data;
}
第二部分处理磁盘的保存和加载(取自 WinRT 组件)。
我想我已经明白了。在加载和保存数据的读取器中,我必须在 WinRT 端使用一组不同的指令,因此这是新的代码结构:
public void doSave(int key, string data) {
if (key == -1) { System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\config.json", data); }
else if (key == 0) { System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\global.json", data); }
else
{
System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\file" + key + ".json", data);
}
}
public void doBackup(int key, string data) {
System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\backup" + key + ".json", data);
}
public void doStartup()
{
for (int i = 0; i <= 200; i++)
{
if (i == -1)
{
if (System.IO.File.Exists(ApplicationData.Current.LocalFolder.Path + "\\config.json"))
{
saveData[i] = System.IO.File.ReadAllText(ApplicationData.Current.RoamingFolder.Path + "\\config.json");
}
}
else if (i == 0)
{
if (System.IO.File.Exists(ApplicationData.Current.LocalFolder.Path + "\\global.json"))
{
saveData[i] = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\global.json");
}
}
else
{
if (System.IO.File.Exists(ApplicationData.Current.LocalFolder.Path + "\\file" + i + ".json"))
{
saveData[i] = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\file" + i + ".json");
}
if (System.IO.File.Exists(ApplicationData.Current.LocalFolder.Path + "\\backup" + i + ".json"))
{
backup[i] = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\backup" + i + ".json");
}
}
}
}
public string getSaveFile(int savefileId)
{
string data;
try
{
data = saveData[savefileId];
if (data == null) data = "";
Debug.WriteLine(data);
}
catch { data = ""; }
return data;
}
public string getBackup(int savefileId)
{
string data;
try
{
data = backup[savefileId];
if (data == null) data = "";
Debug.WriteLine(data);
}
catch { data = ""; }
return data;
}
public string getConfig()
{
string data;
try
{
data = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\config.json");
if (data == null) data = "";
Debug.WriteLine(data);
}
catch { data = ""; }
return data;
}