public async Task<ObservableCollection<CustomerModel>> GetCustomer(string customerNumber, string department)
{
try
{
progressBar.Visibility = ViewStates.Visible;
progressBar.Progress = 0;
listofItems = new ObservableCollection<CustomerModel>();
string url = _client.BaseAddress + "/getcustomers(Number='" + customerNumber + "',department='" +
department + "')";
var response = await _client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
progressBar.Visibility = ViewStates.Invisible;
progressBar.Progress = 100;
string returnjson = await response.Content.ReadAsStringAsync();
ReplyCustomerModel replyCustomerModel =
JsonConvert.DeserializeObject<ReplyCustomerModel>(returnjson);
if (replyCustomerModel != null)
{
listofItems = replyCustomerModel.Customers;
}
}
else
{
AlertDialog.Builder alertDiag = new AlertDialog.Builder();
alertDiag.SetTitle("Butikscanner App");
alertDiag.SetMessage("User Does not exist");
alertDiag.SetPositiveButton("OK",
(senderAlert, args) => { });
alertDiag.SetNegativeButton("Cancel", (senderAlert, args) => { alertDiag.Dispose(); });
Dialog diag = alertDiag.Create();
diag.Show();
}
return listofItems;
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
实际上,如果响应为假,这就是我正在做的事情,我试图显示不存在用户的警报对话框,我正在MVVM light中运行我的项目
实际上,如果响应为假,这就是我正在做的事情,我试图显示不存在用户的警报对话框,我正在MVVM light中运行我的项目
实际上,如果响应为假,这就是我正在做的事情,我试图显示不存在用户的警报对话框,我正在MVVM light中运行我的项目
答案 0 :(得分:1)
通常,如果您也是这种情况,则使用async-await
在后台线程中进行API调用,那么我建议您这样做是在UIThread上调用对话框的show方法。为此,您将需要一个活动上下文,即活动的引用。
您可以通过以下两种方法来执行此操作:直接将这种方法作为操作来调用,例如:
private void ShowDialog()
{
AlertDialog.Builder alertDiag = new AlertDialog.Builder();
alertDiag.SetTitle("Butikscanner App");
alertDiag.SetMessage("User Does not exist");
alertDiag.SetPositiveButton("OK",(senderAlert, args) => { });
alertDiag.SetNegativeButton("Cancel", (senderAlert, args) => { alertDiag.Dispose(); });
Dialog diag = alertDiag.Create();
diag.Show();
}
假设上面是您的方法定义方式,您可以在UI线程上运行它,如:
activity.RunOnUIThread(ShowDialog);
但是在您的情况下,我个人认为这不是一件明智的事情,因为应该在UIThread上的唯一代码行(至少我认为是)是dialog.Show();
>
您应该做的是对匿名方法使用lamba表达式,例如:
private void ShowDialog(Activity activity)
{
AlertDialog.Builder alertDiag = new AlertDialog.Builder();
alertDiag.SetTitle("Butikscanner App");
alertDiag.SetMessage("User Does not exist");
alertDiag.SetPositiveButton("OK",(senderAlert, args) => { });
alertDiag.SetNegativeButton("Cancel", (senderAlert, args) => { alertDiag.Dispose(); });
Dialog diag = alertDiag.Create();
activity.RunOnUIThread(()=>
{diag.Show();});
}