Ich arbeite an einer Xamarin.Forms (ich denke die Forms Teil ist irrelevant) App und versuche das PCLStorage Xamarin Plugin zu verwenden, um eine JSON Zeichenfolge in einer Textdatei zu speichern . Die Textdatei wird für zwischengespeicherte Daten gespeichert, falls keine Internetverbindung verfügbar ist.Xamarin PCLStorage schreibt/liest keine Textdatei
Ich habe das Plugin zu allen Projekten hinzugefügt (PCL, iOS, Android, UWP). Ich habe den folgenden Code zu meiner tragbaren Klasse hinzugefügt.
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;
}
}
}
Sie können feststellen, dass die CreateFolderAsync
Abschnitte kommentiert werden. Ich hatte nicht das Bedürfnis, einen anderen Ordner im App-Ordner zu erstellen. Ich habe es in beide Richtungen versucht, nur für den Fall, dass es eine Voraussetzung war. Keineswegs hat funktioniert. Möglicherweise stellen Sie auch fest, dass LocalStorage
verwendet wird. Ich habe auch versucht RemoteStorage
, mit den gleichen Ergebnissen.
Ich mache Aufrufe an diese Methoden, wenn ich die JSON-Zeichenfolge abrufen.
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);
}
Es scheint alles ziemlich einfach (nicht zu erwähnen, dass es funktioniert, ganz gut, mit meinem nativen Windows 10-Code). Versuchen Sie und erhalten Sie den JSON. Wenn nichts vorhanden ist (kein Internet oder API ist ausgefallen), greifen Sie den JSON-String aus dem Cache. Wenn JSON da ist, schreibe es in den Cache.
Meine Annahme ist, dass es nicht tatsächlich in die Datei schreibt. Ich kenne keine Möglichkeit, den Inhalt dieser Datei tatsächlich zu überprüfen (ich kann Notepad nicht einfach öffnen und laden).
Ich öffne die App. Ich schließe die App. Ich entferne den Internetzugang. Ich öffne die App. Wenn alles fertig ist, ist result
leer (result == ""
).
Nun, sie haben einen Debugger genau für diesen Zweck erfunden :-) Öffnen Sie einfach Ihre App im Debugger und gehen Sie Schritt für Schritt durch Ihren Code ... Machen Sie keine "Annahmen" - überprüfen Sie es einfach. – Grisha
Kann ich die Textdatei im Debugger öffnen? – doubleJ
Nein, aber Sie können dort sehen, wie viele Bytes Sie bekommen haben und wie viele Sie schreiben, und haben Sie Ausnahmen, und ob Sie bestehende Dateien überschreiben oder nicht. Viel - anstatt zu raten. – Grisha