Xamarin.android 如何在响应后应用警报对话框 公共异步任务GetCustomer(字符串customerNumber,字符串部门) { 尝试 { progressBar.Visibility=ViewStates.Visible; progressBar.Progress=0; listofItems=新的ObservableCollection(); 字符串url=_client.BaseAddress+“/getcustomers(数字=”+customerNumber+“,部门=”)+ 部门+“)”; var response=await\u client.GetAsync(url); if(响应。IsSuccessStatusCode) { progressBar.Visibility=ViewStates.Invisible; 进度条。进度=100; string returnjson=await response.Content.ReadAsStringAsync(); ReplyCustomerModel ReplyCustomerModel= 反序列化对象(returnjson); if(replyCustomerModel!=null) { listofItems=replyCustomerModel.Customers; } } 其他的 { AlertDialog.Builder alertDiag=新建AlertDialog.Builder(); alertDiag.SetTitle(“Butikscanner应用程序”); alertDiag.SetMessage(“用户不存在”); alertDiag.SetPositiveButton(“确定”, (senderAlert,args)=>{}); alertDiag.SetNegativeButton(“取消”,(senderAlert,args)=>{alertDiag.Dispose();}); Dialog diag=alertDiag.Create(); diag.Show(); } 返回项目列表; } 捕获(例外情况除外) { 控制台写入线(ex); 投掷; } }

Xamarin.android 如何在响应后应用警报对话框 公共异步任务GetCustomer(字符串customerNumber,字符串部门) { 尝试 { progressBar.Visibility=ViewStates.Visible; progressBar.Progress=0; listofItems=新的ObservableCollection(); 字符串url=_client.BaseAddress+“/getcustomers(数字=”+customerNumber+“,部门=”)+ 部门+“)”; var response=await\u client.GetAsync(url); if(响应。IsSuccessStatusCode) { progressBar.Visibility=ViewStates.Invisible; 进度条。进度=100; string returnjson=await response.Content.ReadAsStringAsync(); ReplyCustomerModel ReplyCustomerModel= 反序列化对象(returnjson); if(replyCustomerModel!=null) { listofItems=replyCustomerModel.Customers; } } 其他的 { AlertDialog.Builder alertDiag=新建AlertDialog.Builder(); alertDiag.SetTitle(“Butikscanner应用程序”); alertDiag.SetMessage(“用户不存在”); alertDiag.SetPositiveButton(“确定”, (senderAlert,args)=>{}); alertDiag.SetNegativeButton(“取消”,(senderAlert,args)=>{alertDiag.Dispose();}); Dialog diag=alertDiag.Create(); diag.Show(); } 返回项目列表; } 捕获(例外情况除外) { 控制台写入线(ex); 投掷; } },xamarin.android,Xamarin.android,事实上,这就是我所做的,如果我的回答是错误的,我试图显示警告对话框,用户不存在,我在MVVM灯光下工作我的项目 事实上,这就是我所做的,如果我的回答是错误的,我试图显示警告对话框,用户不存在,我在MVVM灯光下工作我的项目 实际上,如果我的回答是错误的,我就是这么做的。我试图显示一个警告对话框,提示用户不存在。我在MVVM灯光下工作我的项目通常,API调用是在后台线程中使用async wait进行的,如果您也是这样,那么我建议您在UIThread上调用对话框的show方法。为此,您需要活动上下文

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

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


实际上,如果我的回答是错误的,我就是这么做的。我试图显示一个警告对话框,提示用户不存在。我在MVVM灯光下工作我的项目通常,API调用是在后台线程中使用
async wait
进行的,如果您也是这样,那么我建议您在UIThread上调用对话框的show方法。为此,您需要活动上下文,即活动的引用

有两种方法可以直接调用此方法作为操作,如下所示:

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;
            }
        }
假设上述方法是如何定义的,您可以在UI线程上运行它,如:

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();    
}
但在您的场景中,我个人并不认为这是一件明智的事情,因为UIThread上唯一应该出现的代码行(至少我认为是这样)是
dialog.Show()

您应该做的是为匿名方法使用lamba表达式,例如:

activity.RunOnUIThread(ShowDialog);

这取决于你如何调用你的API,你会分享帽子的部分吗?
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();});
}