gpt4 book ai didi

android - MonoDroid 进度对话框

转载 作者:搜寻专家 更新时间:2023-11-01 07:37:34 24 4
gpt4 key购买 nike

我想做的就是确保用户知道网络服务正在处理,这样他们就不会在它尝试工作时重复混合按钮。

        EditText partnumber = (EditText)FindViewById(Resource.Id.itemNumber);
partnumber.FocusChange += (object sender, View.FocusChangeEventArgs e) =>
{
if (!e.HasFocus)
{
var pd = ProgressDialog.Show(this, "Processing", "Please Wait...", false);
var res = new InventoryApp();
res.partValidateCompleted += delegate { pd.Hide(); };
var isValid = res.partValidate(partnumber.Text);
if (isValid == "Not Found")
{
partnumber.Text = "";
partnumber.RequestFocus();
partqty.ClearFocus();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle("Invalid Part");
builder.SetMessage("Part number does not exist in database. Please ensure you are entering the correct part number and try again");
builder.SetPositiveButton("OK", delegate { });
var dialog = builder.Create();
dialog.Show();
}
else
{
partdesc.Text = isValid;
}
}
};

如果我将 pd.Hide() 留在代码中,那么我将永远看不到进度对话框。如果我把它拿出来,我只会在 web 服务返回它的结果后看到进度对话框,然后因为我没有把它隐藏在任何地方而保持不变。如何在应用等待 partValidate 时显示它以及何时可以隐藏它?

最佳答案

从您的代码来看,partValidate() 方法似乎是同步的。由于此代码是在 UI 线程上执行的,因此应用程序的 UI 将在该方法调用期间被阻塞,这可以解释为什么在它完成之前您看不到任何 UI 更新。您可以通过在后台线程上运行调用来解决此问题,然后在需要更新 UI 时返回 UI 线程:

partnumber.FocusChange += (object sender, View.FocusChangeEventArgs e) =>
{
if (!e.HasFocus)
{
var pd = ProgressDialog.Show(this, "Processing", "Please Wait...", false);
var res = new InventoryApp();
res.partValidateCompleted += delegate { pd.Hide(); };

ThreadPool.QueueUserWorkItem(state =>
{
var isValid = res.partValidate(partnumber.Text);

RunOnUiThread(() =>
{
if (isValid == "Not Found")
{
partnumber.Text = "";
partnumber.RequestFocus();
partqty.ClearFocus();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle("Invalid Part");
builder.SetMessage("Part number does not exist in database. Please ensure you are entering the correct part number and try again");
builder.SetPositiveButton("OK", delegate { });
var dialog = builder.Create();
dialog.Show();
}
else
{
partdesc.Text = isValid;
}
}
}
}
};

我有一篇博文 here解释了在 Android 应用程序的 Mono 上处理这种情况的各种方法。

关于android - MonoDroid 进度对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8661142/

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