PCLStorage和二进制数据

时间:2014-01-10 08:17:18

标签: ios xamarin portable-class-library

我是这个PCL库中的新手,我正在开发一个带有Xamarin的iPhone应用程序,我找不到将其保存在手机上的方法。我最接近的是PCLStorage,但他只保存文本。 还有另一种方法可以用其他程序保存二进制文件。 谢谢。

foreach (images element in json_object)
{
            //var nameFile = Path.Combine (directoryname, element.name);
            try{
                IFile file = await folder_new.GetFileAsync(element.name);
            }catch(FileNotFoundException ex ){
                RestClient _Client = new RestClient(element.root);
                RestRequest request_file = new RestRequest("/images/{FileName}");
                request_file.AddParameter("FileName", element.name, ParameterType.UrlSegment);
                _Client.ExecuteAsync<MemoryStream>(
                        request_file,
                        async Response =>
                        {
                                if (Response != null)
                                {
                                    IFolder rootFolder_new = FileSystem.Current.LocalStorage;
                                    IFile file_new = await rootFolder_new.CreateFileAsync(element.name,CreationCollisionOption.ReplaceExisting);
                                    await file_new.WriteAllTextAsync(Response.Data);
                                }
                        });
            }
        }

1 个答案:

答案 0 :(得分:6)

使用IFile.OpenAsync方法获取可用于读取/写入二进制数据的流。以下是阅读文件的方法:

IFile file = await folder_new.GetFileAsync(element.name);
using (Stream stream = await file.OpenAsync(FileAccess.Read))
{
    //  Read stream and process binary data from it...

}