【发布时间】:2019-08-27 23:39:40
【问题描述】:
我使用 Visual Studio 编写了一个 Xamarin Android 应用程序来创建 Estimates。在不同的时间,我将 Estimate 保存在本地。如果您尝试重新打开 Estimate,它会尝试本地文件,如果它不存在,它会尝试服务器。 将 Estimate 成功保存到服务器后,我会删除本地文件。这是代码。
string filePath = Path.Combine(Settings.DocumentsPath, fileName);
JsonSerializer ser = new JsonSerializer();
StreamWriter opf = new StreamWriter(filePath, false);
try
{
ser.Serialize(opf, obj);
}
catch (Exception ex)
{
EventLog.Error(ex);
}
opf.Close();
Settings.DocumentsPath 设置如下:
private static string _DocumentsPath = null;
public static string DocumentsPath
{
get
{
_DocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
return _DocumentsPath;
}
}
当我加载 Estimate 时,我首先检查是否有本地副本,如果存在则加载该副本。否则它会调用服务器。当 Estimate 成功保存到服务器后,我使用以下代码删除本地文件:
string filePath = Path.Combine(Settings.DocumentsPath, FName);
if (File.Exists(filePath))
{
try
{
File.Delete(filePath);
}
catch (Exception ex)
{
EventLog.Error(ex);
}
}
这行得通。后续调用打开 Estimate 会尝试本地文件,但它不存在,因此请求是从服务器发出的。
问题是,在完成所有这些操作后,如果我清理我的项目并再次运行它,本地保存的 Estimate 的旧版本会重新显示。然后,当加载 Estimate 时,它会找到 Estimate 的旧本地版本,并且不会向服务器发送请求。我可以从平板电脑上完全卸载该应用程序并重新安装它,旧的本地文件又回来了。除了 Environment.SpecialFolder.Personal 之外,还有更好的地方来存储本地文件吗?如果在部署应用程序进行调试时,Visual Studio 中某处的设置会阻止它恢复这些本地文件?
【问题讨论】:
标签: android xamarin visual-studio-2019