在Xamarin上添加Asset dynamiccaly

时间:2018-04-10 08:48:22

标签: android xamarin

我从Xamarin开始,我正在玩资产。 我想做的是一个可以读取xml文件的小应用程序。

如果文件在APK本机中,则没有问题,但是,我要做的是能够读取我从互联网上下载的文件。 我应该把文件放在哪里?我该如何访问它?

目的是创建一个应用程序,用户可以在其中下载“书”并随时阅读。 奖励点,该文件只能从应用程序访问:)

谢谢

1 个答案:

答案 0 :(得分:0)

下载文件后,应将其保存到设备:

string path = Application.Context.FilesDir.Path;
var filePath = Path.Combine(path, "Nineteen-Eighty-Four.xml");
System.IO.File.WriteAllText(filePath, contentOfYourXmlFile);

然后您可以按如下方式阅读文件内容:

System.IO.File.ReadAllText(filePath);

要跟踪文件/书籍的路径,您还需要存储它们。您可以将它们存储在Preferences中,但我建议将它们存储在本地数据库中。您可以阅读有关Xamarin Android here的不同选项。您可以从SQLite中创建一个简单的表开始,如下所示:

public class Book
{
     [PrimaryKey, AutoIncrement]
     public int ID { get; set; }

     public string Name { get; set; }

     public string Author { get; set; }

     public string FilePath { get; set; }
}

请在这里查看完整的Android SQLite示例:Create a Database with SQLite

保存/阅读文件的上述示例来自Data Storage Using Xamarin。上面的示例将文件写入只能为您的应用程序访问的位置。