We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get visible appointments in Xamarin.Forms Schedule (SfSchedule)

Platform: Xamarin.Forms |
Control: SfSchedule |
Published Date: March 3, 2020 |
Last Revised Date: March 3, 2020

You can get the visible custom appointments with recurrence appointments of Xamarin.Forms SfSchedule in VisibleDatesChangedEvent.

Initialize an event handler for the VisibleDatesChangedEvent of SfSchedule to get the visible appointments on visible dates changed.

schedule.VisibleDatesChangedEvent += OnVisibleDatesChangedEvent;
 
private void OnVisibleDatesChangedEvent(object sender, VisibleDatesChangedEventArgs e)
{
    List<Meeting> visibleAppointments;
 
    if (e.visibleDates.Count == 0)
        return;
 
    if (schedule.ScheduleView == ScheduleView.DayView)
    {
        visibleAppointments = this.GetVisibleAppointments(e.visibleDates.FirstOrDefault());
    }
    else
    {
        visibleAppointments = this.GetVisibleAppointments(e.visibleDates.First(), e.visibleDates.Last());
    }
}

Get the visible appointments by comparing the visible dates and the date of each appointment in Schedule DataSource.

private List<Meeting> GetVisibleAppointments(DateTime startDate, DateTime? endDate = null)
{
    List<Meeting> appointments = new List<Meeting>();
 
    if (schedule.DataSource == null)
        return appointments;
 
    if (endDate == null)
    {
        foreach (Meeting app in schedule.DataSource)
        {
            if (app.RecurrenceRule == null && (app.From.Date == startDate.Date || app.To.Date == startDate.Date))
                appointments.Add(app);
        }
    }
    else
    {
        foreach (Meeting app in schedule.DataSource)
        {
            if (app.RecurrenceRule == null && ((app.From.Date >= startDate.Date && app.From.Date <= endDate.Value.Date) ||
                (app.To.Date >= startDate.Date && app.To.Date <= endDate.Value.Date)))
                appointments.Add(app);
        }
    }
    return appointments;
}

You can also check the RRule and get the RecurrenceAppointments using GetRecurrenceDateTimeCollection method. Check the date collection with the visible date range to get the visible appointments.

private List<Meeting> GetVisibleAppointments(DateTime startDate, DateTime? endDate = null)
{
    List<Meeting> appointments = new List<Meeting>();
 
    if (schedule.DataSource == null)
        return appointments;
    //Gets the recurrence appointments within the date range
    foreach (Meeting app in schedule.DataSource)
    {
        if (app.RecurrenceRule != null)
        {
            IEnumerable<DateTime> dateCollection = schedule.GetRecurrenceDateTimeCollection(app.RecurrenceRule, app.From);
 
            if (endDate == null)
            {
                if (dateCollection.Any(d => d.Date == startDate.Date))
                {
                    appointments.Add(app);
                }
 
            }
            else
            {
                while (startDate <= endDate)
                {
                    if (dateCollection.Any(d => d.Date == startDate.Date))
                    {
                        appointments.Add(app);
                    }
                    startDate = startDate.Date.AddDays(1);
                }
            }
 
        }
    }
    return appointments;
}

Download the sample from GitHub.

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile