【发布时间】:2017-11-26 19:09:24
【问题描述】:
我有一个 ListView(如 Xamarin Forms MasterDetails-Template),我想在其中显示项目的标题和下载进度(以百分比表示)。
我创建了一个从 url 下载数据的类。 在此类中,我使用 WebClient 下载文件并使用 downloadProgress-EventHandler 更改全局变量以获取百分比进度。
如何刷新我的 ListView 以百分比显示进度?
谢谢
public class DownloadHelper
{
string url;
string saveToPath;
string filename;
private string downloadError = "";
private int downloadProgress = -1;
private long totalBytesToReceive = -1;
private long bytesReceived = -1;
private long bytesThatRest = -1;
private bool downloadfinished = false;
public string DownloadError { get => downloadError;}
public int DownloadProgress { get => downloadProgress;}
public long TotalBytesToReceive { get => totalBytesToReceive;}
public long BytesReceived { get => bytesReceived;}
public long BytesThatRest { get => bytesThatRest;}
public bool Downloadfinished { get => downloadfinished;}
public DownloadHelper(string url, string saveToPath, string filename)
{
///Prepare a download.
this.url = url;
this.saveToPath = saveToPath;
this.filename = filename;
}
async public void DownloadZip()
{
///Download an Unzip a Zip-File in the saveToDirektory.
var filenameAndPath = Path.Combine(saveToPath, filename);
string foldernameAndPath = saveToPath.Replace(".zip", "");
if (Directory.Exists(foldernameAndPath))
{
Directory.Delete(foldernameAndPath, true);
}
Directory.CreateDirectory(foldernameAndPath);
var webClient = new WebClient();
var urlen = new Uri(url);
await webClient.DownloadFileTaskAsync(urlen, filenameAndPath);
webClient.DownloadProgressChanged += (s, e) =>
{
downloadProgress = e.ProgressPercentage;
totalBytesToReceive = e.TotalBytesToReceive;
bytesReceived = e.BytesReceived;
bytesThatRest = TotalBytesToReceive - BytesReceived;
};
webClient.DownloadFileCompleted += (s, e) =>
{
downloadfinished = true;
if (downloadProgress != 100)
{
downloadError = e.Error.Message.ToString();
}
else
{
//Unzip downloaded Zipfile
ZipFile.ExtractToDirectory(filenameAndPath, foldernameAndPath);
//Delete unused Zip-File
File.Delete(filenameAndPath);
}
};
}
}
public partial class DownloadedView : ContentPage, INotifyPropertyChanged
{
ItemsViewModel viewModel;
public DownloadedView(List<Item> items)
{
InitializeComponent();
BindingContext = viewModel = new ItemsViewModel(items);
}
void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
{
var item = (Item)((ListView)sender).SelectedItem;
string selectedItemName = item.Text;
switch (selectedItemName)
{
case "download1":
string downloadAddress = "";
string destinationFolder = "";
string filename = "";
break;
case "download2":
string downloadAddress = "";
string destinationFolder = "";
string filename = "";
break;
case "download3":
string downloadAddress = "";
string destinationFolder = "";
string filename = "";
break;
}
DownloadHelper dh = download(downloadAddress, destinationFolder, filename);
//UPDATE DOWNLOAD PROGRESS HERE.
//WITH: dh.DownloadProgress
//Deselect Item
((ListView)sender).SelectedItem = null;
}
DownloadHelper download(string downloadAddress, string destinationFolder, string filename)
{
DownloadHelper dh = new DownloadHelper(downloadAddress, destinationFolder, filename);
dh.DownloadZip();
return dh;
}
}
【问题讨论】:
-
如果数据源中的每个项目都有 PerecentComplete 属性,则只需更新该属性并使用 INotifyPropertyChanged 更新 UI
-
我试过了,但没用。你能给我举个例子吗?此外,我注意到,如果我按下后退按钮然后重新打开视图,视图将会更新。
标签: c# download xamarin.forms