UWP WebView中使用C#到JavaScript的JSON未正确读取时出现问题
这相当令人困惑。我正在使用WebView将HTML5游戏转换为UWP来承载游戏内容,并希望从WebView中的localStorage进行备份,并在localStorage中为整个应用程序创建一个副本(即从浏览器存储到软件包存储)。这当然是一个预期的安全网,以防玩家在硬件上浪费时间(因为我显然希望数据备份到玩家的云存储中),但是有些事情不太对劲。具体地说,备份是正确写入的,但当使用通过WinRT组件连接到主机的Javascript方法从主机应用程序读回HTML5部分时,它们不会对WebView中的本地存储产生影响(这反过来使其看起来好像没有保存数据,因此禁用了“继续”选项) 下面是我所做工作的代码示例:UWP WebView中使用C#到JavaScript的JSON未正确读取时出现问题,javascript,c#,uwp,Javascript,C#,Uwp,这相当令人困惑。我正在使用WebView将HTML5游戏转换为UWP来承载游戏内容,并希望从WebView中的localStorage进行备份,并在localStorage中为整个应用程序创建一个副本(即从浏览器存储到软件包存储)。这当然是一个预期的安全网,以防玩家在硬件上浪费时间(因为我显然希望数据备份到玩家的云存储中),但是有些事情不太对劲。具体地说,备份是正确写入的,但当使用通过WinRT组件连接到主机的Javascript方法从主机应用程序读回HTML5部分时,它们不会对WebView中
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);
}
vari;
对于(i=-1;i我想我找到了答案。在加载和保存数据的数据读取器中,我不得不在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;
}
public void doSave(int键,字符串数据){
if(key==-1){System.IO.File.writealText(ApplicationData.Current.LocalFolder.Path+“\\config.json”,data);}
如果(key==0){System.IO.File.writealText(ApplicationData.Current.LocalFolder.Path+“\\global.json”,data);}
其他的
{
System.IO.File.writealText(ApplicationData.Current.LocalFolder.Path+“\\File”+key+“.json”,数据);
}
}
公共无效数据备份(int键、字符串数据){
System.IO.File.writealText(ApplicationData.Current.LocalFolder.Path+“\\backup”+key+“.json”,数据);
}
公共无效数据启动()
{
对于(int i=0;i我想我已经找到了答案。在数据读取器中,它加载并保存数据,我必须在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;
}
public void doSave(int键,字符串数据){
if(key==-1){System.IO.File.writealText(ApplicationData.Current.LocalFolder.Path+“\\config.json”,data);}
如果(key==0){System.IO.File.writealText(ApplicationData.Current.LocalFolder.Path+“\\global.json”,data);}
其他的
{
System.IO.File.writealText(ApplicationData.Current.LocalFolder.Path+“\\File”+key+“.json”,数据);
}
}
公共无效数据备份(int键、字符串数据){
System.IO.File.writealText(ApplicationData.Current.LocalFolder.Path+“\\backup”+key+“.json”,数据);
}
公共无效数据启动()
{
对于(int i=0;我能告诉我您是否正在使用WinJS开发的应用程序吗?如果没有,也许您可以调试window.UWPConnect对象是否存在,以及在JavaScript中调用getSaveFile方法是否返回预期值。我已经检查了,该对象确实存在,否则保存的文件将不会写入dis首先是k。另外,我有一个非常有趣的观察结果:当游戏返回主菜单时,读取和更新保存数据的代码被调用,似乎工作得很好。但是退出游戏后,它就不再工作了。解决了问题的一部分:我使用了localStorage.clear()在同步之前清空它(以防有人入侵Windows系统上的游戏存储并删除其中一个文件,以便游戏能够相应地运行)。不幸的是,这导致游戏中已经存在的数据被清除,而这些数据似乎没有按预期被替换或删除。快速更新。我注意到它至少有一半工作正常(全局变量至少被保留)但是,我希望找到一种方法来确保游戏保存也是如此。你能告诉我你是否正在使用WinJS开发的应用程序吗?如果没有,也许你可以调试window.UWPConnect对象是否存在,以及在JavaScript中调用getSaveFile方法是否返回预期值。我已经检查了,object事实上是存在的,否则保存文件就不会被写入磁盘。另外,我刚刚有一个非常有趣的观察:当游戏返回主菜单时,读取和更新保存数据的代码被调用,似乎工作得很好。但是退出游戏后,它就不再工作了。我发现了其中的一部分问题:我使用localStorage.clear()在同步之前清空它(以防有人入侵Windows系统上的游戏存储并删除其中一个文件,从而使游戏能够相应地运行)。不幸的是,这导致游戏中已经存在的数据被清除,而这些数据似乎没有按预期被替换或删除。快速更新。我注意到它至少有一半工作正常(全局变量至少被保留),但我希望找到一种方法来确保游戏中也存在同样的情况。