gpt4 book ai didi

Xamarin-CrossDownloadManager - 等待下载文件

转载 作者:行者123 更新时间:2023-12-02 00:46:56 25 4
gpt4 key购买 nike

我使用 Xamarin-CrossDownloadManager ( https://github.com/SimonSimCity/Xamarin-CrossDownloadManager ),我需要等待下载文件。我有这段代码:

private static async Task<bool> FileDownloadedTest(string LinkToFile, string PathFile)
{
var downloadManager = CrossDownloadManager.Current;
CrossDownloadManager.Current.PathNameForDownloadedFile = new System.Func<IDownloadFile, string>(file => {
return PathFile;
});
{
await DeleteFile(PathFile);
var file = downloadManager.CreateDownloadFile(LinkToFile);
await Task.Run(() => downloadManager.Start(file, true)); //why this not wait???
}
bool FileExist = await IsFileExist(PathFile);
return FileExist;
}

为什么不等待完成下载操作?怎么做?

他们在图书馆网站上写道,我可以在下载文件时查看 IDownloadManager.Queue 以获取信息。但是,我不知道如何在我的方法中使用它...你能帮帮我吗?

PS:抱歉我的英语不好,我还在学习 ;)

最佳答案

使用该库,当文件下载完成时不会发布回调或事件,但您可以执行一个简单的检查并等待更多循环。

await Task.Run(async () =>
{
var downloadManager = CrossDownloadManager.Current;
var file = downloadManager.CreateDownloadFile(someFileBasedUrl);
downloadManager.Start(file);
bool isDownloading = true;
while (isDownloading)
{
await Task.Delay(10 * 1000);
isDownloading = IsDownloading(file);
}
});

IsDownloading 方法:

bool IsDownloading(IDownloadFile file)
{
if (file == null) return false;

switch (file.Status)
{
case DownloadFileStatus.INITIALIZED:
case DownloadFileStatus.PAUSED:
case DownloadFileStatus.PENDING:
case DownloadFileStatus.RUNNING:
return true;

case DownloadFileStatus.COMPLETED:
case DownloadFileStatus.CANCELED:
case DownloadFileStatus.FAILED:
return false;
default:
throw new ArgumentOutOfRangeException();
}
}

回复:https://github.com/SimonSimCity/Xamarin-CrossDownloadManager/blob/develop/Sample/DownloadExample/Downloader.cs#L46

关于Xamarin-CrossDownloadManager - 等待下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43008813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com