如何在响应后应用警报对话框

问题描述 投票:1回答:1
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灯中工作我的项目

实际上这就是我正在做的如果我的回答是错误的我试图显示用户不存在的警告对话框我在MVVM灯中工作我的项目

实际上这就是我正在做的如果我的回答是错误的我试图显示用户不存在的警告对话框我在MVVM灯中工作我的项目

xamarin.android
1个回答
1
投票

通常,API调用是使用async-await在后台线程中进行的,如果你也是这样,那么我建议你在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();

你应该做的是使用lambda表达式来表示匿名方法:

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();});
}
© www.soinside.com 2019 - 2024. All rights reserved.