Esempio n. 1
0
        //#region WeekdayRowBackgroundColor

        //public static readonly BindableProperty WeekdayRowBackgroundColorProperty =
        //    BindableProperty.Create(nameof(WeekdayRowBackgroundColor), typeof(Color), typeof(Calendar), Color.Transparent,
        //                            propertyChanged: (bindable, oldValue, newValue) => (bindable as Calendar).ChangeWeekdayRowBackgroundColor((Color)newValue, (Color)oldValue));

        //protected void ChangeWeekdayRowBackgroundColor(Color newValue, Color oldValue)
        //{
        //    if (newValue == oldValue) return;
        //    if (rows.Count > 0)
        //    {
        //        rows.First().Color = newValue;
        //    }
        //}

        ///// <summary>
        ///// Gets or sets the background color of the normal dates.
        ///// </summary>
        ///// <value>The color of the dates background on the even rows.</value>
        //public Color WeekdayRowBackgroundColor
        //{
        //    get { return (Color)GetValue(WeekdayRowBackgroundColorProperty); }
        //    set { SetValue(WeekdayRowBackgroundColorProperty, value); }
        //}

        //#endregion

        protected void ChangeWeekdays()
        {
            if (!WeekdaysShow)
            {
                return;
            }
            var start = CalendarStartDate(StartDate);

            for (int i = 0; i < dayLabels.Count; i++)
            {
                if (WeekdaysFormat.ToLower() == "d1")
                {
                    dayLabels[i].Text = start.ToString("ddd").Substring(0, 1);
                }
                else if (WeekdaysFormat.ToLower() == "d2")
                {
                    dayLabels[i].Text = start.ToString("ddd").Substring(0, 2);
                }
                else
                {
                    dayLabels[i].Text = start.ToString(WeekdaysFormat);
                }
                start = start.AddDays(1);
            }
        }
        protected void ChangeCalendar(CalendarChanges changes)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                OnStartRenderCalendar?.Invoke(this, EventArgs.Empty);
                Content = null;
                if (changes.HasFlag(CalendarChanges.StartDate))
                {
                    TitleLabel.Text = StartDate.ToString(TitleLabelFormat);
                    if (TitleLabels != null)
                    {
                        var tls = StartDate.AddMonths(1);
                        foreach (var tl in TitleLabels)
                        {
                            (tl as Label).Text = tls.ToString(TitleLabelFormat);
                            tls = tls.AddMonths(1);
                        }
                    }
                }

                var start        = CalendarStartDate(StartDate).Date;
                var beginOfMonth = false;
                var endOfMonth   = false;
                var hideLastRow  = false;
                for (int i = 0; i < buttons.Count; i++)
                {
                    endOfMonth   |= beginOfMonth && start.Day == 1;
                    beginOfMonth |= start.Day == 1;

                    if (i < dayLabels.Count && WeekdaysShow && changes.HasFlag(CalendarChanges.StartDay))
                    {
                        if (WeekdaysFormat.ToLower() == "d1")
                        {
                            dayLabels[i].Text = start.ToString("ddd").Substring(0, 1);
                        }
                        else if (WeekdaysFormat.ToLower() == "d2")
                        {
                            dayLabels[i].Text = start.ToString("ddd").Substring(0, 2);
                        }
                        else
                        {
                            dayLabels[i].Text = start.ToString(WeekdaysFormat);
                        }
                    }

                    ChangeWeekNumbers(start, i);

                    if (changes.HasFlag(CalendarChanges.All))
                    {
                        buttons[i].Text = string.Format("{0}", start.Day);
                    }
                    else
                    {
                        buttons[i].TextWithoutMeasure = string.Format("{0}", start.Day);
                    }
                    buttons[i].Date = start;

                    buttons[i].IsOutOfMonth = !(beginOfMonth && !endOfMonth);
                    buttons[i].IsEnabled    = ShowNumOfMonths == 1 || !buttons[i].IsOutOfMonth;

                    if (i == 35 && buttons[i].IsOutOfMonth)
                    {
                        // this means that we are on the bottom row and none of the dates
                        // in it belong to the current month we should hide this row
                        hideLastRow           = true;
                        rows.Last().IsVisible = false;
                    }

                    if (i >= 35 && hideLastRow)
                    {
                        buttons[i].IsVisible = false;
                    }
                    else
                    {
                        if (buttons[i].IsOutOfMonth)
                        {
                            buttons[i].IsVisible = ShowOutOfMonthDates;
                        }
                        else
                        {
                            buttons[i].IsVisible = true;
                        }
                        rows.Last().IsVisible = true;
                    }

                    SpecialDate sd = null;
                    if (SpecialDates != null)
                    {
                        sd = SpecialDates.FirstOrDefault(s => s.Date.Date == start.Date);
                    }

                    SetButtonNormal(buttons[i]);

                    if ((MinDate.HasValue && start < MinDate) || (MaxDate.HasValue && start > MaxDate) || (DisableAllDates && sd == null))
                    {
                        SetButtonDisabled(buttons[i]);
                    }
                    else if (buttons[i].IsEnabled && SelectedDates.Select(d => d.Date).Contains(start.Date))
                    {
                        SetButtonSelected(buttons[i], sd);
                    }
                    else if (sd != null)
                    {
                        SetButtonSpecial(buttons[i], sd);
                    }

                    start = start.AddDays(1);
                    if (i != 0 && (i + 1) % 42 == 0)
                    {
                        beginOfMonth = false;
                        endOfMonth   = false;
                        start        = CalendarStartDate(start);
                    }
                }
                if (DisableDatesLimitToMaxMinRange)
                {
                    TitleLeftArrow.IsEnabled  = !(MinDate.HasValue && CalendarStartDate(StartDate).Date < MinDate);
                    TitleRightArrow.IsEnabled = !(MaxDate.HasValue && start > MaxDate);
                }
                Content = MainView;
                OnEndRenderCalendar?.Invoke(this, EventArgs.Empty);
            });
        }