- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 xamarin 表单中工作。我正在尝试在日历控件中绑定(bind) 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”函数,该函数用于绑定(bind)标签和按钮文本并填充适当的颜色。
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. 如果我将函数“CallWebService”和“ChangeCalendar”一个接一个地放在“FillCalendarWindow”中,则列表对象未绑定(bind)并且控制从函数中出来并直接调用 ChangeCalendar 函数并给我空引用对象。
最佳答案
问题没有提供测试解决方案所需的完整源代码。
回答您关于在非异步方法中调用可等待函数的问题。你可以使用
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) =>
{
});
关于c# - 如何从非异步方法调用异步方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40214871/
我是一名优秀的程序员,十分优秀!