所以我用Xamarin在iOS中编写了一个下载程序,使用'WebClient'下载文件等。当用户添加要下载的新文件时,它将使用自定义单元格添加到UITableView中,并带有名称,大小和百分比之类的信息。但是我想知道随着下载进度的变化,如何更改文件特定单元格中的百分比?好吧,这是代码:
首先,我为下载创建了一个名为“ download”的类,以获取文件名,文件大小和百分比:
using System;
namespace iDownloader
{
internal class download
{
public string FileName
{
get;
set;
}
public Uri uri
{
get;
set;
}
public string FileSize
{
get;
set;
}
public double percentage
{
get;
set;
}
}
}
然后在这里,我得到了tableview的源代码:
using System;
using System.Collections.Generic;
using Foundation;
using UIKit;
namespace iDownloader
{
internal class downloadsTVS : UITableViewSource
{
private List<download> downloadFile;
public downloadsTVS(List<download> downloadFile)
{
this.downloadFile = downloadFile;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = (DownloadCell) tableView.DequeueReusableCell("Cell", indexPath);
var currentDownload = downloadFile[indexPath.Row];
cell.updateCell(currentDownload);
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return downloadFile.Count;
}
}
}
然后在这里,我有了tableviewcell的类:
using Foundation;
using System;
using UIKit;
namespace iDownloader
{
public partial class DownloadCell : UITableViewCell
{
public DownloadCell(IntPtr handle) : base(handle)
{
}
internal void updateCell(download currentDownload)
{
FileNameLabel.Text = currentDownload.FileName;
FileSizeLabel.Text = currentDownload.FileSize.ToString();
lblPercentage.Text = currentDownload.percentage.ToString();
}
}
}
这是视图控制器中的代码(我已经删除了不必要的部分): 在课程部分,我列出了下载列表(如果您记得,还提供了文件详细信息)以填充我的tablview。而且,我有我的网络客户端。
List<download> downloadFile = new List<download>
{
};
WebClient webClient = new WebClient();
然后是主要部分:
private async void DownloadAsync(Uri uri, string fileName)
{
downloadsint++;
TabBarItem.BadgeValue = downloadsint.ToString();
var fileNameandPath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName);
//UITableView cell adding
download download = new download();
download.FileName = fileName;
webClient = new WebClient();
webClient.OpenRead(uri.ToString());
Int64 totalbytes = System.Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]) / 1000000;
download.FileSize = totalbytes.ToString() + " MB";
downloadFile.Add(download);
tableview.Source = new downloadsTVS(downloadFile);
tableview.RowHeight = UITableView.AutomaticDimension;
tableview.EstimatedRowHeight = 70f;
tableview.ReloadData();
//UITableView cell adding
webClient = new WebClient();
await webClient.DownloadFileTaskAsync(uri, fileName);
btnPause.Enabled = true;
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged(fileName);
webClient.DownloadFileCompleted += DownloadFileCompleted(fileName);
}
public DownloadProgressChangedEventHandler WebClient_DownloadProgressChanged(string filename)
{
Action<object, DownloadProgressChangedEventArgs> action = (sender, e) =>
{
double received = e.BytesReceived;
double total = e.TotalBytesToReceive;
double percentage = received / total * 100;
download download = downloadFile.Find((download obj) => obj.FileName == filename);
downloadFile.Remove(download);
download.percentage = percentage;
downloadFile.Add(download);
tableview.Source = new downloadsTVS(downloadFile);
};
return new DownloadProgressChangedEventHandler(action);
}
public AsyncCompletedEventHandler DownloadFileCompleted(string filename)
{
Action<object, AsyncCompletedEventArgs> action = (sender, e) =>
{
if (e.Error.Message == "Request Aborted")
{
var alert = UIAlertController.Create("Download(s) has been cancelled.", null, UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create("Okay", UIAlertActionStyle.Cancel, null));
alert.View.Subviews[0].Subviews[0].Subviews[0].BackgroundColor = UIColor.DarkGray;
alert.View.TintColor = UIColor.White;
PresentViewController(alert, true, null);
}
if (e.Error != null)
{
var alert = UIAlertController.Create("The operation has faced a problem.", null, UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create("Okay", UIAlertActionStyle.Cancel, null));
alert.View.Subviews[0].Subviews[0].Subviews[0].BackgroundColor = UIColor.DarkGray;
alert.View.TintColor = UIColor.White;
PresentViewController(alert, true, null);
}
if (e.Cancelled)
{
var alert = UIAlertController.Create("The operation has been cancelled", null, UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create("Okay", UIAlertActionStyle.Cancel, null));
alert.View.Subviews[0].Subviews[0].Subviews[0].BackgroundColor = UIColor.DarkGray;
alert.View.TintColor = UIColor.White;
PresentViewController(alert, true, null);
}
else
{
downloadsint--;
TabBarItem.BadgeValue = downloadsint.ToString();
//Show finished downloades notification
var alert = UIAlertController.Create("File has been downloaded:", "'" + filename + "' has been downloaded!", UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create("Okay", UIAlertActionStyle.Cancel, null));
PresentViewController(alert, true, null);
//Show finished downloades notification
if (downloadsint == 0)
{
TabBarItem.BadgeValue = null;
btnPause.Enabled = false;
}
}
download file = new download()
{
FileName = filename
};
};
return new AsyncCompletedEventHandler(action);
}
所以现在这里出现了问题: 我尝试使用“ Lambada”将文件名传递给“ DownloadProgressChanged”,但找不到文件并将其百分比更改并添加到“ downloadFile”中无效(*请注意,我已经下载了多个文件因此,我希望它们的百分比也能得到更新。)最后,在“ DownloadFileCompeleted”中,我还尝试使用“ Lambada”做同样的事情,并找到文件名等于下载文件的位置,并在下载完成后删除该单元格。完成。
因此,总而言之,我需要解决以下问题: 1-更改下载文件的百分比(有多个文件正在下载。) 2-下载文件后删除单元格。
谢谢大家!!!感谢您的帮助。
答案 0 :(得分:0)
刷新UI(更新DownloadProgress和Delete单元格)的代码应该在主线程中,因此最好在主线程上调用它。
InvokeOnMainThread(() => {
//update tableviewcell
});