【发布时间】:2017-09-26 13:06:44
【问题描述】:
我正在使用 Xamarin 和 C# 构建一个 android 应用程序。 该应用程序使用 restsharp 连接到我的服务器并提取我需要的信息。
我正在尝试构建一个注册页面,并且我想检查用户是否存在。 我想在用户看到 ProgressDialog 时在后台执行此操作。
这是我的代码:
if (!string.IsNullOrEmpty(PhoneNumber) && !string.IsNullOrEmpty(Password)
&& !string.IsNullOrEmpty(LicenceId) && LicenceImage.Length > 1)
{
ProgressDialog mDialog = new ProgressDialog(this);
mDialog.SetMessage("Loading data...");
mDialog.SetCancelable(false);
mDialog.Show();
bool checkExistance = await api.CheckIfExist(PhoneNumber);
if (checkExistance)
{
Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(this);
alert.SetTitle("");
}
else
{
Intent intent = new Intent(this, typeof(RegisterDone));
StartActivity(intent);
}
}
ProgressDialog 显示,但没有发生任何事情。 我尝试过用其他方式来做,但仍然没有效果。
正确的方法是什么?提前谢谢
【问题讨论】:
-
小心异步,因为它会创建一个新线程并更改您需要在 UI 线程中的对话框。
-
您是否尝试过使用断点查看
api.CheckIfExist(PhoneNumber);是否运行? -
是的,thia 方法运行并返回一个布尔值。问题是服务器返回结果后什么也没发生
-
请尝试:await api.CheckIfExist(PhoneNumber).ConfigureAwait(false);
-
在 get 方法周围放置 try/catch 并在 catch 处设置断点,您可能会遇到某种异常,如果您没有遇到任何异常并且您想使用数据结果进行管理方法你应该使用 RunOnUiThread(()=> WhatEverFunction())
标签: c# android asynchronous xamarin