File Xamarin PCLStorage不是';t写入/读取文本文件
我正在开发一个Xamarin.Forms(我想表单部分是不相关的)应用程序,并尝试使用PCLStorage Xamarin插件将JSON字符串保存到文本文件中。如果internet连接不可用,文本文件将用于缓存数据 我将插件添加到所有项目(PCL、iOS、Android、UWP)。我在我的可移植类中添加了以下代码File Xamarin PCLStorage不是';t写入/读取文本文件,file,xamarin,xamarin.forms,storage,File,Xamarin,Xamarin.forms,Storage,我正在开发一个Xamarin.Forms(我想表单部分是不相关的)应用程序,并尝试使用PCLStorage Xamarin插件将JSON字符串保存到文本文件中。如果internet连接不可用,文本文件将用于缓存数据 我将插件添加到所有项目(PCL、iOS、Android、UWP)。我在我的可移植类中添加了以下代码 using PCLStorage; namespace DailyBibleReading { public class Helper { // re
using PCLStorage;
namespace DailyBibleReading
{
public class Helper
{
// read a text file from the app's local folder
public static async Task<string> ReadTextFileAsync(string _filename)
{
// declare an empty variable to be filled later
string result = null;
// see if the file exists
try
{
// get hold of the file system
IFolder rootFolder = FileSystem.Current.LocalStorage;
// create a folder, if one does not exist already
//IFolder folder = await rootFolder.CreateFolderAsync("DailyBibleReading", CreationCollisionOption.OpenIfExists);
// create a file, overwriting any existing file
IFile file = await rootFolder.CreateFileAsync(_filename, CreationCollisionOption.ReplaceExisting);
// populate the file with some text
result = await file.ReadAllTextAsync();
}
// if the file doesn't exist
catch (Exception ex)
{
// Output to debugger
Debug.WriteLine(ex);
}
// return the contents of the file
return result;
}
// write a text file to the app's local folder
public static async Task<string> WriteTextFileAsync(string _filename, string _content)
{
// declare an empty variable to be filled later
string result = null;
try
{
// get hold of the file system
IFolder rootFolder = FileSystem.Current.LocalStorage;
// create a folder, if one does not exist already
//IFolder folder = await rootFolder.CreateFolderAsync("DailyBibleReading", CreationCollisionOption.OpenIfExists);
// create a file, overwriting any existing file
IFile file = await rootFolder.CreateFileAsync(_filename, CreationCollisionOption.ReplaceExisting);
// populate the file with some text
await file.WriteAllTextAsync(_content);
result = _content;
}
// if there was a problem
catch (Exception ex)
{
// Output to debugger
Debug.WriteLine(ex);
}
// return the contents of the file
return result;
}
}
}
这一切看起来都很简单(更不用说,它在我的原生Windows10代码中工作得很好)。尝试获取JSON。如果没有任何内容(没有internet或API关闭),则从缓存中获取JSON字符串。如果存在JSON,则将其写入缓存
我的假设是它实际上并没有写入文件。我不知道有什么方法可以真正检查该文件的内容(不能只是打开记事本并加载它)
我打开应用程序。我关闭应用程序。我删除了互联网接入。我打开应用程序。完成所有操作后,
result
为空(result==“
)。您的代码中清楚地注释了该问题
// create a file overwriting any existing file
IFile file = await rootFolder.CreateFileAsync(_filename, CreationCollisionOption.ReplaceExisting);
//创建一个文件,覆盖任何现有文件
因此,如果该文件存在,则将其丢弃并创建一个新文件。然后从新文件中读取文本,但没有任何内容。您可能想尝试CreationCollisionOption.OpenIfExists来打开文件。他们发明了一个调试器,正是为了这个目的:-)只需在调试器中打开您的应用程序,然后逐步完成代码。。。不要做“假设”-只要检查它。我能在调试器中打开文本文件吗?不,但你可以看到你得到了多少字节,写了多少字节,是否有任何异常,以及是否覆盖了现有文件。很多-而不是猜测。@Grisha我得调查一下。我知道您可以查看变量定义,但我不知道您可以签出文件信息。很有趣。。。我使用的是来自PCLStorage文档的直接复制/粘贴。呵呵。。。也许他们没有像我一样使用它。复制pastaI的危险只在
ReadTextFileAsync()
中改变了ReplaceExisting
。我只希望缓存最后一个成功的结果集,因此我会在每次写入之前进行替换。
// create a file overwriting any existing file
IFile file = await rootFolder.CreateFileAsync(_filename, CreationCollisionOption.ReplaceExisting);