UWP WebView中使用C#到JavaScript的JSON未正确读取



这相当令人困惑。我正在使用WebView将HTML5游戏转换为UWP来托管游戏内容,并希望从WebView中的localStorage进行备份,并在localStorage中为整个应用程序创建一个副本(即从浏览器存储到包存储(。这当然是一个旨在防止玩家在硬件上崩溃的安全网(因为我显然希望数据备份到玩家的云存储(,但有些地方不太对劲。具体来说,备份是正确写入的,但当使用通过WinRT组件连接到主机的Javascript方法将它们从主机应用程序读回HTML5部分时,它们不会对WebView中的localStorage产生影响(这反过来会使其看起来没有保存数据,因此continue选项被禁用(。

这里有一个我正在做的代码示例:

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

最新更新