我在Xamarin.Android应用程序中使用MaterialCalendarView库,该库在大多数情况下都运行良好,但无法将跨度装饰添加到日历中。


Documentation and example for span decoration


这是我的代码:

public class Calendar : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.Calendar);

            MaterialCalendarView calendar = FindViewById<MaterialCalendarView>(Resource.Id.calendarView);
            calendar.SetSelectedDate(Java.Util.Calendar.GetInstance(Java.Util.Locale.English));

            List<CalendarDay> dates = new List<CalendarDay>();

            CalendarDay a = CalendarDay.From(2017, 7, 10);
            CalendarDay b = CalendarDay.From(2017, 7, 15);
            CalendarDay c = CalendarDay.From(2017, 7, 9);
            CalendarDay d = CalendarDay.From(2017, 6, 1);

            dates.Add(a);
            dates.Add(b);
            dates.Add(c);
            dates.Add(d);

            //calendar.Post(() =>
            //{
            //    calendar.AddDecorators(new EventDecorator(Android.Resource.Color.HoloOrangeLight, dates));
            //});

            calendar.AddDecorators(new EventDecorator(Android.Resource.Color.HoloOrangeLight, dates));

        }
    }

class EventDecorator : Java.Lang.Object, IDayViewDecorator
{
    private int color;
    private List<CalendarDay> dates;

    public EventDecorator(int color, List<CalendarDay> dates)
    {
        this.color = color;
        this.dates = dates;
    }

    public void Decorate(DayViewFacade view)
    {
        view.AddSpan(new DotSpan(5, color));
    }

    public bool ShouldDecorate(CalendarDay day)
    {
        //return dates.Contains(day);
        return dates.Exists(e => (e == day));
    }
}


任何帮助,将不胜感激!

最佳答案

我无法在日历上添加跨度装饰。


问题在于,当您使用ShouldDecoratedates.Contains(day)时,您的dates.Exists(e => (e == day))总是返回false。

它将比较HashCode触发,因此它将始终返回false,如下所示修改您的代码可以解决此问题:

if (dates[0].ToString() == day.ToString() || dates[1].ToString() == day.ToString() || dates[2].ToString() == day.ToString() || dates[3].ToString() == day.ToString())
{
      return true;
}
else
{
       return false;
}


效果是这样的:

android - 使用Xamarin.Android中的MaterialCalendarView绑定(bind)库添加装饰-LMLPHP

10-08 03:09
查看更多