0
由於Xamarin PCL中沒有System.IO.File,我聽說編寫JSON文件問題的唯一方法就是使用Streams。但是,我還沒有找到一個很好的鏈接,因爲它很容易使用它們。此外,這是唯一的出路還是有其他方法可以幫助我以JSON格式編寫輸出。在Xamarin PCL中編寫JSON文件
由於Xamarin PCL中沒有System.IO.File,我聽說編寫JSON文件問題的唯一方法就是使用Streams。但是,我還沒有找到一個很好的鏈接,因爲它很容易使用它們。此外,這是唯一的出路還是有其他方法可以幫助我以JSON格式編寫輸出。在Xamarin PCL中編寫JSON文件
正如@Jason所說,您可以使用PclStorage。 否則,您可以使用DependencyService並在平臺特定的項目中編寫代碼。 你可以看看這個回購
這事爲Android
[assembly: Xamarin.Forms.Dependency(typeof(FilesImplementation))]
namespace TestReadFile.Droid
{
public class FilesImplementation : IFiles
{
public FilesImplementation()
{
}
public string ReadTextFile(string path, string fileName)
{
//throw new NotImplementedException();
using (System.IO.StreamReader sr = new System.IO.StreamReader(System.IO.Path.Combine(path, fileName))){
string line = sr.ReadToEnd();
sr.Close();
return line;
}
}
private string creaFileName(string directory, string fileName) {
string path = RootDirectory();
string file = System.IO.Path.Combine(path, fileName);
return file;
}
public void WriteTextFile(string path, string fileName, string stringToWrite)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(System.IO.Path.Combine(path, fileName),false))
{
sw.WriteLine(stringToWrite);
sw.Close();
}
}
public string RootDirectory()
{
File path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);
return path.AbsolutePath;
}
}
}
這是PCL接口
public interface IFiles
{
string ReadTextFile(string path, string fileName);
void WriteTextFile(string path, string filename, string stringToWrite);
string RootDirectory();
}
這取決於其配置文件PCL你使用。但是,您可以使用PCLStorage插件從PCL內提供File IO,或使用XF的DependencyService從平臺項目注入File IO方法。 – Jason