关键词:
【中文标题】在 Xamarin Forms 中下载带有进度条的文件【英文标题】:Download file with progress bar in Xamarin Forms 【发布时间】:2015-08-18 13:32:33 【问题描述】:我正在尝试使用下载进度条在 Xamarin Forms(PCL,因此 WebClient 不可用)中创建一个下载页面。我使用了来自 Xamarin 的以下信息,但没有成功:
http://developer.xamarin.com/recipes/ios/network/web_requests/download_a_file/ http://developer.xamarin.com/recipes/cross-platform/networking/download_progress/
这是我当前的代码(带有工作进度条):
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using System.Net.Http;
using System.IO;
using System.Threading.Tasks;
namespace DownloadExample
public partial class DownloadPage : ContentPage
public DownloadPage ()
InitializeComponent ();
DownloadFile("https://upload.wikimedia.org/wikipedia/commons/3/3d/LARGE_elevation.jpg");
private async Task<long> DownloadFile(string url)
long receivedBytes = 0;
long totalBytes = 0;
HttpClient client = new HttpClient ();
using (var stream = await client.GetStreamAsync(url))
byte[] buffer = new byte[4096];
totalBytes = stream.Length;
for (;;)
int bytesRead = await stream.ReadAsync (buffer, 0, buffer.Length);
if (bytesRead == 0)
await Task.Yield ();
break;
receivedBytes += bytesRead;
int received = unchecked((int)receivedBytes);
int total = unchecked((int)totalBytes);
double percentage = ((float) received) / total;
progressBar1.Progress = percentage;
return receivedBytes;
现在,我需要将文件保存到本地存储。但是,在此示例中,我没有获取文件内容,因此无法将其写入本地存储。我需要在代码中进行哪些更改才能实现这一点?
仅供参考:在此示例中,我正在下载一个图像,但它将是一个 .pdf / .doc / .docx 的特征。
提前致谢。
BR、FG
【问题讨论】:
How to implement progress reporting for Portable HttpClient 的可能副本 你能分享你是如何保存文件的以及你是如何连接缓冲区的吗?... 【参考方案1】:想想你的文章,我也解决了下载文件
WebClient client = new WebClient();
using (var stream = await client.OpenReadTaskAsync(Download point))
using (MemoryStream ms = new MemoryStream())
var buffer = new byte[BufferSize];
int read = 0;
totalBytes = Int32.Parse(client.ResponseHeaders[HttpResponseHeader.ContentLength]);
while ((read = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, read);//important : receive every buffer
receivedBytes += read;
received = unchecked((int)receivedBytes);
total = unchecked((int)totalBytes);
percentage = ((float)received) / total;
progressBar1.Progress = percentage;
labProg.Text = AppResources.MsgDownloadprogress + Math.Truncate(percentage * 100).ToString() + "%";
//END while
Stream ALLstream = new MemoryStream(ms.ToArray());//important change Stream
App.APK.GenApkFile(ALLstream);//change APK file and save memory of phone
//END using (MemoryStream
stream.Close();
//END using (var stream
【讨论】:
【参考方案2】:您实际上将文件内容复制到 for
循环内的 buffer
中。将每次运行该循环的buffer
内容连接到一个新的byte[] fileContentBuffer
中,您可以访问可以保存在本地存储中的内容。
【讨论】:
在每个 TableViewcell 中播放带有进度条的音频
】在每个TableViewcell中播放带有进度条的音频【英文标题】:PlayingaudiowithprogressbarineachTableViewcell【发布时间】:2013-06-2410:04:38【问题描述】:有一个UITableView,我在我的每个单元格中放置了一个音频按钮,点击该按钮有一个进度... 查看详情
在 Xamarin.forms C# 中显示带有 JSON 数据的列表
】在Xamarin.formsC#中显示带有JSON数据的列表【英文标题】:ShowlistwithJSONdatainXamarin.formsC#【发布时间】:2017-01-2111:00:14【问题描述】:我想将JSON数据放在一个列表中以向用户显示记录。但是当我运行系统时,列表只显示了两次项目... 查看详情
如何在 Xamarin Forms iOS 中显示带有小型大写字体样式的标签?
】如何在XamarinFormsiOS中显示带有小型大写字体样式的标签?【英文标题】:HowtodisplayalabelwithsmallcapsfontstyleinXamarinFormsiOS?【发布时间】:2018-01-1617:26:58【问题描述】:我的XamarinForms应用程序设计师为标签字段指定了SmallCaps。在Xamar... 查看详情
在 Xamarin.Forms Shell 中隐藏 tabbedPage 的标题
】在Xamarin.FormsShell中隐藏tabbedPage的标题【英文标题】:HidetitleoftabbedPageinXamarin.FormsShell【发布时间】:2019-07-0414:53:31【问题描述】:我正在将一个项目重构为使用Xamarin.Forms4.0发布的带有shell的新导航,现在我正在迁移一个带有eff... 查看详情
使用 Xamarin Forms 在 UWP 中自定义进度条颜色
】使用XamarinForms在UWP中自定义进度条颜色【英文标题】:CustomizingtheprogressbarcolorinUWPusingXamarinForms【发布时间】:2017-11-2300:17:33【问题描述】:我进行了进度条自定义(自定义渲染器)以更改iOS和Droid中的进度条颜色,如下所示PCL... 查看详情
如何在 Xamarin.Forms 中访问每个平台中的公共下载文件夹
】如何在Xamarin.Forms中访问每个平台中的公共下载文件夹【英文标题】:HowtoaccesspublicdownloadfolderineveryplatforminXamarin.Forms【发布时间】:2017-01-2312:24:57【问题描述】:我需要创建一个备份服务,所以我打算在每个平台上保存SQLite数... 查看详情
xamarin.forms的activityindicator和progressbar比较
Xamarin.Forms的ActivityIndicator和ProgressBar比较 在Xamarin.Forms中,控件ActivityIndicator和ProgressBar都用来表示程序正在进行较长时间的操作,需要用户等待。虽然功能相同,但实际使用还是有差别的。 第一,对于不确定进度的操作... 查看详情
带有进度条的 Firebase 数据库
】带有进度条的Firebase数据库【英文标题】:Firebasedatabasewithprogressbar【发布时间】:2018-01-2706:05:45【问题描述】:我有问题。从Firebase数据库加载数据时,我无法将progressbar附加到我的活动中。我尝试通过Asynctask附加进度条,将fi... 查看详情
Xamarin.Forms - Webview(带有本地文件)在 Windows Phone 8.1 和 10 上不起作用
】Xamarin.Forms-Webview(带有本地文件)在WindowsPhone8.1和10上不起作用【英文标题】:Xamarin.Forms-Webview(withlocalfiles)notworkingonWindowsPhone8.1&10【发布时间】:2016-06-1607:18:46【问题描述】:我想在windowsphone8.1/10上使用WebView访问本地HTML文... 查看详情
如何在 Xamarin.Forms 中实现 INotifyPropertyChanged
】如何在Xamarin.Forms中实现INotifyPropertyChanged【英文标题】:HowtoimplementINotifyPropertyChangedinXamarin.Forms【发布时间】:2015-12-1612:16:29【问题描述】:我正在Xamarin.Forms中实现一个购物车。在我的购物车页面中有一个带有数据的ListView。... 查看详情
如何在 Android 11 Xamarin Forms 中下载和打开 PDF
】如何在Android11XamarinForms中下载和打开PDF【英文标题】:HowtodownloadandopenaPDFinAndroid11XamarinForms【发布时间】:2022-01-1906:23:19【问题描述】:在面向Android11之前,我可以使用PDF阅读器下载并打开该PDF。现在将PDF下载到应用特定的存... 查看详情
带有图像和圆形进度条的圆形按钮
】带有图像和圆形进度条的圆形按钮【英文标题】:Circularbuttonwithimageandcircularprogressbar【发布时间】:2019-12-2819:29:53【问题描述】:我正在尝试制作一个按钮,里面有一个图像,外面有一个圆形进度条,按下按钮后是彩色的。我... 查看详情
带有进度条的按钮 android
】带有进度条的按钮android【英文标题】:Buttonwithprogressbarandroid【发布时间】:2021-05-2910:54:36【问题描述】:我正在尝试在Android中创建一个带有进度条的自定义按钮。按钮应该有两种状态:正常和正在加载。在正常状态下它应该... 查看详情
在 xamarin Forms Android 中访问被拒绝的 PDF 文件下载问题
】在xamarinFormsAndroid中访问被拒绝的PDF文件下载问题【英文标题】:PDFFileDownloadIssueofAccessdeniedinxamarinFormsAndroid【发布时间】:2021-05-1015:07:04【问题描述】:我在AndroidManifest文件中拥有这些权限<uses-permissionandroid:name="android.permissi... 查看详情
kivy:带有进度条的弹出窗口
】kivy:带有进度条的弹出窗口【英文标题】:kivy:popupwithprogressbar【发布时间】:2014-09-2305:59:31【问题描述】:是否可以创建带有“移动”进度条的弹出窗口?这是我尝试过的一个示例,但它只会在循环完成后显示弹出窗口。我... 查看详情
如何使用 swift 显示带有进度条的 AVAudioPlayer?
】如何使用swift显示带有进度条的AVAudioPlayer?【英文标题】:HowtoAVAudioPlayerwithprogressbarshowusingswift?【发布时间】:2019-01-2413:26:17【问题描述】:我正在使用AVAudioPlayer播放歌曲。我需要一个进度条来显示歌曲的进度。我的问题是... 查看详情
Xamarin.Forms 将文件 (pdf) 保存在本地存储中并使用默认查看器打开
】Xamarin.Forms将文件(pdf)保存在本地存储中并使用默认查看器打开【英文标题】:Xamarin.Formssavefile(pdf)inlocalstorageandopenwiththedefaultviewer【发布时间】:2020-06-0904:19:54【问题描述】:已修改问题在Android中,我需要将pdf文件下载到“Doc... 查看详情
带有 Swing 进度条的多线程
】带有Swing进度条的多线程【英文标题】:MultithreadingwithSwingprogressbar【发布时间】:2015-05-2822:52:31【问题描述】:在执行RMI连接期间,我想不出如何让进度条响应,所以我决定向您寻求帮助。代码如下:ThreadperformLogin=newThread(newR... 查看详情