Rathana,
Here are the relevant snippets of code. I created a test project just to test loading static, hard-coded events when the month changes, and it works. The problem must be with the asynchronous loading of events when the REST services returns. One option I would have is to perform that synchronously, which in my opinion is not an option. The UI would block until the call was complete. It seems in my asynchronous scenario, the calendar is first loaded into the UI. Then the REST service returns the data and the calendar UI is not refreshed, even though the data has been loaded into the calendar, it is not displayed until you take some action on the calendar. Again, this is working on iOS, so I assume it has something to do with the UI lifecycle on Android. Anyway, here is almost all of the the code from the calendar page:
namespace Foo
{
public partial class Calendar : ContentPage
{
private string _flightdata = null;
private string _holddata = null;
private string _mxdata = null;
private string _authdata = "";
private string _authheader = "";
private List<string> _loadedMonths = null;
SfCalendar calendar;
CalendarEventCollection col = new CalendarEventCollection();
public Calendar()
{
_loadedMonths = new List<string>();
calendar = new SfCalendar();
calendar.ShowInlineEvents = true;
calendar.ShowNavigationButtons = true;
calendar.MonthChanged += LoadNewMonth;
LoadFlights(DateTime.Now);
_loadedMonths.Add(DateTime.Now.Month.ToString());
layout.Children.Add(calendar);
}
private void LoadFlights(DateTime currDate)
{
vm.IsLoading = true;
string startDate = currDate.Month.ToString() + "/1/" + currDate.Year.ToString();
string endDate = currDate.Month.ToString() + "/" + DateTime.DaysInMonth(currDate.Year, currDate.Month).ToString() +
"/" + currDate.Year.ToString();
Task<bool> success = GetData(startDate, endDate);
}
private void LoadNewMonth(object sender, MonthChangedEventArgs e)
{
DateTime currDate = e.args.CurrentValue;
DateTime prevDate = e.args.PreviousValue;
if (!_loadedMonths.Contains(currDate.Month.ToString()))
{
_loadedMonths.Add(currDate.Month.ToString());
LoadFlights(currDate);
}
calendar.MoveToDate = e.args.CurrentValue;
}
public async Task<bool> GetData(string start, string end)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _authheader);
client.MaxResponseContentBufferSize = 512000;
Uri flightUrl = new Uri(String.Format(Constants.ServiceUrl + "/getflights?start=" + start + "&end=" + end, string.Empty));
try
{
HttpContent responseContent;
var response = await client.GetAsync(flightUrl);
if (response.IsSuccessStatusCode)
{
responseContent = response.Content;
_flightdata = responseContent.ReadAsStringAsync().Result;
var flightobj = JsonConvert.DeserializeObject<List<Flight>>(_flightdata);
foreach (Flight f in flightobj)
{
CalendarInlineEvent ev = new CalendarInlineEvent();
ev.Subject = f.ORIGIN + "-" + f.DEST;
ev.StartTime = Convert.ToDateTime(f.LOCALLEAVE);
ev.EndTime = Convert.ToDateTime(f.LOCALARRIVE);
ev.Color = Color.FromHex("68A0ED");
col.Add(ev);
}
}
calendar.DataSource = col;
calendar.MoveToDate = Convert.ToDateTime(start);
vm.IsLoading = false;
return true;
}
catch (Exception e)
{
Page p = new Page();
await p.DisplayAlert("ERROR!", e.ToString(), "Close");
return false;
}