【发布时间】:2022-02-10 23:01:54
【问题描述】:
我正在尝试显示一个进度对话框,显示使用等待所有内容的方法完成的百分比 + await Task.Delay(20); 和等待我要执行的方法。现在我注意到使用 task.delay 执行需要更长的时间。
我想要实现的是 progress.dialog 计算该方法需要多长时间而不是延迟它,因为它的工作速度要慢一些。
我有哪些选择?
这是我的代码
private async Task DownloadAllAlert()
{
//alert to download everything
bool result = await DisplayAlert("Download", "Do you want to download everything?", "Yes", "No"); ;
//alert is user chose yes
if (result)
{
// loading dialog in percentage till downloading is done
using (var progress = UserDialogs.Instance.Progress("Loading..."))
{
for (var i = 0; i < 100; i++)
{
progress.PercentComplete = i;
await Api.DownloadAll();
await Task.Delay(20);
}
}
}
}
【问题讨论】:
-
您拨打
Api.DownloadAll()100 次 - 您确定这是您想要做的吗?另外:Now I notice that with that task.delay the execution takes much longer.- 这就是Task.Delay的设计目的。 -
哦,我的错,但如果我将其设置为 1,则对话框在方法结束之前结束@FranzGleichmann
标签: c# xamarin async-await android-asynctask mvvmcross