Articles in this section
Category / Section

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

3 mins read

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.  


Conclusion

I hope you enjoyed learning about how to get visible appointments in Xamarin.Forms Schedule (SfSchedule).

You can refer to our Xamarin.Forms Scheduler feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied