Xamarin PCLStorage 不是 Writing/Reading 文本文件

Xamarin PCLStorage Isn't Writing/Reading A Text File

我正在开发 Xamarin.Forms(我想表单部分无关紧要)应用程序并尝试使用 PCLStorage Xamarin 插件将 JSON 字符串保存到文本文件。如果互联网连接不可用,文本文件将用于缓存数据。

我将插件添加到所有项目(PCL、iOS、Android、UWP)。我将以下代码添加到我的便携式 class.

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

您可能会注意到 CreateFolderAsync 部分被注释掉了。我觉得不需要在应用程序的文件夹中创建另一个文件夹。不过,我确实尝试了两种方法,以防万一这是一项要求。两种方法都行不通。您可能还会注意到 LocalStorage 正在使用中。我也试了RemoteStorage,结果一样。

我在获得 JSON 字符串时调用这些方法。

string result = null;

// make the api call
result = await Helper.GetHttpStringAsync("http://www.anti-exe.com/api/dailybiblereading?begindate=" + _begindate + "&enddate=" + _enddate + "&version=" + _version);

string textfile = "ApiResults.txt";
// if there is no content
if (result == null || result == "" || result.Contains("SQLSTATE") == true)
{
    // read content from a pre-existing file
    result = await Helper.ReadTextFileAsync(textfile);
    // Output to debugger
    Debug.WriteLine("Can't connect to the API." + "\r\n" + "Loading from local cache.");
}
else
{
    // write to a cache file
    await Helper.WriteTextFileAsync(textfile, result);
}

这一切看起来都非常简单(更不用说它可以正常工作,使用我的原生 Windows 10 代码)。尝试获取 JSON。如果什么都没有(没有互联网或 API 已关闭),请从缓存中获取 JSON 字符串。如果JSON存在,则将其写入缓存。

我的假设是它实际上并没有写入文件。我不知道有什么方法可以实际检查该文件的内容(不能只打开记事本并加载它)。

我打开应用程序。我关闭应用程序。我删除了互联网访问权限。我打开应用程序。全部完成后,result 为空 (result == "")。

您在代码中清楚地注释了问题。

// create a file overwriting any existing file
IFile file = await rootFolder.CreateFileAsync(_filename, CreationCollisionOption.ReplaceExisting);

// 创建文件 覆盖任何现有文件

因此,如果该文件存在,您将丢弃它并制作一个新文件。然后您从新文件中读取文本,但那里什么也没有。您可能想尝试 CreationCollisionOption.OpenIfExists 文件打开方法。