Articles in this section
Category / Section

How to handle appointments for multiple resources in Xamarin.Forms Schedule?

3 mins read

 

In SfSchedule, you can enable resources and add appointments under it in sample level.

 

C#

     
     SfSchedule sfschedule;
     ScheduleAppointmentCollection appointmentCollection;
     WeekViewSettings weekviewsettings;
 
    public  TestClass()
    {  
        sfschedule=new SfSchedule();
        sfschedule.ScheduleView = ScheduleView.WeekView;
        sfschedule.VerticalOptions = LayoutOptions.FillAndExpand;
        sfschedule.BackgroundColor=Color.White;
        appointmentCollection = new ScheduleAppointmentCollection();
        weekviewsettings = new WeekViewSettings();
        weekviewsettings.ShowAllDay = true;
        sfschedule.WeekViewSettings = weekviewsettings;
 
        Switch resource1 = new Switch();
        resource1.BackgroundColor = Color.Red;
        Label resource1_label = new Label();
        resource1_label.Text="Dr. Joseph (nephrologist)";
        resource1_label.TextColor = Color.Black;
        resource1_label.BackgroundColor = Color.Red;
        resource1.IsToggled = false;
        resource1.Toggled += resource1_Toggled;
        StackLayout resource1_layout = new StackLayout();
        resource1_layout.Orientation = StackOrientation.Horizontal;
        resource1_layout.Children.Add(resource1);
        resource1_layout.Children.Add(resource1_label);
 
        Switch resource2 = new Switch();
        resource2.BackgroundColor = Color.Blue;
        Label resource2_label = new Label();
        resource2_label.Text = "Dr. Stephen (diabetoligist)";
        resource2_label.TextColor = Color.Black;
        resource2_label.BackgroundColor = Color.Blue;
        resource2.IsToggled = false;
        resource2.Toggled += resource2_Toggled;
        StackLayout resource2_layout = new StackLayout();
        resource2_layout.Orientation = StackOrientation.Horizontal;
        resource2_layout.Children.Add(resource2);
        resource2_layout.Children.Add(resource2_label);
 
        StackLayout layout = new StackLayout();
        layout.Orientation = StackOrientation.Vertical;
        layout.HorizontalOptions = LayoutOptions.FillAndExpand;
        layout.Children.Add(resource1_layout);
        layout.Children.Add(resource2_layout);
 
        StackLayout schedule_layout = new StackLayout();
        schedule_layout.VerticalOptions = LayoutOptions.FillAndExpand;
        schedule_layout.Children.Add(sfschedule);
 
        StackLayout mainlayout = new StackLayout();
        mainlayout.Orientation = StackOrientation.Vertical;
        mainlayout.Children.Add(layout);
        mainlayout.Children.Add(schedule_layout);
    
        this.Content= mainlayout;
    }
 

 

Add appointments accordingly to the respective resources. Here, two switches are used such that appointment collection is set for each switch. When the switch is toggled, respective schedule’s appointment is displayed in the view and when it is toggled back, appointment collection gets cleared. Thus, appointments are handled using switch in this sample.

 

C#

 
   void resource1_Toggled(object sender, ToggledEventArgs e)
    {
        if (e.Value)
        {
            ScheduleAppointment resource1_appointment = new ScheduleAppointment();
 
            DateTime currentDate = DateTime.Now;
            DateTime startTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 10, 0, 0);
            DateTime endTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 12, 0, 0);
 
            resource1_appointment.StartTime = startTime;
            resource1_appointment.EndTime = endTime;
            resource1_appointment.Color = Color.Red;
            resource1_appointment.Subject = "Kidney Stones removal";
            appointmentCollection.Add(resource1_appointment);
            sfschedule.DataSource = appointmentCollection;
        }
        else
        {
            appointmentCollection.Clear();
        }
    }
 
    void resource2_Toggled(object sender, ToggledEventArgs e)
    {
        if (e.Value)
        {
            ScheduleAppointment resource2_appointment = new ScheduleAppointment();
 
            DateTime currentDate1 = DateTime.Now.AddDays(2);
            DateTime startTime1 = new DateTime(currentDate1.Year, currentDate1.Month, currentDate1.Day, 9, 0, 0);
            DateTime endTime1 = new DateTime(currentDate1.Year, currentDate1.Month, currentDate1.Day, 12, 0, 0);
 
            resource2_appointment.StartTime = startTime1;
            resource2_appointment.EndTime = endTime1;
            resource2_appointment.Color = Color.Blue;
            resource2_appointment.Subject = "Blood Sugar Test";
            appointmentCollection.Add(resource2_appointment);
            sfschedule.DataSource = appointmentCollection;
 
        }else
        {
            appointmentCollection.Clear();
        }
    }

 

The following screenshot displays the resource with appointment in Week View.

../Projects/Schedule_Resource/Screenshots/android/Resource1.jpg

../Projects/Schedule_Resource/Screenshots/android/Resource2.jpg

 

 

You can download the entire source code of this demo for Xamarin.Forms from  
here, ScheduleResource .

 


 

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