web-services - 异步方法中的 .Show() 上未显示进度对话框
问题描述
我有异步方法ExecuteWithRetryAsync
,它实现了一些重试逻辑,并且必须在调用时立即显示 ProgressDialog。目前,第一个电话Show()
从来没有真正奏效。进度对话框仅在AlertDialog
确认后显示(第二条评论)。如何Show()
在 ExecuteWithRetryAsync 开始时实际显示进度对话框?
public async Task<object> ExecuteWithRetryAsync(string methodName, object[] args)
{
MethodInfo methodInfo = typeof(Service1).GetMethod(methodName);
// below progress dialog not showing
mDialog = new ProgressDialog(context);
mDialog.SetMessage("Bitte warten...");
mDialog.SetCancelable(false);
mDialog.Show();
for (; ; )
{
try
{
object result = null;
try
{
// Call web service.
result = methodInfo?.Invoke(webservice, args);
}
catch (TargetInvocationException tie)
{
if (tie.InnerException != null) throw tie.InnerException;
}
mDialog?.Dismiss();
return result;
}
catch (Exception e)
{
Trace.TraceError("Operation Exception");
currentRetry++;
if (/*currentRetry > RetryCount || */!IsTransient(e))
{
// If this isn't a transient error or we shouldn't retry,
// rethrow the exception.
throw;
}
}
mDialog?.Dismiss();
await DisplayAlert(
context.GetString(Resource.String.timeout),
context.GetString(Resource.String.retry_operation),
context.GetString(Resource.String.Ok),
methodInfo);
// this progress dialog is showing
mDialog = new ProgressDialog(context);
mDialog.SetMessage("Bitte warten...");
mDialog.SetCancelable(false);
mDialog.Show();
await Task.Delay(MaxDelayMilliseconds);
}
}
更新:我观察到,当设备连接被禁用时,大约需要 10-15 秒ExecuteWithRetryAsync
才能开始执行,同时设备多次显示应用程序没有响应对话框,而连接上它会立即执行。为什么?
更新 2:当我await Task.Delay (50)
调用 Show() 后,它确实显示了,但进度对话框动画没有更新,它被冻结了。
更新 3:我result = methodInfo?.Invoke(Utility.WsHueckmann, args)
在 await Task.Run 中放入以下行,这样它就变成await Task.Run(() => { result = methodInfo?.Invoke(Utility.WsHueckmann, args); })
了,现在它正在工作并且微调器正在更新。
解决方案
您的进度不旋转背后的原因是因为它不是不确定的添加以下代码并且它应该可以工作
progress.Indeterminate = true;
progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner);
更新
放入以下行
result = methodInfo?.Invoke(Utility.WsHueckmann, args)
在 await Task.Run 里面,它变成了
await Task.Run(() => { result = methodInfo?.Invoke(Utility.WsHueckmann, args); })
现在它正在工作并且微调器正在更新。
推荐阅读
- ruby-on-rails - 使用 capybara selenium 测试 vuetify on rails 项目
- python - Keras 序列序列
- mysql - 如何获取多个ID的子查询中的计数最大的行
- git - 在 Ansible 中将 git 存储库克隆到本地计算机
- android - 如何在 Android 上迭代 Firestore 集合?
- google-cloud-platform - 多个 Cloud SQL 代理
- c# - c# 导出到 excel 的 openxml 数据表在 windows 10 office 2019 的电子表格比较器实用程序中使用时导致修复
- sql - SQL Server 根据文件格式的顺序更新重复的文件记录
- javascript - Bootstrip 4工具提示位置:自动向右
- javascript - 如何在 gatsby 构建的项目中获取最新的 cms 帖子数据?