Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin.forms Xamarin Forms:是否可以使用SettingsPlugin保存和检索电子书数据?_Xamarin.forms_Epub - Fatal编程技术网

Xamarin.forms Xamarin Forms:是否可以使用SettingsPlugin保存和检索电子书数据?

Xamarin.forms Xamarin Forms:是否可以使用SettingsPlugin保存和检索电子书数据?,xamarin.forms,epub,Xamarin.forms,Epub,我正在使用将值保存到本地数据库。使用此插件,我保存了一个EpubBook数据,如下所示: CrossSettings.Current.AddOrUpdateValue("epub", epubBook.ToString()); string epubString = CrossSettings.Current.GetValueOrDefault("epub", string.Empty); if (!string.IsNullOrWhiteSpace(e

我正在使用将值保存到本地数据库。使用此插件,我保存了一个EpubBook数据,如下所示:

CrossSettings.Current.AddOrUpdateValue("epub", epubBook.ToString());
string epubString = CrossSettings.Current.GetValueOrDefault("epub", string.Empty);
if (!string.IsNullOrWhiteSpace(epubString))
{
    //Need to convert string epub data to EpubBook data
}
检索的值如下所示:

CrossSettings.Current.AddOrUpdateValue("epub", epubBook.ToString());
string epubString = CrossSettings.Current.GetValueOrDefault("epub", string.Empty);
if (!string.IsNullOrWhiteSpace(epubString))
{
    //Need to convert string epub data to EpubBook data
}
我需要将字符串epub数据转换为EpubBook?这可能吗?这用于缓存EpubBook数据

我使用
Application.Current.Properties
也做了同样的工作,在那里保存和检索工作正常。请参阅以下代码:

//Saving value
Application.Current.Properties["epub"] = epubBook;
//Retriving value
EpubBook epubBook = (EpubBook)Application.Current.Properties["epub"];
但是
Application.Current.Properties
值在关闭应用程序后会自动清除。这就是为什么我要开始做
设置plugin
。那么,是否有任何方法可以使用
SettingsPlugin
保存和检索EpubBook?如果否,建议缓存EpubBook数据的方法?

请从my中回答

尝试使用http客户端请求数据,以便直接获取字节:

bool hasKey = Preferences.ContainsKey("epub");
if (hasKey)
{
    string fileName = Preferences.Get("epub", string.Empty);
    var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    var filePath = Path.Combine(folderPath, fileName);
    var bytes = File.ReadAllBytes(filePath);
    // convert
    var stream1 = new MemoryStream(bytes);
    epubBook = EpubReader.ReadBook(stream1);
}
else
{
    Stream stream;
    //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileUrl);
    //HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    HttpClient client = new HttpClient();
    var response = await client.GetAsync(fileUrl);

    //stream = response.GetResponseStream();
    stream = await response.Content.ReadAsStreamAsync();
    epubBook = EpubReader.ReadBook(stream);

    //saving to folder
    byte[] bytes = await response.Content.ReadAsByteArrayAsync();
    string filename = Path.GetFileName(fileUrl);
    var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    var filePath = Path.Combine(folderPath, filename);
    File.WriteAllBytes(filePath, bytes);
    Preferences.Set("epub", filename);           
}
我们需要在Android上添加明文:

<application android:label="EPubDemo.Android" android:usesCleartextTraffic="true"></application>

并在iOS ss中禁用ATS。此请求为http

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
回答我的问题

尝试使用http客户端请求数据,以便直接获取字节:

bool hasKey = Preferences.ContainsKey("epub");
if (hasKey)
{
    string fileName = Preferences.Get("epub", string.Empty);
    var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    var filePath = Path.Combine(folderPath, fileName);
    var bytes = File.ReadAllBytes(filePath);
    // convert
    var stream1 = new MemoryStream(bytes);
    epubBook = EpubReader.ReadBook(stream1);
}
else
{
    Stream stream;
    //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileUrl);
    //HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    HttpClient client = new HttpClient();
    var response = await client.GetAsync(fileUrl);

    //stream = response.GetResponseStream();
    stream = await response.Content.ReadAsStreamAsync();
    epubBook = EpubReader.ReadBook(stream);

    //saving to folder
    byte[] bytes = await response.Content.ReadAsByteArrayAsync();
    string filename = Path.GetFileName(fileUrl);
    var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    var filePath = Path.Combine(folderPath, filename);
    File.WriteAllBytes(filePath, bytes);
    Preferences.Set("epub", filename);           
}
我们需要在Android上添加明文:

<application android:label="EPubDemo.Android" android:usesCleartextTraffic="true"></application>

并在iOS ss中禁用ATS。此请求为http

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads

为什么?为什么不直接将其写入文件系统?一个epub可以是数百KB,也可以是数百MB。为什么要尝试使用设置保存它们?@Jason当前我的epub是一个URL,我总是从URL解析epub章节。首先,将URL转换为流,然后再转换为epubbook。这种方法太慢了,所以请提出解决方案?这仍然不能解释为什么不将数据写入文件系统为什么?为什么不直接将其写入文件系统?一个epub可以是数百KB,也可以是数百MB。为什么要尝试使用设置保存它们?@Jason当前我的epub是一个URL,我总是从URL解析epub章节。首先,将URL转换为流,然后再转换为epubbook。这种方法太慢了,所以请建议一种解决方案?这仍然不能解释为什么不将数据写入文件系统