2017-06-22 32 views
0

我正在Xamarin.Forms(我猜Forms部分是不相關的)應用程序,並嘗試使用PCLStorage Xamarin插件來保存一個JSON字符串到文本文件。如果互聯網連接不可用,該文本文件將在那裏用於緩存數據。Xamarin PCLStorage不寫/讀文本文件

我將該插件添加到所有項目(PCL,iOS,Android,UWP)。我將下面的代碼添加到我的便攜式課程中。

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字符串時會調用這些方法。這一切似乎都非常直截了當(更不用說它的工作原理,只需使用我的原生Windows 10代碼就可以了)。嘗試並獲取JSON。如果沒有任何內容(沒有互聯網或API關閉),請從緩存中獲取JSON字符串。如果存在JSON,請將其寫入緩存。

我的假設是它並沒有真正寫入文件。我不知道實際檢查該文件內容的任何方式(不能只打開記事本並加載它)。

我打開了應用程序。我關閉了應用程序。我刪除了互聯網訪問。我打開應用程序。完成後,result爲空(result == "")。

+1

嗯,他們發明了一個調試器,完全是爲了這個目的:-) 只需在調試器中打開您的應用程序,並逐步通過您的代碼...不要做出「假設」 - 只需檢查它。 – Grisha

+0

我能在調試器中打開文本文件嗎? – doubleJ

+0

不,但你可以看到你有多少字節,你寫了多少,你有沒有例外,你是否覆蓋現有的文件。很多 - 而不是猜測。 – Grisha

回答

1

您的問題在您的代碼中有明確的評論。

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

//創建一個文件覆蓋所有現有文件

因此,如果該文件存在,你把它扔了,使一個新的。然後,您從新文件中讀取文本,並且沒有任何內容。您可能想要爲您的文件打開方法嘗試CreationCollisionOption.OpenIfExists。

+0

有趣...我使用的是PCLStorage文檔的直接複製/粘貼。 Hehehe ...也許他們沒有使用它,就像我一樣。 – doubleJ

+1

複製意大利麪的危險 –

+0

我只更改了'ReadTextFileAsync()'中的'ReplaceExisting'。我只想將最後一個成功的結果集緩存起來,所以在每次寫入之前我都會替換它。 – doubleJ