如何从 xamarin.ios 中的 TouchUpInside 函数显示 UIView?

How to show UIView from a TouchUpInside function in xamarin.ios?

我有一个名为 black_screen 的视图,其中包含一个 activity 指标。我在 viewDidLoad() 函数的开头隐藏了 UIView。现在我想在按下按钮调用名为 Submitt_TouchUpInside 的函数后将 Hidden 属性设置为 false。我的问题是显示 UIView 的代码在我的 submit() 函数执行完成之前不起作用,我不确定为什么。


    partial  void Submitt_TouchUpInside(UIButton sender)
        {

        try {
             
  var okCancelAlertController = UIAlertController.Create("", "Do you want to submit?", UIAlertControllerStyle.Alert);


                //Add Actions
                okCancelAlertController.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default,   Alert =>
                    {

                        
                        InvokeOnMainThread(() =>
                        {
                            

                            black_screen.Hidden = false;// << viewing the UIView here
                            indicator.StartAnimating();


                        });
                      
                            submit();// function that does post requests

                        
                        if (next_form == true)
                        {

//code to go to another view controller
                         
                        }



                           }));

                    //Add Actions
                    okCancelAlertController.AddAction(UIAlertAction.Create("No", UIAlertActionStyle.Default, alert => Console.WriteLine("test")));
                    // alert.SetCanceledOnTouchOutside(false);
                    PresentViewController(okCancelAlertController, true, null);
             

            }
            catch (Exception ex)
            {
                var t = Toast.ShowToast("Something went wrong");

            }



        }

这是我的提交功能代​​码:

   public  void  submit()
        {
          

           
            x = Convert.ToBase64String(datareturn());

            select_person_id();
            select_person_id2();
        
            if (CommonClass.counter == 0)
            {




                visits ob = new visits { visit_no = CommonClass.visit_count + 1 ,   visit_type = CommonClass.visit_type_id, visit_date = DateTime.Now, conclusion = "", BU_BA = "AMECA\ANE", site_id = CommonClass.site_id, source = CommonClass.visit_type_, conducted_by = idd3, QHSE_AF = "", active = true };
                BasePage.postData(ob, BasePage.GetUrl() + "api/insert_visits");

          //      Uri uri = new Uri(BasePage.postData(ob, BasePage.GetUrl() + "api/insert_visits");





                for (int i = 0; i <= (CommonClass.accomp_by_id.Count - 1); i++)
                {

                    visit_teams ob7 = new visit_teams { visit_no = CommonClass.visit_count +1,  visit_type = CommonClass.visit_type_id, team_id = 4, serial = i + 1, person_id = CommonClass.accomp_by_id[i] /*,  issue_id=select_issue_id() */ };
                    BasePage.postData(ob7, BasePage.GetUrl() + "api/insert_visit_teams"); 

                }


                visit_teams ob11 = new visit_teams { visit_no = CommonClass.visit_count +1,  visit_type = CommonClass.visit_type_id, team_id = 8, serial = 1, person_id = idd3/*, issue_id = select_issue_id()*/ };
                BasePage.postData(ob11, BasePage.GetUrl() + "api/insert_visit_teams"); 
             





            }



            for (int n = 0; n <= (fill_resp_id.Count - 1); n++)
            {

                visit_teams ob9 = new visit_teams
                {
                    visit_no = CommonClass.visit_count +1,
                     visit_type = CommonClass.visit_type_id
                    /*issue_id=select_issue_id()*/ ,
                    team_id = 5,
                    serial = CommonClass.counter + 1,
                    person_id = fill_resp_id[n]
                };
                BasePage.postData(ob9, BasePage.GetUrl() + "api/insert_visit_teams"); 

            }


            select_issue_id();
            select_area_id();

            string subc = "";

            additional_info ob3 = new additional_info { visit_no = CommonClass.visit_count +1, issue_id = issue_id, subcategory = "9", category = "9", level_ = level.CurrentTitle.ToString(), area_id = area_id,persons_met = pm.Text.ToString(), priority = priority.CurrentTitle.ToString(), activity = "", subcontractor = subcontractor.Text.ToString(),  visit_type = CommonClass.visit_type_id, photo = x.ToString(), observation = observation.Text.ToString(), type = type.CurrentTitle.ToString(), proposed_action = action.Text.ToString(), comp_date = (DateTime)dateb.Date, action_date = (DateTime)dateb.Date, serial = CommonClass.counter + 1, active = true };
            BasePage.postData(ob3, BasePage.GetUrl() + "api/insert_additional_info");

            CommonClass.resp_name.Clear();
            CommonClass.resp_data.Clear();
            CommonClass.accomp_by_id.Clear();
            CommonClass.ac_alert.Clear();
            CommonClass.ac_data.Clear();
            CommonClass.area_data.Clear();
            CommonClass.level_data.Clear();
            CommonClass.priority_data.Clear();
            CommonClass.issue_data.Clear();
            CommonClass.type_data.Clear();
            CommonClass.ac_alert.Clear();
            CommonClass.site_data.Clear();
            CommonClass.type_data.Clear();
            CommonClass.vd_data.Clear();
  
            x = "";
            fill_resp_name.Clear();
            fill_resp_id.Clear();
            next_form = true;
            CommonClass.counter++;

       

        }

我能够通过使我的 Submitt_TouchUpInside 方法异步并像这样使用 await 来使其工作:

await Task.Run(() =>
                        {
                            InvokeOnMainThread(() => {
                                black_screen.Hidden = false;
                                indicator.StartAnimating();
                            });

                        });