Using MonthChanging event to setup data for converters (not)

May 9, 2014 at 8:15 PM
I would like to use the ColorConverter class's Convert method to color a WPControls calendar day item indicating whether there is WP 8 appointment on that day.

After the MonthChanging event triggers, I initiate an appt.SearchAsync call (MS.Phone.UserData) that will retrieve the appointment data. However, I find that the calendar's Cal_MonthChanged event fires before the Appointment_SearchCompleted handler has finished.

So I assume the ColorConverter would also get called ahead of the appointments data being ready.

As the SearchAsync return is of type void, the compiler does not allow the use of the await keyword - as a means of suspending the return from the MonthChanging event until the search is completed. The MonthChanging event, therefore, cannot be used reliably to gather the data that the color converter will use.

Does anyone know how to delay the UI update until the Appointments_SearchCompleted handler has done its job? or maybe my assumptions are incorrect? I don't believe tinkering with thread.sleep will work as the app will end up being blocked completely.

thank you.
May 9, 2014 at 10:27 PM
I think I solved this. My problem has been that I assumed that the call into the Phone.UserData namespace of
        appts.SearchAsync(dtStart, dtEnd, max, "Appointments Test #1");
would be made from a thread other than the one the user app and UI are running from. But I don't think this is the case and it would explain why any kind of wait would block the whole application.

What I did now was call the procedure that initiates the SearchAsync function from another newly created task and then wait for that to complete before returning from the Cal_MonthChanging event handler.
    private void Cal_MonthChanging(object sender, WPControls.MonthChangedEventArgs e)
        Task taskA = Task.Factory.StartNew(() => SetupAppointmentData(e));

    private  void SetupAppointmentData(WPControls.MonthChangedEventArgs e)
... //Identify the method that runs after the asynchronous search completes.
        appts.SearchCompleted += new EventHandler<AppointmentsSearchEventArgs>(Appointments_SearchCompleted);
        appts.SearchAsync(dtStart, dtEnd, max, "Appointments Test #1");
I will have to enclose some of this in a try catch in case something goes wrong - but my debug output - is now showing that the Cal_MonthChanged event occurs after output line with appointments found. Maybe some safety timeouts will be needed as well - which I yet have to think about how that would be done.

I hope this helps someone and if Sergy or someone much wiser - sees any pitfalls with this logic, please let me know.
I will now proceed to actually save the collection of appointment datetime objects and verify that the colorconverter will paint the days accordingly and only after the appointment data has been gathered.

OK - we have liftoff on the device. (clicked next button)

Cal_MonthChanging fired. New year is 2014 new month is 6
Sunday, June 1, 2014
Appointments found = 3
Cal_MonthChanged fired. New year is 2014 new month is 6
Vertical Flick: Angle 90 Velocity 0,307

Thank you Sergy for your work!