[英]Downloadprogress in ListView Xamarin Forms
我有一个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;
}
}
现在我解决了。 我不得不在MainThread上更新我的视图模型(因为GUI正在运行)。
我做了一个更新功能
void UpdateGUI(Item item, string newItemText){
item.Description = newItemText;
viewModel.DataStore.UpdateItemAsync(item);
viewModel.LoadItemsCommand.Execute(null);}
并这样调用它,我必须更新GUI:
Device.BeginInvokeOnMainThread(() => UpdateGUI(item, "newItemText..."));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.