Xamarin.Forms - 从另一个文件将约会添加到 syncfusion 计划

Xamarin.Forms - Adding appointment to syncfusion schedule from another file

我正在尝试从另一个文件向我的 syncfusion 计划 (SfSchedule) 添加约会。然而,它给我这个错误信息:“CS0122 MainPage.ScheduleDay' 由于其保护级别 School_schedule 而无法访问" 第二个文件无法访问计划表格第一个文件。花了几个小时寻找解决方案:/有人可以帮忙吗?

code image

我的代码:

MainPage.xaml

        <syncfusion:SfSchedule.DayViewSettings>
            <syncfusion:DayViewSettings
                NonWorkingHoursTimeSlotColor="White">
            </syncfusion:DayViewSettings>
        </syncfusion:SfSchedule.DayViewSettings>

        <syncfusion:SfSchedule.SelectionView>
            <Button 
                x:Name="Button_Open_Add_Event"
                BackgroundColor="Orange"
                Text="+New Event"
                TextColor="White"
                Clicked ="Open_Add_Event"/>
        </syncfusion:SfSchedule.SelectionView>
    </syncfusion:SfSchedule>
</StackLayout>

MainPage.xaml.cs

 public async void Open_Add_Event(Object sender, EventArgs e)
    {
        
        AddEvent Popup_Open_AddEvent = new AddEvent();

        await this.Navigation.PushModalAsync(Popup_Open_AddEvent);
    }

AddEvent.xaml

<ContentPage.Content>
    <StackLayout>

        <Label Text="Welcome to Xamarin.Forms!"
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" />

        <DatePicker 
            x:Name="Date_Add_Event"
        />

        <TimePicker
            x:Name="Time_Add_Event"
        />

        <Label x:Name="Test"></Label>

        <Button 
            Clicked="Button_CreateEvent"
            Text="Create"
        />

        <Button 
            Clicked="Button_BacktomainPage" 
            Text="Cancel"
        />

    </StackLayout>
</ContentPage.Content>

AddEvent.xaml.cs

public void Button_CreateEvent (object sender, EventArgs e)
    {

        
        DateTime Date_from = Date_Add_Event.Date + Time_Add_Event.Time;

        Test.Text = Date_from.ToString();

        // Creating an instance for schedule appointment collection
        ScheduleAppointmentCollection scheduleAppointmentCollection = new ScheduleAppointmentCollection();
        //Adding schedule appointment in schedule appointment collection 
        scheduleAppointmentCollection.Add(new ScheduleAppointment()
        {
            StartTime = new DateTime (2021, 02, 11, 10, 00, 00),
            EndTime = new DateTime(2021, 02, 11, 12,00,00),
            Subject = "Meeting",
            Location = "Hutchison road",
        }) ;

        //Adding schedule appointment col+lection to DataSource of SfSchedule
          MainPage.ScheduleDay.DataSource = scheduleAppointmentCollection;

    }

    

    public void Button_BacktomainPage (object sender, EventArgs e)
    {
        this.Navigation.PopModalAsync();
    }

我们已经根据给定的信息准备了样本,并从我们这边检查了报告的场景。我们很遗憾地通知您,我们无法从我们这边复制报告的场景。它按预期工作。

查看

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ScheduleXamarin"
             xmlns:schedule="clr-namespace:Syncfusion.SfSchedule.XForms;assembly=Syncfusion.SfSchedule.XForms"
             x:Class="ScheduleXamarin.MainPage">
    <ContentPage.Content>
    <Grid>
       <schedule:SfSchedule x:Name="schedule"
                                 DataSource="{Binding ListOfMeeting}"
                                 ScheduleView="DayView" CellDoubleTapped="Schedule_CellDoubleTapped"
                                 Margin="0">
                <schedule:SfSchedule.AppointmentMapping>
                    <schedule:ScheduleAppointmentMapping
                        ColorMapping="Color"
                        EndTimeMapping="To"
                        IsAllDayMapping="IsAllDay"
                        StartTimeMapping="From"
                        SubjectMapping="EventName"
                        MinHeightMapping="MinimumHeight"
                        StartTimeZoneMapping="StartTimeZone"
                        EndTimeZoneMapping="EndTimeZone"/>
                </schedule:SfSchedule.AppointmentMapping>
                <schedule:SfSchedule.BindingContext>
                    <local:AppointmentEditorViewModel/>
            </schedule:SfSchedule.BindingContext>
        </schedule:SfSchedule>
    </Grid>
         </ContentPage.Content>
   </ContentPage>

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:schedulexamarin="clr-namespace:ScheduleXamarin"
             x:Class="ScheduleXamarin.View.EditorLayout">
    <ContentPage.BindingContext>
        <schedulexamarin:EditorLayoutViewModel />
    </ContentPage.BindingContext>
   
    <ContentPage.Content>
        <StackLayout x:Name="editorLayout">
            <ScrollView x:Name="editorScrollView">
                <StackLayout>
                    <Grid
        x:Name="eventName_layout"
        Padding="20,10,20,0"
        HeightRequest="40"
        VerticalOptions="Start">
                        <Entry
            x:Name="eventNameText"
            HeightRequest="40"
            TextColor="Black" />
                    </Grid>
                    <Grid
        x:Name="organizer_layout"
        Padding="20,20,20,0"
        HeightRequest="40"
        VerticalOptions="Start">
                        <Entry
            x:Name="organizerText"
            HeightRequest="40"
            TextColor="Black" />
                    </Grid>
                    <Grid
        x:Name="startTimeLabel_layout"
        Padding="20,10,20,0"
        HeightRequest="20"
        VerticalOptions="Start">
                        <Label
            Font="15"
            Text="From"
            TextColor="Gray" />
                    </Grid>
                    <Grid
        x:Name="StartdateTimePicker_layout"
        Padding="20,0,20,0"
        VerticalOptions="Start">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Grid
            x:Name="start_datepicker_layout"
            Grid.Column="0"
            HeightRequest="50"
            VerticalOptions="Start"
            WidthRequest="20">
                            <DatePicker x:Name="startDate_picker" HeightRequest="40" />
                        </Grid>
                        <Grid
            x:Name="start_timepicker_layout"
            Grid.Column="1"
            HeightRequest="50"
            VerticalOptions="Start"
            WidthRequest="20">
                            <TimePicker x:Name="startTime_picker" HeightRequest="40" />
                        </Grid>
                    </Grid>
                    <Grid BackgroundColor="White" 
                Padding="20,0,20,0"
                HorizontalOptions="FillAndExpand" 
                VerticalOptions="FillAndExpand">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Label Grid.Row="0" x:Name="startTimeZoneLabel" 
                    VerticalOptions="Center" 
                    Text="Start Time Zone" 
                    FontSize="15" TextColor="Gray" />
                        <Picker x:Name="startTimeZonePicker" Grid.Row="1" 
                        VerticalOptions="Center" 
                        SelectedIndex="0"
                        HorizontalOptions="FillAndExpand">
                        </Picker>
                    </Grid>
                    <Grid
        x:Name="endTimeLabel_layout"
        Padding="20,10,20,0"
        HeightRequest="20"
        VerticalOptions="Start">
                        <Label
            Font="15"
            HeightRequest="20"
            Text="To"
            TextColor="Gray" />
                    </Grid>
                    <Grid
        x:Name="EndDateTimePicker_layout"
        Padding="20,0,20,0"
        VerticalOptions="Start">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Grid
            x:Name="end_datepicker_layout"
            Grid.Column="0"
            HeightRequest="50"
            VerticalOptions="Start">
                            <DatePicker
                x:Name="endDate_picker"
                Grid.Column="0"
                HeightRequest="50"
                VerticalOptions="Start" />
                        </Grid>
                        <Grid
            x:Name="end_timepicker_layout"
            Grid.Column="1"
            HeightRequest="50"
            VerticalOptions="Start">
                            <TimePicker
                x:Name="endTime_picker"
                HeightRequest="50"
                VerticalOptions="Start" />
                        </Grid>
                    </Grid>
                    <Grid BackgroundColor="White" 
                Padding="20,0,20,0"
                HorizontalOptions="FillAndExpand" 
                VerticalOptions="FillAndExpand">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Label Grid.Row="0" x:Name="endTimeZoneLabel" 
                    VerticalOptions="Center" 
                    Text="End Time Zone" 
                    FontSize="15" TextColor="Gray" />
                        <Picker x:Name="endTimeZonePicker" Grid.Row="1" 
                        VerticalOptions="Center" 
                        SelectedIndex="0"
                        HorizontalOptions="FillAndExpand">
                        </Picker>
                    </Grid>
                    <Grid
        x:Name="allDayGrid"
        Padding="20,10,20,0"
        VerticalOptions="Start">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Label x:Name="allDayLabel"
            Grid.Column="0"
            Font="15"
            Text="All Day"
            TextColor="Gray"
            VerticalTextAlignment="Center" />
                        <Grid Grid.Column="1" Padding="20,0,0,0">
                            <Switch
                x:Name="switchAllDay"
                HorizontalOptions="Start"
                VerticalOptions="Center" />
                        </Grid>
                    </Grid>
                </StackLayout>
            </ScrollView>

            <Grid x:Name="editorButtons" BackgroundColor="White"
        VerticalOptions="Start">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid x:Name="cancelButtonGrid"
            Grid.Column="0">
                    <Button
                x:Name="cancelButton"
                Text="Cancel"
                TextColor="#3B3B3B"
                WidthRequest="100" />
                </Grid>
                <Grid  x:Name="saveButtonGrid"
            Grid.Column="1">
                    <Button
                x:Name="saveButton"
                Text="Save"
                TextColor="White"
                WidthRequest="100" />
                </Grid>
            </Grid>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

视图模型

namespace ScheduleXamarin
{
    #region AppointmentEditorViewModel
    public class AppointmentEditorViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// current day meetings 
        /// </summary>
        private List<string> currentDayMeetings;

        /// <summary>
        /// minimum time meetings
        /// </summary>
        private List<string> minTimeMeetings;

        /// <summary>
        /// color collection
        /// </summary>
        private List<Color> colorCollection;

        /// <summary>
        /// list of meeting
        /// </summary>
        private ObservableCollection<Meeting> listOfMeeting;

        /// <summary>
        /// header label value
        /// </summary>
        private string headerLabelValue = DateTime.Today.Date.ToString("MMMM yyyy");

        #region ScheduleType

        /// <summary>
        /// schedule type collection
        /// </summary>
        private ObservableCollection<ScheduleTypeClass> scheduleTypeCollection = new ObservableCollection<ScheduleTypeClass>();

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="AppointmentEditorViewModel" /> class.
        /// </summary>
        public AppointmentEditorViewModel()
        {
            this.ListOfMeeting = new ObservableCollection<Meeting>();
            this.InitializeDataForBookings();
            this.BookingAppointments();
            this.AddScheduleType();
        }

        /// <summary>
        /// Property changed event handler
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #region ListOfMeeting

        /// <summary>
        /// Gets or sets list of meeting
        /// </summary>
        public ObservableCollection<Meeting> ListOfMeeting
        {
            get
            {
                return this.listOfMeeting;
            }

            set
            {
                this.listOfMeeting = value;
                this.RaiseOnPropertyChanged("ListOfMeeting");
            }
        }
        #endregion

        #region HeaderLabelValue

        /// <summary>
        /// Gets or sets header label value
        /// </summary>
        public string HeaderLabelValue
        {
            get
            {
                return this.headerLabelValue;
            }

            set
            {
                this.headerLabelValue = value;
                this.RaiseOnPropertyChanged("HeaderLabelValue");
            }
        }

        /// <summary>
        /// Gets or sets schedule type collection
        /// </summary>
        public ObservableCollection<ScheduleTypeClass> ScheduleTypeCollection
        {
            get
            {
                return this.scheduleTypeCollection;
            }

            set
            {
                this.scheduleTypeCollection = value;
                this.RaiseOnPropertyChanged("ScheduleTypeCollection");
            }
        }
        #endregion

        /// <summary>
        /// method for adding schedule types
        /// </summary>
        private void AddScheduleType()
        {
            this.ScheduleTypeCollection.Add(new ScheduleTypeClass() { ScheduleType = "Day view" });
            this.ScheduleTypeCollection.Add(new ScheduleTypeClass() { ScheduleType = "Week view" });
            this.ScheduleTypeCollection.Add(new ScheduleTypeClass() { ScheduleType = "Work week view" });
            this.ScheduleTypeCollection.Add(new ScheduleTypeClass() { ScheduleType = "Month view" });
            this.scheduleTypeCollection.Add(new ScheduleTypeClass() { ScheduleType = "Timeline view" });
        }

        #region BookingAppointments

        /// <summary>
        /// Method for booking appointments.
        /// </summary>
        private void BookingAppointments()
        {
            Random randomTime = new Random();
            List<Point> randomTimeCollection = this.GettingTimeRanges();

            DateTime date;
            DateTime dateFrom = DateTime.Now.AddDays(-10);
            DateTime dateTo = DateTime.Now.AddDays(10);
            DateTime dateRangeStart = DateTime.Now.AddDays(-3);
            DateTime dateRangeEnd = DateTime.Now.AddDays(3);

            for (date = dateFrom; date < dateTo; date = date.AddDays(1))
            {
                if ((DateTime.Compare(date, dateRangeStart) > 0) && (DateTime.Compare(date, dateRangeEnd) < 0))
                {
                    for (int additionalAppointmentIndex = 0; additionalAppointmentIndex < 3; additionalAppointmentIndex++)
                    {
                        Meeting meeting = new Meeting();
                        int hour = randomTime.Next((int)randomTimeCollection[additionalAppointmentIndex].X, (int)randomTimeCollection[additionalAppointmentIndex].Y);
                        meeting.From = new DateTime(date.Year, date.Month, date.Day, hour, 0, 0);
                        meeting.To = meeting.From.AddHours(1);
                        meeting.EventName = this.currentDayMeetings[randomTime.Next(9)];
                        meeting.Color = this.colorCollection[randomTime.Next(9)];
                        meeting.IsAllDay = false;
                        meeting.StartTimeZone = string.Empty;
                        meeting.EndTimeZone = string.Empty;
                        this.ListOfMeeting.Add(meeting);
                    }
                }
                else
                {
                    Meeting meeting = new Meeting();
                    meeting.From = new DateTime(date.Year, date.Month, date.Day, randomTime.Next(9, 11), 0, 0);
                    meeting.To = meeting.From.AddDays(2).AddHours(1);
                    meeting.EventName = this.currentDayMeetings[randomTime.Next(9)];
                    meeting.Color = this.colorCollection[randomTime.Next(9)];
                    meeting.IsAllDay = true;
                    meeting.StartTimeZone = string.Empty;
                    meeting.EndTimeZone = string.Empty;
                    this.ListOfMeeting.Add(meeting);
                }
            }

            // Minimum Height Meetings
            DateTime minDate;
            DateTime minDateFrom = DateTime.Now.AddDays(-2);
            DateTime minDateTo = DateTime.Now.AddDays(2);

            for (minDate = minDateFrom; minDate < minDateTo; minDate = minDate.AddDays(1))
            {
                Meeting meeting = new Meeting();
                meeting.From = new DateTime(minDate.Year, minDate.Month, minDate.Day, randomTime.Next(9, 18), 30, 0);
                meeting.To = meeting.From;
                meeting.EventName = this.minTimeMeetings[randomTime.Next(0, 4)];
                meeting.Color = this.colorCollection[randomTime.Next(0, 10)];
                meeting.StartTimeZone = string.Empty;
                meeting.EndTimeZone = string.Empty;

                // Setting Mininmum Appointment Height for Schedule Appointments
                if (Device.RuntimePlatform == "Android")
                {
                    meeting.MinimumHeight = 50;
                }
                else
                {
                    meeting.MinimumHeight = 25;
                }

                this.ListOfMeeting.Add(meeting);
            }
        }

        #endregion BookingAppointments

        #region GettingTimeRanges

        /// <summary>
        /// Method for get timing range.
        /// </summary>
        /// <returns>return time collection</returns>
        private List<Point> GettingTimeRanges()
        {
            List<Point> randomTimeCollection = new List<Point>();
            randomTimeCollection.Add(new Point(9, 11));
            randomTimeCollection.Add(new Point(12, 14));
            randomTimeCollection.Add(new Point(15, 17));

            return randomTimeCollection;
        }

        #endregion GettingTimeRanges

        #region InitializeDataForBookings

        /// <summary>
        /// Method for initialize data bookings.
        /// </summary>
        private void InitializeDataForBookings()
        {
            this.currentDayMeetings = new List<string>();
            this.currentDayMeetings.Add("General Meeting");
            this.currentDayMeetings.Add("Plan Execution");
            this.currentDayMeetings.Add("Project Plan");
            this.currentDayMeetings.Add("Consulting");
            this.currentDayMeetings.Add("Performance Check");
            this.currentDayMeetings.Add("Yoga Therapy");
            this.currentDayMeetings.Add("Plan Execution");
            this.currentDayMeetings.Add("Project Plan");
            this.currentDayMeetings.Add("Consulting");
            this.currentDayMeetings.Add("Performance Check");

            // MinimumHeight Appointment Subjects
            this.minTimeMeetings = new List<string>();
            this.minTimeMeetings.Add("Work log alert");
            this.minTimeMeetings.Add("Birthday wish alert");
            this.minTimeMeetings.Add("Task due date");
            this.minTimeMeetings.Add("Status mail");
            this.minTimeMeetings.Add("Start sprint alert");

            this.colorCollection = new List<Color>();
            this.colorCollection.Add(Color.FromHex("#FF339933"));
            this.colorCollection.Add(Color.FromHex("#FF00ABA9"));
            this.colorCollection.Add(Color.FromHex("#FFE671B8"));
            this.colorCollection.Add(Color.FromHex("#FF1BA1E2"));
            this.colorCollection.Add(Color.FromHex("#FFD80073"));
            this.colorCollection.Add(Color.FromHex("#FFA2C139"));
            this.colorCollection.Add(Color.FromHex("#FFA2C139"));
            this.colorCollection.Add(Color.FromHex("#FFD80073"));
            this.colorCollection.Add(Color.FromHex("#FF339933"));
            this.colorCollection.Add(Color.FromHex("#FFE671B8"));
            this.colorCollection.Add(Color.FromHex("#FF00ABA9"));
        }

        #endregion InitializeDataForBookings

        #region Property Changed Event

        /// <summary>
        /// Invoke method when property changed
        /// </summary>
        /// <param name="propertyName">property name</param>
        private void RaiseOnPropertyChanged(string propertyName)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
    #endregion

    #region ScheduleTypeClass

    /// <summary>
    /// Schedule Type Class
    /// </summary>
    public class ScheduleTypeClass : INotifyPropertyChanged
    {
        #region ScheduleType

        /// <summary>
        /// schedule type
        /// </summary>
        private string scheduleType = " ";

        /// <summary>
        /// Gets or sets schedule types
        /// </summary>
        public string ScheduleType
        {
            get
            {
                return this.scheduleType;
            }

            set
            {
                this.scheduleType = value;
                this.RaiseOnPropertyChanged("ScheduleType");
            }
        }
        #endregion

        /// <summary>
        /// property changed event handler
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Invoke method when property changed
        /// </summary>
        /// <param name="propertyName">property name</param>
        private void RaiseOnPropertyChanged(string propertyName)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

namespace ScheduleXamarin
{
    internal class EditorLayoutViewModel
    {
        /// <summary>
        /// method for appointment modified
        /// </summary>
        /// <param name="e">Schedule Appointment Modified Event Args</param>
        public virtual void OnAppointmentModified(ScheduleAppointmentModifiedEventArgs e)
        {
            EventHandler<ScheduleAppointmentModifiedEventArgs> handler = this.AppointmentModified;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        /// <summary>
        /// Event handler
        /// </summary>
        public event EventHandler<ScheduleAppointmentModifiedEventArgs> AppointmentModified;
    }

    /// <summary>
    /// schedule appointment modified event args
    /// </summary>
    public class ScheduleAppointmentModifiedEventArgs : EventArgs
    {
        /// <summary>
        /// Gets or sets appointment
        /// </summary>
        public Meeting Appointment
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets is modified value
        /// </summary>
        public bool IsModified
        {
            get;
            set;
        }
    }
}

型号

namespace ScheduleXamarin
{
    [Preserve(AllMembers = true)]
    public class Meeting
    {
        /// <summary>
        /// Gets or sets event name
        /// </summary>
        public string EventName { get; set; }

        /// <summary>
        /// Gets or sets organizer
        /// </summary>
        public string Organizer { get; set; }

        /// <summary>
        /// Gets or sets contact ID
        /// </summary>
        public string ContactID { get; set; }

        /// <summary>
        /// Gets or sets capacity
        /// </summary>
        public int Capacity { get; set; }

        /// <summary>
        /// Gets or sets date
        /// </summary>
        public DateTime From { get; set; }

        /// <summary>
        /// Gets or sets date
        /// </summary>
        public DateTime To { get; set; }

        /// <summary>
        /// Gets or sets color
        /// </summary>
        public Color Color { get; set; }

        /// <summary>
        /// Gets or sets minimum height
        /// </summary>
        public double MinimumHeight { get; set; }

        /// <summary>
        /// Gets or sets all day
        /// </summary>
        public bool IsAllDay { get; set; }

        /// <summary>
        /// Gets or sets start time zone
        /// </summary>
        public string StartTimeZone { get; set; }

        /// <summary>
        /// Gets or sets end time zone
        /// </summary>
        public string EndTimeZone { get; set; }

        public ObservableCollection<object> Resources { get; set; }
    }

    public class Employees : INotifyPropertyChanged
    {
        private string name;
        private object id;
        private Color color;
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                this.OnPropertyChanged("Name");
            }
        }
        public object ID
        {
            get { return id; }
            set
            {
                id = value;
                this.OnPropertyChanged("ID");
            }
        }

        public Color Color
        {
            get { return color; }
            set
            {
                color = value;
                this.OnPropertyChanged("Color");
            }
        }

        public string Image { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}