如何从非异步方法调用异步方法?

时间:2016-10-24 09:22:51

标签: c# xamarin xamarin.ios xamarin.android xamarin.forms

我正在使用xamarin表格。我试图在日历控件中绑定Web服务。请参阅以下日历控件链接(XamForms.Controls.Calendar)。

https://github.com/rebeccaXam/XamForms.Controls.Calendar

第一个功能是创建7 * 6 = 42个标签和按钮,然后使用“callWebService”方法调用服务函数,这是异步方法从服务获取响应。

protected void FillCalendarWindows()
        {
            try
            {
                for (int r = 0; r < 6; r++)
                {
                    for (int c = 0; c < 7; c++)
                    {
                        if (r == 0)
                        {
                            labels.Add(new Label
                            {
                                HorizontalOptions = LayoutOptions.Center,
                                VerticalOptions = LayoutOptions.Center,
                                TextColor = Color.Black,
                                FontSize = 18,
                                FontAttributes = FontAttributes.Bold
                            });
                            DayLabels.Children.Add(labels.Last(), c, r);
                        }
                        buttons.Add(new CalendarButton
                        {
                            BorderRadius = 0,
                            BorderWidth = BorderWidth,
                            BorderColor = BorderColor,
                            FontSize = DatesFontSize,
                            BackgroundColor = DatesBackgroundColor,
                            HorizontalOptions = LayoutOptions.FillAndExpand,
                            VerticalOptions = LayoutOptions.FillAndExpand
                        });
                        buttons.Last().Clicked += DateClickedEvent;
                        MainCalendar.Children.Add(buttons.Last(), c, r);
                    }
                }
                flag = 1;
                //Device.BeginInvokeOnMainThread(() => CallWebService(StartDate.Month, StartDate.Year));
                CallWebService(StartDate.Month, StartDate.Year);
                //CallServiceInNewTask(StartDate.Month, StartDate.Year);
                //Device.BeginInvokeOnMainThread(() => ChangeCalendar(CalandarChanges.All));

            }
            catch (Exception e)
            {

            }
        }

第二个函数是“callWebService”函数,我在列表集合对象中收集响应然后调用“ChangeClaendar”函数,该函数用于绑定标签和按钮文本并填充适当的颜色。

public async void CallWebService(int Month, int Year)
        {
            try
            {
                var response = await GetResponseFromWebService.GetResponse<ServiceClasses.RootObject_AttendanceTable>(ServiceURL.GetAttendanceTableList + "Month=" + Month + "&Year=" + Year + "&EmpCd=" + _empCode);
                if (response.Flag == true)
                {
                    if (ListObjAttendanceTblList == null)
                    {
                        ListObjAttendanceTblList = new List<LstAttendanceDtl>();
                    }
                    for (int i = 0; i < response.lstAttendanceDtl.Count; i++)
                    {
                        var objAttendanceTableList = new LstAttendanceDtl();

                        objAttendanceTableList.AttendanceDt = response.lstAttendanceDtl[i].AttendanceDt;
                        objAttendanceTableList.EarlyDeparture = response.lstAttendanceDtl[i].EarlyDeparture;
                        objAttendanceTableList.InTime = response.lstAttendanceDtl[i].EarlyDeparture;
                        objAttendanceTableList.LateArrival = response.lstAttendanceDtl[i].EarlyDeparture;
                        objAttendanceTableList.OutTime = response.lstAttendanceDtl[i].OutTime;
                        objAttendanceTableList.OverTime = response.lstAttendanceDtl[i].OverTime;
                        objAttendanceTableList.Reason = response.lstAttendanceDtl[i].Reason;
                        objAttendanceTableList.Remark = response.lstAttendanceDtl[i].Remark;
                        objAttendanceTableList.Shift = response.lstAttendanceDtl[i].Shift;
                        objAttendanceTableList.WorkingHrs = response.lstAttendanceDtl[i].WorkingHrs;

                        ListObjAttendanceTblList.Add(objAttendanceTableList);
                    }
                }
                else
                {
                }
                if (flag == 1)
                {
                    ChangeCalendar(CalandarChanges.All);
                }
                else
                {
                    ChangeCalendar(CalandarChanges.StartDate);
                }
            }
            catch (WebException e)
            {

            }
        } 

第三个功能是“ChangeCalendar”

protected void ChangeCalendar(CalandarChanges changes)
        {
            try
            {
                if (changes.HasFlag(CalandarChanges.StartDate))
                {
                    Device.BeginInvokeOnMainThread(() => CenterLabel.Text = StartDate.ToString(TitleLabelFormat));
                }
                var start = CalendarStartDate;
                var beginOfMonth = false;
                var endOfMonth = false;
                for (int i = 0; i < buttons.Count; i++)
                {
                    endOfMonth |= beginOfMonth && start.Day == 1;
                    beginOfMonth |= start.Day == 1;

                    LstAttendanceDtl objAttendanceDtl = ListObjAttendanceTblList.Find(s => s.AttendanceDt.Equals(start.Date.ToString("dd/MM/yyyy")));
                    string remarks = string.Empty;

                    if (i < 7 && WeekdaysShow && changes.HasFlag(CalandarChanges.StartDay))
                    {
                        Device.BeginInvokeOnMainThread(() => labels[i].Text = start.ToString(WeekdaysFormat));
                        //labels[i].Text = start.ToString(WeekdaysFormat);
                        //DateTime d = Convert.ToDateTime(objAttendanceDtl.AttendanceDt).Date; 
                    }
                    if (changes.HasFlag(CalandarChanges.All))
                    {
                        Device.BeginInvokeOnMainThread(()=>buttons[i].Text = string.Format("{0}", start.Day));
                        //buttons[i].Text = string.Format("{0}", start.Day);
                    }
                    else
                    {
                        Device.BeginInvokeOnMainThread(() => buttons[i].TextWithoutMeasure = string.Format("{0}", start.Day));
                    }

                    buttons[i].Date = start;
                    var isInsideMonth = beginOfMonth && !endOfMonth;
                    if (objAttendanceDtl != null)
                    {
                        remarks = objAttendanceDtl.Remark;

                        if ((remarks.ToLower()).Trim() == stringFullDay.ToLower().Trim())
                        {
                            SetButtonPresent(buttons[i], isInsideMonth);
                        }
                        else if (remarks.ToLower().Trim() == stringAbsent.ToLower().Trim())
                        {
                            SetButtonAbsent(buttons[i], isInsideMonth);
                        }
                        else if (remarks.ToLower().Trim() == stringWeekOff.ToLower().Trim())
                        {
                            SetButtonWeekendMood(buttons[i], isInsideMonth);
                        }
                        else if (remarks.ToLower().Trim() == stringHolidays.ToLower().Trim())
                        {
                            SetButtonHolidays(buttons[i], isInsideMonth);
                        }
                        else if (remarks.ToLower().Trim() == stringSecondhalfAbsent.ToLower().Trim() ||
                            remarks.ToLower().Trim() == stringFirsthalfAbsent.ToLower().Trim())
                        {
                            SetButtonHalfDayMood(buttons[i], isInsideMonth);
                        }
                        else
                        {
                            SetButtonDisabled(buttons[i]);
                        }
                    }
                    else
                    {
                        SetButtonOutSideMonth(buttons[i]);
                    }
                    SpecialDate sd = null;
                    if (SpecialDates != null)
                    {
                        sd = SpecialDates.FirstOrDefault(s => s.Date.Date == start.Date);
                    }

                    if (sd != null)
                    {
                        SetButtonSpecial(buttons[i], sd);
                    }
                    else if (SelectedDate.HasValue && start.Date == SelectedDate.Value.Date)
                    {
                        SetButtonSelected(buttons[i], isInsideMonth);
                    }
                    start = start.AddDays(1);
                }
            }
            catch (Exception e)
            {

            }
        }

问题是:

1。当我尝试直接填写标签列表时,在“Changecalendar”功能中

labels[i].Text = start.ToString(WeekdaysFormat);

它向我显示错误

“UIKit一致性错误:您正在调用只能从UI线程调用的UIKit方法。”所以要删除此错误,我写了

Device.BeginInvokeOnMainThread(() => labels[i].Text = start.ToString(WeekdaysFormat));

Device.BeginInvokeOnMainThread(()=>buttons[i].Text = string.Format("{0}", start.Day));

Device.BeginInvokeOnMainThread(() => buttons[i].TextWithoutMeasure = string.Format("{0}", start.Day));

但它显示错误

System.ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。

2. 如果我把“FillCalendarWindow”中的函数“CallWebService”和“ChangeCalendar”一个接一个地放入,那么列表对象没有绑定,控件就是从函数中出来而直接用ChangeCalendar函数调用并给我空引用对象。

4 个答案:

答案 0 :(得分:3)

该问题未提供测试解决方案所需的完整源代码。

回答关于在非异步方法中调用等待函数的问题。你可以使用

CallWebService().Wait(optional timeout); 

CallWebService().GetAwaiter().GetResult(); 

您还应该更改功能定义

async void CallWebService(int Month, int Year)

async Task CallWebService(int Month, int Year);

正确处理异常和线程切换

如果您可以不阻止地调用CallWebService,那么您也可以

CallWebService(1,2).ContinueWith((task) =>
{

});

答案 1 :(得分:2)

我假设从UI线程调用FillCalendarWindows()方法,但是你不希望UI线程在外部操作GetResponseFromWebService.GetResponse发生时等待并冻结控件,这就是你在等待的原因CallWebService()内部的异步调用。

我认为使用异步CallWebService的更好方法是使用“async all as”,因此您必须将CallWebService上游的所有方法更改为异步方法。这有一个额外的好处,你不需要做Device.BeginInvokeOnMainThread,因为async / await捕获调用者的同步上下文,因此将在UI线程上调用ChangeCalendar。

async void SomeEventHandler()
{
// called from the UI thread (or its equivalent in Xamarin)
    await FillCalendarWindows();
}

protected async Task FillCalendarWindows()
    {
        try
        {
           //create 7*6 = 42 labels and buttons

            await CallWebService(StartDate.Month, StartDate.Year);

        }
        catch (Exception e)
        {

        }
    }

public async Task CallWebService(int Month, int Year)
    {
        try
        {
            await GetResponseFromWebService.GetResponse... ;

            // .... same code as in your example 

            ChangeCalendar(....);

        }
        catch /*... */
        {

        }
    } 

protected void ChangeCalendar(int changes)
    {
        try
        {
            /* no need to do Device.BeginInvokeOnMainThread () so you can replace all that with normal calls*/
        }
        catch (Exception e)
        {
            /* ... */
        }
    }

不确定如何引发System.ArgumentOutOfRangeException,我无法在github上找到正确的代码版本,因此我无法调查该特定错误。我的猜测是你有多个线程修改“按钮”集合,当你调用Device.BeginInvokeOnMainThread时,你可能会发现集合的元素少于预期。

TL; DR:一直使用async / await而不是以同步方式调用异步方法,这样可以更容易地找到问题的原因

答案 2 :(得分:2)

尝试以下

protected async Task FillCalendarWindows()
        {
            try
            {
                for (int r = 0; r < 6; r++)
                {
                    for (int c = 0; c < 7; c++)
                    {
                        if (r == 0)
                        {
                            labels.Add(new Label
                            {
                                HorizontalOptions = LayoutOptions.Center,
                                VerticalOptions = LayoutOptions.Center,
                                TextColor = Color.Black,
                                FontSize = 18,
                                FontAttributes = FontAttributes.Bold
                            });
                            DayLabels.Children.Add(labels.Last(), c, r);
                        }
                        buttons.Add(new CalendarButton
                        {
                            BorderRadius = 0,
                            BorderWidth = BorderWidth,
                            BorderColor = BorderColor,
                            FontSize = DatesFontSize,
                            BackgroundColor = DatesBackgroundColor,
                            HorizontalOptions = LayoutOptions.FillAndExpand,
                            VerticalOptions = LayoutOptions.FillAndExpand
                        });
                        buttons.Last().Clicked += DateClickedEvent;
                        MainCalendar.Children.Add(buttons.Last(), c, r);
                    }
                }
                flag = 1;
                //Device.BeginInvokeOnMainThread(() => CallWebService(StartDate.Month, StartDate.Year));
                await CallWebService(StartDate.Month, StartDate.Year);
                //CallServiceInNewTask(StartDate.Month, StartDate.Year);
                //Device.BeginInvokeOnMainThread(() => await ChangeCalendar(CalandarChanges.All));

            }
            catch (Exception e)
            {

            }
        }

您的网络服务应该像

public async Task CallWebService(int Month, int Year)
    {
        try
        {
            var response = await GetResponseFromWebService.GetResponse<ServiceClasses.RootObject_AttendanceTable>(ServiceURL.GetAttendanceTableList + "Month=" + Month + "&Year=" + Year + "&EmpCd=" + _empCode);
            if (response.Flag == true)
            {
                if (ListObjAttendanceTblList == null)
                {
                    ListObjAttendanceTblList = new List<LstAttendanceDtl>();
                }
                for (int i = 0; i < response.lstAttendanceDtl.Count; i++)
                {
                    var objAttendanceTableList = new LstAttendanceDtl();

                    objAttendanceTableList.AttendanceDt = response.lstAttendanceDtl[i].AttendanceDt;
                    objAttendanceTableList.EarlyDeparture = response.lstAttendanceDtl[i].EarlyDeparture;
                    objAttendanceTableList.InTime = response.lstAttendanceDtl[i].EarlyDeparture;
                    objAttendanceTableList.LateArrival = response.lstAttendanceDtl[i].EarlyDeparture;
                    objAttendanceTableList.OutTime = response.lstAttendanceDtl[i].OutTime;
                    objAttendanceTableList.OverTime = response.lstAttendanceDtl[i].OverTime;
                    objAttendanceTableList.Reason = response.lstAttendanceDtl[i].Reason;
                    objAttendanceTableList.Remark = response.lstAttendanceDtl[i].Remark;
                    objAttendanceTableList.Shift = response.lstAttendanceDtl[i].Shift;
                    objAttendanceTableList.WorkingHrs = response.lstAttendanceDtl[i].WorkingHrs;

                    ListObjAttendanceTblList.Add(objAttendanceTableList);
                }
            }
            else
            {
            }
            if (flag == 1)
            {
               await ChangeCalendar(CalandarChanges.All);
            }
            else
            {
               await ChangeCalendar(CalandarChanges.StartDate);
            }
        }
        catch (WebException e)
        {

        }
    } 

你的ChnageCalender方法应该像

protected async Task ChangeCalendar(CalandarChanges changes)
    {
        try
        {
            if (changes.HasFlag(CalandarChanges.StartDate))
            {
                Device.BeginInvokeOnMainThread(() => CenterLabel.Text = StartDate.ToString(TitleLabelFormat));
            }
            var start = CalendarStartDate;
            var beginOfMonth = false;
            var endOfMonth = false;
            for (int i = 0; i < buttons.Count; i++)
            {
                endOfMonth |= beginOfMonth && start.Day == 1;
                beginOfMonth |= start.Day == 1;

                LstAttendanceDtl objAttendanceDtl = ListObjAttendanceTblList.Find(s => s.AttendanceDt.Equals(start.Date.ToString("dd/MM/yyyy")));
                string remarks = string.Empty;

                if (i < 7 && WeekdaysShow && changes.HasFlag(CalandarChanges.StartDay))
                {
                    Device.BeginInvokeOnMainThread(() => labels[i].Text = start.ToString(WeekdaysFormat));
                    //labels[i].Text = start.ToString(WeekdaysFormat);
                    //DateTime d = Convert.ToDateTime(objAttendanceDtl.AttendanceDt).Date; 
                }
                if (changes.HasFlag(CalandarChanges.All))
                {
                    Device.BeginInvokeOnMainThread(()=>buttons[i].Text = string.Format("{0}", start.Day));
                    //buttons[i].Text = string.Format("{0}", start.Day);
                }
                else
                {
                    Device.BeginInvokeOnMainThread(() => buttons[i].TextWithoutMeasure = string.Format("{0}", start.Day));
                }

                buttons[i].Date = start;
                var isInsideMonth = beginOfMonth && !endOfMonth;
                if (objAttendanceDtl != null)
                {
                    remarks = objAttendanceDtl.Remark;

                    if ((remarks.ToLower()).Trim() == stringFullDay.ToLower().Trim())
                    {
                        SetButtonPresent(buttons[i], isInsideMonth);
                    }
                    else if (remarks.ToLower().Trim() == stringAbsent.ToLower().Trim())
                    {
                        SetButtonAbsent(buttons[i], isInsideMonth);
                    }
                    else if (remarks.ToLower().Trim() == stringWeekOff.ToLower().Trim())
                    {
                        SetButtonWeekendMood(buttons[i], isInsideMonth);
                    }
                    else if (remarks.ToLower().Trim() == stringHolidays.ToLower().Trim())
                    {
                        SetButtonHolidays(buttons[i], isInsideMonth);
                    }
                    else if (remarks.ToLower().Trim() == stringSecondhalfAbsent.ToLower().Trim() ||
                        remarks.ToLower().Trim() == stringFirsthalfAbsent.ToLower().Trim())
                    {
                        SetButtonHalfDayMood(buttons[i], isInsideMonth);
                    }
                    else
                    {
                        SetButtonDisabled(buttons[i]);
                    }
                }
                else
                {
                    SetButtonOutSideMonth(buttons[i]);
                }
                SpecialDate sd = null;
                if (SpecialDates != null)
                {
                    sd = SpecialDates.FirstOrDefault(s => s.Date.Date == start.Date);
                }

                if (sd != null)
                {
                    SetButtonSpecial(buttons[i], sd);
                }
                else if (SelectedDate.HasValue && start.Date == SelectedDate.Value.Date)
                {
                    SetButtonSelected(buttons[i], isInsideMonth);
                }
                start = start.AddDays(1);
            }
        }
        catch (Exception e)
        {

        }
    }

答案 3 :(得分:1)

我发现你的评论中至少有两个问题

  1. &#34;如果(response.flag == true)(控件退出该功能)并直接调用ChangeCalendar() - 显然是因为您没有等待。

  2. 您的按钮数量(42)与标签数量(7)不同,所以当您尝试使用相同的标签[i]和按钮[i]时,#34; i&#34 ;您正在获得标签索引的ArgumentOutOfRangeException。注意if(r==0)将标签数限制为7。

            for (int r = 0; r < 6; r++)
            {
                for (int c = 0; c < 7; c++)
                {
                    if (r == 0)
                    {
                        labels.Add(new Label